PluginDependencyTrait.php 1.36 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Plugin;

5
use Drupal\Component\Plugin\DependentPluginInterface;
6 7 8 9 10 11 12 13 14 15 16
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Entity\DependencyTrait;

/**
 * Provides a trait for calculating the dependencies of a plugin.
 */
trait PluginDependencyTrait {

  use DependencyTrait;

  /**
17 18 19 20 21
   * Calculates and adds dependencies of a specific plugin instance.
   *
   * Dependencies are added for the module that provides the plugin, as well
   * as any dependencies declared by the instance's calculateDependencies()
   * method, if it implements
22
   * \Drupal\Component\Plugin\DependentPluginInterface.
23 24 25 26 27 28 29 30 31 32 33
   *
   * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
   *   The plugin instance.
   */
  protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
    $definition = $instance->getPluginDefinition();
    $this->addDependency('module', $definition['provider']);
    // Plugins can declare additional dependencies in their definition.
    if (isset($definition['config_dependencies'])) {
      $this->addDependencies($definition['config_dependencies']);
    }
34 35
    // If a plugin is dependent, calculate its dependencies.
    if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
36 37 38 39 40
      $this->addDependencies($plugin_dependencies);
    }
  }

}