3 namespace Drupal\node\Plugin\Condition;
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;
12 * Provides a 'Node Type' condition.
16 * label = @Translation("Node Bundle"),
18 * "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
22 class NodeType extends ConditionPluginBase implements ContainerFactoryPluginInterface {
27 * @var \Drupal\Core\Entity\EntityStorageInterface
29 protected $entityStorage;
32 * Creates a new NodeType instance.
34 * @param \Drupal\Core\Entity\EntityStorageInterface $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.
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;
54 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56 $container->get('entity.manager')->getStorage('node_type'),
66 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
68 $node_types = $this->entityStorage->loadMultiple();
69 foreach ($node_types as $type) {
70 $options[$type->id()] = $type->label();
73 '#title' => $this->t('Node types'),
74 '#type' => 'checkboxes',
75 '#options' => $options,
76 '#default_value' => $this->configuration['bundles'],
78 return parent::buildConfigurationForm($form, $form_state);
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);
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]);
99 $bundle = reset($this->configuration['bundles']);
100 return $this->t('The node bundle is @bundle', ['@bundle' => $bundle]);
106 public function evaluate() {
107 if (empty($this->configuration['bundles']) && !$this->isNegated()) {
110 $node = $this->getContextValue('node');
111 return !empty($this->configuration['bundles'][$node->getType()]);
117 public function defaultConfiguration() {
118 return ['bundles' => []] + parent::defaultConfiguration();