You are here

class ListPlugins in Plugin 8.2

Handles the "list plugin" route.

Hierarchy

Expanded class hierarchy of ListPlugins

1 file declares its use of ListPlugins
ListPluginsTest.php in tests/src/Unit/Controller/ListPluginsTest.php

File

src/Controller/ListPlugins.php, line 18

Namespace

Drupal\plugin\Controller
View source
class ListPlugins extends ListBase {

  /**
   * The class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translator.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   The class resolver.
   */
  public function __construct(TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, ClassResolverInterface $class_resolver) {
    parent::__construct($string_translation, $module_handler);
    $this->classResolver = $class_resolver;
  }

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

  /**
   * Returns the route's title.
   *
   * @param \Drupal\plugin\PluginType\PluginTypeInterface $plugin_type
   *   The plugin type.
   *
   * @return string
   */
  public function title($plugin_type) {
    return $this
      ->t('%label plugins', [
      '%label' => $plugin_type
        ->getLabel(),
    ]);
  }

  /**
   * Handles the route.
   *
   * @param \Drupal\plugin\PluginType\PluginTypeInterface $plugin_type
   *   The plugin type.
   *
   * @return mixed[]|\Symfony\Component\HttpFoundation\Response
   *   A render array or a Symfony response.
   */
  public function execute(PluginTypeInterface $plugin_type) {
    $build = [
      '#empty' => $this
        ->t('There are no available plugins.'),
      '#header' => [
        $this
          ->t('Plugin'),
        $this
          ->t('ID'),
        $this
          ->t('Description'),
        $this
          ->t('Provider'),
        $this
          ->t('Operations'),
      ],
      '#type' => 'table',
    ];
    $plugin_discovery = new TypedDefinitionEnsuringPluginDiscoveryDecorator($plugin_type);

    /** @var \Drupal\plugin\PluginDefinition\PluginDefinitionInterface[] $plugin_definitions */
    $plugin_definitions = $plugin_discovery
      ->getDefinitions();
    ksort($plugin_definitions);
    foreach ($plugin_definitions as $plugin_definition) {
      $operations = [];
      if ($plugin_definition instanceof PluginOperationsProviderDefinitionInterface) {
        $operations_provider_class = $plugin_definition
          ->getOperationsProviderClass();
        if ($operations_provider_class) {

          /** @var \Drupal\plugin\PluginOperationsProviderInterface $operations_provider */
          $operations_provider = $this->classResolver
            ->getInstanceFromDefinition($operations_provider_class);
          $operations = $operations_provider
            ->getOperations($plugin_definition
            ->getId());
        }
      }
      $build[$plugin_definition
        ->getId()] = [
        'label' => [
          '#markup' => $plugin_definition instanceof PluginLabelDefinitionInterface ? (string) $plugin_definition
            ->getLabel() : NULL,
        ],
        'id' => [
          '#markup' => $plugin_definition
            ->getId(),
          '#prefix' => '<code>',
          '#suffix' => '</code>',
        ],
        'description' => [
          '#markup' => $plugin_definition instanceof PluginDescriptionDefinitionInterface ? (string) $plugin_definition
            ->getDescription() : NULL,
        ],
        'provider' => [
          '#markup' => $this
            ->getProviderLabel($plugin_definition
            ->getProvider()),
        ],
        'operations' => [
          '#links' => $operations,
          '#type' => 'operations',
        ],
      ];
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ListBase::$moduleHandler protected property The module handler.
ListBase::getProviderLabel protected function Gets the human-readable provider label.
ListPlugins::$classResolver protected property The class resolver.
ListPlugins::create public static function Instantiates a new instance of this class. Overrides ListBase::create
ListPlugins::execute public function Handles the route.
ListPlugins::title public function Returns the route's title.
ListPlugins::__construct public function Constructs a new instance. Overrides ListBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.