class ProcessDecorator

Allows custom processing of the discovered definition.

Example use cases include adding in default values for a definition, or providing a backwards compatibility layer for renamed definition properties.

Hierarchy

Expanded class hierarchy of ProcessDecorator

8 files declare their use of ProcessDecorator
DefaultsTestPluginManager.php in drupal/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php
Contains Drupal\plugin_test\Plugin\DefaultsTestPluginManager.
EntityManager.php in drupal/core/lib/Drupal/Core/Entity/EntityManager.php
Contains \Drupal\Core\Entity\EntityManager.
FormatterPluginManager.php in drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php
Definition of Drupal\field\Plugin\Type\Formatter\FormatterPluginManager..
JoinManager.php in drupal/core/modules/views/lib/Drupal/views/Plugin/Type/JoinManager.php
Definition of Drupal\views\Plugin\Type\JoinManager.
LayoutManager.php in drupal/core/modules/layout/lib/Drupal/layout/Plugin/Type/LayoutManager.php
Definition of Drupal\layout\Plugin\Type\LayoutManager.

... See full list

File

drupal/core/lib/Drupal/Component/Plugin/Discovery/ProcessDecorator.php, line 16
Contains \Drupal\Component\Plugin\Discovery\ProcessDecorator.

Namespace

Drupal\Component\Plugin\Discovery
View source
class ProcessDecorator implements DiscoveryInterface {

  /**
   * The Discovery object being decorated.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
   */
  protected $decorated;

  /**
   * The processor callback to run on each discovered definition.
   *
   * @var callable
   */
  protected $processCallback;

  /**
   * Constructs a \Drupal\Component\Plugin\Discovery\ProcessDecorator object.
   *
   * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
   *   The discovery object that is being decorated.
   * @param callable $process_callback
   *   The processor callback to run on each discovered definition. The
   *   callback will be called with the following arguments:
   *   - array $definition: the discovered definition, that the callback
   *     should accept by reference and modify in place.
   *   - string $plugin_id: the corresponding plugin_id.
   */
  public function __construct(DiscoveryInterface $decorated, $process_callback) {
    $this->decorated = $decorated;
    $this->processCallback = $process_callback;
  }

  /**
   * Implements \Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition().
   */
  public function getDefinition($plugin_id) {
    $definitions = $this
      ->getDefinitions();
    if (isset($definitions[$plugin_id])) {
      return $definitions[$plugin_id];
    }
  }

  /**
   * Implements \Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions().
   */
  public function getDefinitions() {
    $definitions = $this->decorated
      ->getDefinitions();
    foreach ($definitions as $plugin_id => &$definition) {
      call_user_func_array($this->processCallback, array(
        &$definition,
        $plugin_id,
      ));
    }
    return $definitions;
  }

  /**
   * Passes through all unknown calls onto the decorated object.
   */
  public function __call($method, $args) {
    return call_user_func_array(array(
      $this->decorated,
      $method,
    ), $args);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProcessDecorator::$decorated protected property The Discovery object being decorated.
ProcessDecorator::$processCallback protected property The processor callback to run on each discovered definition.
ProcessDecorator::getDefinition public function Implements \Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition(). Overrides DiscoveryInterface::getDefinition
ProcessDecorator::getDefinitions public function Implements \Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions(). Overrides DiscoveryInterface::getDefinitions
ProcessDecorator::__call public function Passes through all unknown calls onto the decorated object.
ProcessDecorator::__construct public function Constructs a \Drupal\Component\Plugin\Discovery\ProcessDecorator object.