Version 1
[yaffs-website] / web / modules / contrib / ctools / src / Plugin / Condition / EntityBundle.php
1 <?php
2
3 namespace Drupal\ctools\Plugin\Condition;
4
5 use Drupal\Core\Condition\ConditionPluginBase;
6 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\ctools\ConstraintConditionInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a 'Entity Bundle' condition.
15  *
16  * @Condition(
17  *   id = "entity_bundle",
18  *   deriver = "\Drupal\ctools\Plugin\Deriver\EntityBundle"
19  * )
20  *
21  */
22 class EntityBundle extends ConditionPluginBase implements ConstraintConditionInterface, ContainerFactoryPluginInterface {
23
24   /**
25    * The entity type bundle info service.
26    *
27    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
28    */
29   protected $entityTypeBundleInfo;
30
31   /**
32    * @var \Drupal\Core\Entity\EntityTypeInterface|null
33    */
34   protected $bundleOf;
35
36   /**
37    * Creates a new EntityBundle instance.
38    *
39    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
40    *   The entity type manager.
41    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
42    *   The entity type bundle info service.
43    * @param array $configuration
44    *   The plugin configuration, i.e. an array with configuration values keyed
45    *   by configuration option name. The special key 'context' may be used to
46    *   initialize the defined contexts by setting it to an array of context
47    *   values keyed by context names.
48    * @param string $plugin_id
49    *   The plugin_id for the plugin instance.
50    * @param mixed $plugin_definition
51    *   The plugin implementation definition.
52    */
53   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, array $configuration, $plugin_id, $plugin_definition) {
54     parent::__construct($configuration, $plugin_id, $plugin_definition);
55     $this->entityTypeBundleInfo = $entity_type_bundle_info;
56     $this->bundleOf = $entity_type_manager->getDefinition($this->getDerivativeId());
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
63     return new static(
64       $container->get('entity_type.manager'),
65       $container->get('entity_type.bundle.info'),
66       $configuration,
67       $plugin_id,
68       $plugin_definition
69     );
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
76     $options = array();
77     $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->bundleOf->id());
78     foreach ($bundles as $id => $info) {
79       $options[$id] = $info['label'];
80     }
81     $form['bundles'] = array(
82       '#title' => $this->pluginDefinition['label'],
83       '#type' => 'checkboxes',
84       '#options' => $options,
85       '#default_value' => $this->configuration['bundles'],
86     );
87     return parent::buildConfigurationForm($form, $form_state);
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
94     $this->configuration['bundles'] = array_filter($form_state->getValue('bundles'));
95     parent::submitConfigurationForm($form, $form_state);
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function evaluate() {
102     if (empty($this->configuration['bundles']) && !$this->isNegated()) {
103       return TRUE;
104     }
105     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
106     $entity = $this->getContextValue($this->bundleOf->id());
107     return !empty($this->configuration['bundles'][$entity->bundle()]);
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function summary() {
114     if (count($this->configuration['bundles']) > 1) {
115       $bundles = $this->configuration['bundles'];
116       $last = array_pop($bundles);
117       $bundles = implode(', ', $bundles);
118       return $this->t('@bundle_type is @bundles or @last', array('@bundle_type' => $this->bundleOf->getBundleLabel(), '@bundles' => $bundles, '@last' => $last));
119     }
120     $bundle = reset($this->configuration['bundles']);
121     return $this->t('@bundle_type is @bundle', array('@bundle_type' => $this->bundleOf->getBundleLabel(), '@bundle' => $bundle));
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function defaultConfiguration() {
128     return array('bundles' => array()) + parent::defaultConfiguration();
129   }
130
131   /**
132    * {@inheritdoc}
133    *
134    * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
135    */
136   public function applyConstraints(array $contexts = array()) {
137     // Nullify any bundle constraints on contexts we care about.
138     $this->removeConstraints($contexts);
139     $bundle = array_values($this->configuration['bundles']);
140     // There's only one expected context for this plugint type.
141     foreach ($this->getContextMapping() as $definition_id => $context_id) {
142       $contexts[$context_id]->getContextDefinition()->addConstraint('Bundle', ['value' => $bundle]);
143     }
144   }
145
146   /**
147    * {@inheritdoc}
148    *
149    * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
150    */
151   public function removeConstraints(array $contexts = array()) {
152     // Reset the bundle constraint for any context we've mapped.
153     foreach ($this->getContextMapping() as $definition_id => $context_id) {
154       $constraints = $contexts[$context_id]->getContextDefinition()->getConstraints();
155       unset($constraints['Bundle']);
156       $contexts[$context_id]->getContextDefinition()->setConstraints($constraints);
157     }
158   }
159
160 }