Plugins: Combine fields, widgets, methods

Edit: Fields are proving far more difficult than the widget example below was. I’ll let you know if I find a solution, otherwise it may be a Bastian problem. :wink:

Here’s an example of extending the dashboard to build widgets from plugin directories as well, though it involves modifying the core files directly.

/panel/app/controllers/views/dashboard.php


<?php

class DashboardController extends Controller {

  public function index() {

    $site    = site();
    $widgets = array();
    $wroot   = kirby()->roots()->widgets();
    $wdirs   = dir::read($wroot);

    // for plugins
    $proot   = kirby()->roots()->plugins();
    $pdirs   = dir::read($proot);

    // fetch all top-level pages in the right order
    $blueprint = blueprint::find($site);
    $pages     = api::subpages($site->children(), $blueprint);

    foreach($wdirs as $dir) {
      $file = $wroot . DS . $dir . DS . $dir . '.php';
      if(file_exists($file)) {
        $widgets[$dir] = require($file);
      }
    }

    // for plugins
    foreach($pdirs as $dir) {
      $file = $proot . DS . $dir . DS . 'widgets' . DS . $dir . '.php';
      if(file_exists($file)) {
        $widgets[$dir] = require($file);
      }
    }

    return view('dashboard/index', array(
      'topbar' => new Snippet('pages/topbar', array(
        'breadcrumb' => new Snippet('breadcrumb'),
        'search'     => purl('pages/search/')
      )),
      'history' => history::get(),
      'site'    => $site,
      'pages'   => $pages,
      'widgets' => $widgets,
      'user'    => site()->user(),
    ));
  }

}

1 Like