You are here

trait FilteredPluginManagerTrait in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Plugin/FilteredPluginManagerTrait.php \Drupal\Core\Plugin\FilteredPluginManagerTrait
  2. 9 core/lib/Drupal/Core/Plugin/FilteredPluginManagerTrait.php \Drupal\Core\Plugin\FilteredPluginManagerTrait

Provides a trait for plugin managers that allow filtering plugin definitions.

Hierarchy

4 files declare their use of FilteredPluginManagerTrait
BlockManager.php in core/lib/Drupal/Core/Block/BlockManager.php
ConditionManager.php in core/lib/Drupal/Core/Condition/ConditionManager.php
FilteredPluginManagerTraitTest.php in core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php
LayoutPluginManager.php in core/lib/Drupal/Core/Layout/LayoutPluginManager.php

File

core/lib/Drupal/Core/Plugin/FilteredPluginManagerTrait.php, line 12

Namespace

Drupal\Core\Plugin
View source
trait FilteredPluginManagerTrait {
  use ContextAwarePluginManagerTrait;

  /**
   * Implements \Drupal\Core\Plugin\FilteredPluginManagerInterface::getFilteredDefinitions().
   */
  public function getFilteredDefinitions($consumer, $contexts = NULL, array $extra = []) {
    if (!is_null($contexts)) {
      $definitions = $this
        ->getDefinitionsForContexts($contexts);
    }
    else {
      $definitions = $this
        ->getDefinitions();
    }
    $type = $this
      ->getType();
    $hooks = [];
    $hooks[] = "plugin_filter_{$type}";
    $hooks[] = "plugin_filter_{$type}__{$consumer}";
    $this
      ->moduleHandler()
      ->alter($hooks, $definitions, $extra, $consumer);
    $this
      ->themeManager()
      ->alter($hooks, $definitions, $extra, $consumer);
    return $definitions;
  }

  /**
   * A string identifying the plugin type.
   *
   * This string should be unique and generally will correspond to the string
   * used by the discovery, e.g. the annotation class or the YAML file name.
   *
   * @return string
   *   A string identifying the plugin type.
   */
  protected abstract function getType();

  /**
   * Wraps the module handler.
   *
   * @return \Drupal\Core\Extension\ModuleHandlerInterface
   *   The module handler.
   */
  protected function moduleHandler() {
    if (property_exists($this, 'moduleHandler') && $this->moduleHandler instanceof ModuleHandlerInterface) {
      return $this->moduleHandler;
    }
    return \Drupal::service('module_handler');
  }

  /**
   * Wraps the theme manager.
   *
   * @return \Drupal\Core\Theme\ThemeManagerInterface
   *   The theme manager.
   */
  protected function themeManager() {
    if (property_exists($this, 'themeManager') && $this->themeManager instanceof ThemeManagerInterface) {
      return $this->themeManager;
    }
    return \Drupal::service('theme.manager');
  }

}

Members