3 namespace Drupal\ctools\Plugin\Condition;
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;
14 * Provides a 'Entity Bundle' condition.
17 * id = "entity_bundle",
18 * deriver = "\Drupal\ctools\Plugin\Deriver\EntityBundle"
22 class EntityBundle extends ConditionPluginBase implements ConstraintConditionInterface, ContainerFactoryPluginInterface {
25 * The entity type bundle info service.
27 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
29 protected $entityTypeBundleInfo;
32 * @var \Drupal\Core\Entity\EntityTypeInterface|null
37 * Creates a new EntityBundle instance.
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.
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());
62 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64 $container->get('entity_type.manager'),
65 $container->get('entity_type.bundle.info'),
75 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
77 $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->bundleOf->id());
78 foreach ($bundles as $id => $info) {
79 $options[$id] = $info['label'];
81 $form['bundles'] = array(
82 '#title' => $this->pluginDefinition['label'],
83 '#type' => 'checkboxes',
84 '#options' => $options,
85 '#default_value' => $this->configuration['bundles'],
87 return parent::buildConfigurationForm($form, $form_state);
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);
101 public function evaluate() {
102 if (empty($this->configuration['bundles']) && !$this->isNegated()) {
105 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
106 $entity = $this->getContextValue($this->bundleOf->id());
107 return !empty($this->configuration['bundles'][$entity->bundle()]);
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));
120 $bundle = reset($this->configuration['bundles']);
121 return $this->t('@bundle_type is @bundle', array('@bundle_type' => $this->bundleOf->getBundleLabel(), '@bundle' => $bundle));
127 public function defaultConfiguration() {
128 return array('bundles' => array()) + parent::defaultConfiguration();
134 * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
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]);
149 * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
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);