trait PluginDependencyTrait

Same name in other branches
  1. 9 core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php \Drupal\Core\Plugin\PluginDependencyTrait
  2. 8.9.x core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php \Drupal\Core\Plugin\PluginDependencyTrait
  3. 11.x core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php \Drupal\Core\Plugin\PluginDependencyTrait

Provides a trait for calculating the dependencies of a plugin.

Hierarchy

5 files declare their use of PluginDependencyTrait
ConfigEntityBase.php in core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
DisplayPluginBase.php in core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
EntityField.php in core/modules/views/src/Plugin/views/field/EntityField.php
PluginDependencyTraitTest.php in core/tests/Drupal/Tests/Core/Plugin/PluginDependencyTraitTest.php
VariantBase.php in core/lib/Drupal/Core/Display/VariantBase.php

File

core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php, line 15

Namespace

Drupal\Core\Plugin
View source
trait PluginDependencyTrait {
    use DependencyTrait;
    
    /**
     * Calculates and returns 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
     * \Drupal\Component\Plugin\DependentPluginInterface.
     *
     * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
     *   The plugin instance.
     *
     * @return array
     *   An array of dependencies keyed by the type of dependency.
     */
    protected function getPluginDependencies(PluginInspectionInterface $instance) {
        $dependencies = [];
        $definition = $instance->getPluginDefinition();
        $provider = NULL;
        $config_dependencies = [];
        if ($definition instanceof PluginDefinitionInterface) {
            $provider = $definition->getProvider();
            if ($definition instanceof DependentPluginDefinitionInterface) {
                $config_dependencies = $definition->getConfigDependencies();
            }
        }
        elseif (is_array($definition)) {
            $provider = $definition['provider'];
            if (isset($definition['config_dependencies'])) {
                $config_dependencies = $definition['config_dependencies'];
            }
        }
        // Add the provider as a dependency, taking into account if it's a module or
        // a theme.
        if ($provider) {
            if ($provider === 'core' || $this->moduleHandler()
                ->moduleExists($provider)) {
                $dependencies['module'][] = $provider;
            }
            elseif ($this->themeHandler()
                ->themeExists($provider)) {
                $dependencies['theme'][] = $provider;
            }
        }
        // Add the config dependencies.
        if ($config_dependencies) {
            $dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
        }
        // If a plugin is dependent, calculate its dependencies.
        if ($instance instanceof DependentPluginInterface && ($plugin_dependencies = $instance->calculateDependencies())) {
            $dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
        }
        return $dependencies;
    }
    
    /**
     * 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
     * \Drupal\Component\Plugin\DependentPluginInterface.
     *
     * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
     *   The plugin instance.
     */
    protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
        $this->addDependencies($this->getPluginDependencies($instance));
    }
    
    /**
     * Wraps the module handler.
     *
     * @return \Drupal\Core\Extension\ModuleHandlerInterface
     *   The module handler.
     */
    protected function moduleHandler() {
        return \Drupal::moduleHandler();
    }
    
    /**
     * Wraps the theme handler.
     *
     * @return \Drupal\Core\Extension\ThemeHandlerInterface
     *   The theme handler.
     */
    protected function themeHandler() {
        return \Drupal::service('theme_handler');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance.
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.