3cf96006aa7523e215a1509b7eb1df1caf1d0b8d
[yaffs-website] / web / modules / contrib / ctools / src / Plugin / Condition / NodeType.php
1 <?php
2
3 namespace Drupal\ctools\Plugin\Condition;
4
5 use Drupal\node\Plugin\Condition\NodeType as CoreNodeType;
6 use Drupal\ctools\ConstraintConditionInterface;
7
8 class NodeType extends CoreNodeType implements ConstraintConditionInterface {
9
10   /**
11    * {@inheritdoc}
12    *
13    * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
14    */
15   public function applyConstraints(array $contexts = array()) {
16     // Nullify any bundle constraints on contexts we care about.
17     $this->removeConstraints($contexts);
18     // If a single bundle is configured, we can set a proper constraint.
19     if (count($this->configuration['bundles']) == 1) {
20       $bundle = array_values($this->configuration['bundles']);
21       foreach ($this->getContextMapping() as $definition_id => $context_id) {
22         $contexts[$context_id]->getContextDefinition()->addConstraint('Bundle', ['value' => $bundle[0]]);
23       }
24     }
25   }
26
27   /**
28    * {@inheritdoc}
29    *
30    * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
31    */
32   public function removeConstraints(array $contexts = array()) {
33     // Reset the bundle constraint for any context we've mapped.
34     foreach ($this->getContextMapping() as $definition_id => $context_id) {
35       $constraints = $contexts[$context_id]->getContextDefinition()->getConstraints();
36       unset($constraints['Bundle']);
37       $contexts[$context_id]->getContextDefinition()->setConstraints($constraints);
38     }
39   }
40
41 }