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

10 files declare their use of ProcessDecorator
CKEditorPluginManager.php in drupal/core/modules/ckeditor/lib/Drupal/ckeditor/CKEditorPluginManager.php
Contains \Drupal\ckeditor\CKEditorPluginManager.
ConstraintManager.php in drupal/core/lib/Drupal/Core/Validation/ConstraintManager.php
Contains \Drupal\Core\Validation\ConstraintManager.
DefaultsTestPluginManager.php in drupal/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php
Contains Drupal\plugin_test\Plugin\DefaultsTestPluginManager.
EditorManager.php in drupal/core/modules/editor/lib/Drupal/editor/Plugin/EditorManager.php
Contains \Drupal\editor\Plugin\InPlaceEditorManager.
InPlaceEditorManager.php in drupal/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditorManager.php
Contains \Drupal\edit\Plugin\InPlaceEditorManager.

... 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,
      ));
    }

    // Allow process callbacks to unset definitions.
    return array_filter($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.