You are here

pluginController.php in Plugin 8

Contains \Drupal\plugin\PluginController.

File

src/Controller/pluginController.php
View source
<?php

/**
 * @file
 * Contains \Drupal\plugin\PluginController.
 */
namespace Drupal\plugin\Controller;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Component\Utility\String;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Composer\Autoload\ClassLoader;
use Drupal\Core\Controller\ControllerBase;

/**
 * Provides content for dialog tests.
 */
class PluginController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('class_loader'));
  }

  /**
   * Constructs a SystemInfoController object.
   *
   * @param \Drupal\system\SystemManager $systemManager
   *   System manager service.
   */
  public function __construct(ClassLoader $class_loader) {
    $this->classLoader = $class_loader;
  }

  /**
   * Returns example content for dialog testing.
   */
  public function pluginSourceCode($service_id, $plugin_id) {
    try {
      $definitions = \Drupal::service($service_id)
        ->getDefinitions();
    } catch (\Exception $e) {
      throw new NotFoundHttpException();
    }
    if (isset($definitions[$plugin_id])) {
      $definition = $definitions[$plugin_id];
    }
    else {
      throw new NotFoundHttpException();
    }

    // Some classes stats with '\'.
    $class = ltrim($definition['class'], '\\');
    $file = $this->classLoader
      ->findFile($class);

    //drupal_set_title($path);
    $build['#title'] = $class;
    $build['#markup'] = highlight_file($file, TRUE);
    return $build;
  }

  /**
   *
   */
  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;
  }

  /**
   * Convert a multi-level array to UL list.
   */
  protected function array2ul($array) {
    $output = '<ul>';
    foreach ($array as $key => $value) {
      if (is_object($value)) {
        continue;
      }
      $value = is_array($value) ? $this
        ->array2ul($value) : String::checkPlain($value);
      $output .= "<li><b>{$key}:</b> <em>{$value}</em></li>";
    }
    return $output . '</ul>';
  }

}

Classes

Namesort descending Description
PluginController Provides content for dialog tests.