Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\node\Plugin\Condition\NodeType.
Angie Byron
committed
*/
namespace Drupal\node\Plugin\Condition;
Angie Byron
committed
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityStorageInterface;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Angie Byron
committed
/**
* Provides a 'Node Type' condition.
*
* @Condition(
Angie Byron
committed
* id = "node_type",
* label = @Translation("Node Bundle"),
* context = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
Angie Byron
committed
* }
* )
Angie Byron
committed
*/
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class NodeType extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* The entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;
/**
* Creates a new NodeType instance.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*/
public function __construct(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityStorage = $entity_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('entity.manager')->getStorage('node_type'),
$configuration,
$plugin_id,
$plugin_definition
);
}
Angie Byron
committed
/**
Angie Byron
committed
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
Angie Byron
committed
$options = array();
$node_types = $this->entityStorage->loadMultiple();
foreach ($node_types as $type) {
$options[$type->id()] = $type->label();
Angie Byron
committed
}
$form['bundles'] = array(
'#title' => $this->t('Node types'),
Angie Byron
committed
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $this->configuration['bundles'],
Angie Byron
committed
);
Angie Byron
committed
return parent::buildConfigurationForm($form, $form_state);
Angie Byron
committed
}
/**
Angie Byron
committed
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
Alex Pott
committed
$this->configuration['bundles'] = array_filter($form_state->getValue('bundles'));
Angie Byron
committed
parent::submitConfigurationForm($form, $form_state);
Angie Byron
committed
}
/**
Angie Byron
committed
* {@inheritdoc}
Angie Byron
committed
*/
public function summary() {
if (count($this->configuration['bundles']) > 1) {
$bundles = $this->configuration['bundles'];
$last = array_pop($bundles);
$bundles = implode(', ', $bundles);
return $this->t('The node bundle is @bundles or @last', array('@bundles' => $bundles, '@last' => $last));
Angie Byron
committed
}
Angie Byron
committed
$bundle = reset($this->configuration['bundles']);
return $this->t('The node bundle is @bundle', array('@bundle' => $bundle));
Angie Byron
committed
}
/**
Angie Byron
committed
* {@inheritdoc}
Angie Byron
committed
*/
public function evaluate() {
if (empty($this->configuration['bundles']) && !$this->isNegated()) {
return TRUE;
}
Angie Byron
committed
$node = $this->getContextValue('node');
Angie Byron
committed
return !empty($this->configuration['bundles'][$node->getType()]);
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array('bundles' => array()) + parent::defaultConfiguration();
}