Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / node / src / Plugin / Condition / NodeType.php
1 <?php
2
3 namespace Drupal\node\Plugin\Condition;
4
5 use Drupal\Core\Condition\ConditionPluginBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Provides a 'Node Type' condition.
13  *
14  * @Condition(
15  *   id = "node_type",
16  *   label = @Translation("Node Bundle"),
17  *   context = {
18  *     "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
19  *   }
20  * )
21  */
22 class NodeType extends ConditionPluginBase implements ContainerFactoryPluginInterface {
23
24   /**
25    * The entity storage.
26    *
27    * @var \Drupal\Core\Entity\EntityStorageInterface
28    */
29   protected $entityStorage;
30
31   /**
32    * Creates a new NodeType instance.
33    *
34    * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
35    *   The entity storage.
36    * @param array $configuration
37    *   The plugin configuration, i.e. an array with configuration values keyed
38    *   by configuration option name. The special key 'context' may be used to
39    *   initialize the defined contexts by setting it to an array of context
40    *   values keyed by context names.
41    * @param string $plugin_id
42    *   The plugin_id for the plugin instance.
43    * @param mixed $plugin_definition
44    *   The plugin implementation definition.
45    */
46   public function __construct(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) {
47     parent::__construct($configuration, $plugin_id, $plugin_definition);
48     $this->entityStorage = $entity_storage;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55     return new static(
56       $container->get('entity.manager')->getStorage('node_type'),
57       $configuration,
58       $plugin_id,
59       $plugin_definition
60     );
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
67     $options = [];
68     $node_types = $this->entityStorage->loadMultiple();
69     foreach ($node_types as $type) {
70       $options[$type->id()] = $type->label();
71     }
72     $form['bundles'] = [
73       '#title' => $this->t('Node types'),
74       '#type' => 'checkboxes',
75       '#options' => $options,
76       '#default_value' => $this->configuration['bundles'],
77     ];
78     return parent::buildConfigurationForm($form, $form_state);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
85     $this->configuration['bundles'] = array_filter($form_state->getValue('bundles'));
86     parent::submitConfigurationForm($form, $form_state);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function summary() {
93     if (count($this->configuration['bundles']) > 1) {
94       $bundles = $this->configuration['bundles'];
95       $last = array_pop($bundles);
96       $bundles = implode(', ', $bundles);
97       return $this->t('The node bundle is @bundles or @last', ['@bundles' => $bundles, '@last' => $last]);
98     }
99     $bundle = reset($this->configuration['bundles']);
100     return $this->t('The node bundle is @bundle', ['@bundle' => $bundle]);
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function evaluate() {
107     if (empty($this->configuration['bundles']) && !$this->isNegated()) {
108       return TRUE;
109     }
110     $node = $this->getContextValue('node');
111     return !empty($this->configuration['bundles'][$node->getType()]);
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function defaultConfiguration() {
118     return ['bundles' => []] + parent::defaultConfiguration();
119   }
120
121 }