diff --git a/core/lib/Drupal/Core/Entity/Plugin/Condition/Deriver/EntityBundle.php b/core/lib/Drupal/Core/Entity/Plugin/Condition/Deriver/EntityBundle.php new file mode 100644 index 0000000..008e34d --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Plugin/Condition/Deriver/EntityBundle.php @@ -0,0 +1,89 @@ +entityTypeManager = $entity_type_manager; + $this->stringTranslation = $string_translation; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, $base_plugin_id) { + return new static( + $container->get('entity.manager'), + $container->get('string_translation') + ); + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { + if ($entity_type->hasKey('bundle')) { + $this->derivatives[$entity_type_id] = $base_plugin_definition; + $this->derivatives[$entity_type_id]['label'] = $this->getEntityBundleLabel($entity_type); + $this->derivatives[$entity_type_id]['context'] = [ + $entity_type_id => new ContextDefinition('entity:' . $entity_type_id), + ]; + } + } + return $this->derivatives; + } + + /** + * Provides the bundle label with a fallback when not defined. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type we are looking the bundle label for. + * + * @return \Drupal\Core\StringTranslation\TranslatableMarkup + * The entity bundle label or a fallback label. + */ + protected function getEntityBundleLabel($entity_type) { + if ($label = $entity_type->getBundleLabel()) { + return $this->t('@label', ['@label' => $label]); + } + + $fallback = $entity_type->getLabel(); + if ($bundle_entity_type = $entity_type->getBundleEntityType()) { + // This is a better fallback. + $fallback = $this->entityTypeManager->getDefinition($bundle_entity_type)->getLabel(); + } + + return $this->t('@label bundle', ['@label' => $fallback]); + } + +} diff --git a/core/lib/Drupal/Core/Entity/Plugin/Condition/EntityBundle.php b/core/lib/Drupal/Core/Entity/Plugin/Condition/EntityBundle.php new file mode 100644 index 0000000..8b91888 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Plugin/Condition/EntityBundle.php @@ -0,0 +1,138 @@ +entityTypeBundleInfo = $entity_type_bundle_info; + $this->bundleOf = $entity_type_manager->getDefinition($this->getDerivativeId()); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $container->get('entity_type.manager'), + $container->get('entity_type.bundle.info'), + $configuration, + $plugin_id, + $plugin_definition + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $options = []; + $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->bundleOf->id()); + foreach ($bundles as $id => $info) { + $options[$id] = $info['label']; + } + $form['bundles'] = [ + '#title' => $this->pluginDefinition['label'], + '#type' => 'checkboxes', + '#options' => $options, + '#default_value' => $this->configuration['bundles'], + ]; + return parent::buildConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['bundles'] = array_filter($form_state->getValue('bundles')); + parent::submitConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + if (empty($this->configuration['bundles']) && !$this->isNegated()) { + return TRUE; + } + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $this->getContextValue($this->bundleOf->id()); + return !empty($this->configuration['bundles'][$entity->bundle()]); + } + + /** + * {@inheritdoc} + */ + public function summary() { + if (count($this->configuration['bundles']) > 1) { + $bundles = $this->configuration['bundles']; + $last = array_pop($bundles); + $bundles = implode(', ', $bundles); + return $this->t('@bundle_type is @bundles or @last', [ + '@bundle_type' => $this->bundleOf->getBundleLabel(), + '@bundles' => $bundles, + '@last' => $last, + ]); + } + $bundle = reset($this->configuration['bundles']); + return $this->t('@bundle_type is @bundle', [ + '@bundle_type' => $this->bundleOf->getBundleLabel(), + '@bundle' => $bundle, + ]); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'bundles' => [], + ] + parent::defaultConfiguration(); + } + +}