3 namespace Drupal\Core\Condition;
5 use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Executable\ExecutableManagerInterface;
8 use Drupal\Core\Executable\ExecutableInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
11 use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
12 use Drupal\Core\Plugin\DefaultPluginManager;
15 * A plugin manager for condition plugins.
17 * @see \Drupal\Core\Condition\Annotation\Condition
18 * @see \Drupal\Core\Condition\ConditionInterface
19 * @see \Drupal\Core\Condition\ConditionPluginBase
23 class ConditionManager extends DefaultPluginManager implements ExecutableManagerInterface, CategorizingPluginManagerInterface {
25 use CategorizingPluginManagerTrait;
26 use ContextAwarePluginManagerTrait;
29 * Constructs a ConditionManager object.
31 * @param \Traversable $namespaces
32 * An object that implements \Traversable which contains the root paths
33 * keyed by the corresponding namespace to look for plugin implementations.
34 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
35 * Cache backend instance to use.
36 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
37 * The module handler to invoke the alter hook with.
39 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
40 $this->alterInfo('condition_info');
41 $this->setCacheBackend($cache_backend, 'condition_plugins');
43 parent::__construct('Plugin/Condition', $namespaces, $module_handler, 'Drupal\Core\Condition\ConditionInterface', 'Drupal\Core\Condition\Annotation\Condition');
49 public function createInstance($plugin_id, array $configuration = []) {
50 $plugin = $this->getFactory()->createInstance($plugin_id, $configuration);
52 // If we receive any context values via config set it into the plugin.
53 if (!empty($configuration['context'])) {
54 foreach ($configuration['context'] as $name => $context) {
55 $plugin->setContextValue($name, $context);
59 return $plugin->setExecutableManager($this);
65 public function execute(ExecutableInterface $condition) {
66 $result = $condition->evaluate();
67 return $condition->isNegated() ? !$result : $result;