You are here

public function Fasttoggle::description in Fasttoggle 8.2

Displays a page with an overview of our plugin type and plugins.

Lists all the managed object and their settings' plugin definitions by using methods on the \Drupal\fasttoggle\ObjectPluginManager and SettingPluginManger classes. Lists out the description for each object and setting found by invoking methods defined on the plugins themselves. You can find the plugins we have defined in the \Drupal\fasttoggle\Plugin\Object and Setting namespaces.

File

src/Controller/Fasttoggle_overview.php, line 76

Class

Fasttoggle
Route controller for Fasttoggle.

Namespace

Drupal\fasttoggle\Controller

Code

public function description() {
  $build = [];
  $build['intro'] = [
    '#markup' => t("This page lists Fasttoggle plugins and objects that may be managed."),
  ];

  // Get the list of all the Fasttoggle plugins defined on the system from the
  // plugin manager. Note that at this point, what we have is *definitions* of
  // plugins, not the plugins themselves.
  $object_plugin_definitions = $this->objectManager
    ->getDefinitions();

  // Let's output a list of the plugin definitions we now have.
  $items = [];
  foreach ($object_plugin_definitions as $object_plugin_definition) {
    $items[] = t("!id (calories: !calories, foobar: !foobar )", [
      '!id' => $object_plugin_definition['id'],
      '!calories' => $object_plugin_definition['calories'],
      '!foobar' => $object_plugin_definition['foobar'],
    ]);
  }

  // Add our list to the render array.
  $build['plugin_definitions'] = [
    '#theme' => 'item_list',
    '#title' => 'Object type plugin definitions',
    '#items' => $items,
  ];

  // If we want just a single plugin definition, we can use getDefinition().
  // This requires us to know the ID of the plugin we want. This is set in the
  // annotation on the plugin class: see ExampleHamSandwich.
  // $ham_object_plugin_definition = $this->sandwichManager->getDefinition('ham_sandwich');
  // To get an actual plugin, we call createInstance() on the plugin manager,
  // passing the ID of the plugin we want to load. Let's output a list of the
  // actual plugins.
  $items = [];

  // The array of plugin definitions is keyed by plugin id, so we can just use
  // that to load our plugins.
  foreach ($object_plugin_definitions as $plugin_id => $object_plugin_definition) {

    // We now have a plugin! From here on it can be treated just as any other
    // object: have its properties examined, methods called, etc.
    $plugin = $this->objectTypeManager
      ->createInstance($plugin_id, [
      'of' => 'configuration values',
    ]);
    $items[] = $plugin
      ->description();
  }
  $build['plugins'] = [
    '#theme' => 'item_list',
    '#title' => 'Object plugins',
    '#items' => $items,
  ];
  return $build;
}