You are here

public function PluginController::overview in Plugin 8

File

src/Controller/pluginController.php, line 76
Contains \Drupal\plugin\PluginController.

Class

PluginController
Provides content for dialog tests.

Namespace

Drupal\plugin\Controller

Code

public function overview() {
  $header = [
    [
      'data' => t('ID'),
      'class' => 'plugin-id',
    ],
    [
      'data' => t('Module'),
      'class' => 'plugin-module',
    ],
    [
      'data' => t('Description'),
      'class' => 'plugin-description',
    ],
    [
      'data' => t('Actions'),
      'class' => 'plugin-actions',
    ],
  ];

  // Is there a better way to find all available plugins?
  foreach (\Drupal::getContainer()
    ->getServiceIds() as $serviceId) {
    if (strpos($serviceId, 'plugin.manager') === 0) {
      $rows = [];
      $definitions = \Drupal::service($serviceId)
        ->getDefinitions();
      foreach ($definitions as $id => $plugin_definition) {
        $description = [];
        $description['wrapper'] = [
          '#type' => 'details',
          '#title' => t('Definition'),
          '#collapsed' => TRUE,
        ];
        $description['wrapper']['definition']['#markup'] = $this
          ->array2ul($plugin_definition);
        list(, $module) = explode('\\', $plugin_definition['class']);
        $attributes = [
          'class' => [
            'use-ajax',
          ],
          'data-accepts' => 'application/vnd.drupal-modal',
          'data-dialog-options' => json_encode([
            'height' => 700,
            'width' => 1000,
          ]),
        ];
        $url = Url::fromRoute('plugin.source_code', [
          'service_id' => $serviceId,
          'plugin_id' => $id,
        ], [
          'attributes' => $attributes,
        ]);
        $action = [
          '#markup' => \Drupal::l(t('View source'), $url, []),
        ];
        $rows[] = [
          $id,
          $module,
          render($description),
          render($action),
        ];
      }
      $build['services'][$serviceId] = [
        '#type' => 'details',
        '#title' => str_replace('plugin.manager.', '', $serviceId) . ' (' . count($definitions) . ')',
        '#collapsed' => TRUE,
        '#attributes' => [
          'class' => [
            'plugins-wrapper',
          ],
        ],
      ];
      $build['services'][$serviceId]['plugins'] = [
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#empty' => t('No plugins available.'),
      ];
    }
  }
  $build['#attached']['library'][] = 'core/drupal.ajax';
  $build['#attached']['library'][] = 'plugin/plugin.overview';
  return $build;
}