Unverified Commit 0f22eb1c authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1932810 by Berdir, jibran, joegraduate, joelpittet, JeroenT, RenatoG,...

Issue #1932810 by Berdir, jibran, joegraduate, joelpittet, JeroenT, RenatoG, tim.plunkett, tedbow, andypost, paulocs, hugronaphor, ankithashetty, Meenakshi_j, chr.fritsch, Beanjammin, nikitagupta, EclipseGc, dawehner, xjm, alexpott, fago, tstoeckler, catch, seanB, larowlan: Add entity bundles condition plugin for entities with bundles
parent 3fb0eded
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -339,6 +339,14 @@ condition.plugin:
      sequence:
        type: string

condition.plugin.entity_bundle:*:
  type: condition.plugin
  mapping:
    bundles:
      type: sequence
      sequence:
        type: string

display_variant.plugin:
  type: mapping
  label: 'Display variant'
+59 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Entity\Plugin\Condition\Deriver;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Deriver that creates a condition for each entity type with bundles.
 */
class EntityBundle extends DeriverBase implements ContainerDeriverInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new EntityBundle.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@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'] = $entity_type->getBundleLabel();
        $this->derivatives[$entity_type_id]['provider'] = $entity_type->getProvider();
        $this->derivatives[$entity_type_id]['context_definitions'] = [
          $entity_type_id => EntityContextDefinition::fromEntityType($entity_type),
        ];
      }
    }
    return $this->derivatives;
  }

}
+144 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Entity\Plugin\Condition;

use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the 'Entity Bundle' condition.
 *
 * @Condition(
 *   id = "entity_bundle",
 *   deriver = "\Drupal\Core\Entity\Plugin\Condition\Deriver\EntityBundle",
 * )
 */
class EntityBundle extends ConditionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Creates a new EntityBundle instance.
   *
   * @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.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.bundle.info')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->getDerivativeId());
    $form['bundles'] = [
      '#title' => $this->pluginDefinition['label'],
      '#type' => 'checkboxes',
      '#options' => array_combine(array_keys($bundles), array_column($bundles, 'label')),
      '#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() {
    // Returns true if no bundles are selected and negate option is disabled.
    if (empty($this->configuration['bundles']) && !$this->isNegated()) {
      return TRUE;
    }
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->getContextValue($this->getDerivativeId());
    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);

      if (empty($this->configuration['negate'])) {
        return $this->t('@bundle_type is @bundles or @last', [
          '@bundle_type' => $this->pluginDefinition['label'],
          '@bundles' => $bundles,
          '@last' => $last,
        ]);
      }
      else {
        return $this->t('@bundle_type is not @bundles or @last', [
          '@bundle_type' => $this->pluginDefinition['label'],
          '@bundles' => $bundles,
          '@last' => $last,
        ]);
      }
    }
    $bundle = reset($this->configuration['bundles']);

    if (empty($this->configuration['negate'])) {
      return $this->t('@bundle_type is @bundle', [
        '@bundle_type' => $this->pluginDefinition['label'],
        '@bundle' => $bundle,
      ]);
    }
    else {
      return $this->t('@bundle_type is not @bundle', [
        '@bundle_type' => $this->pluginDefinition['label'],
        '@bundle' => $bundle,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'bundles' => [],
    ] + parent::defaultConfiguration();
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -191,6 +191,7 @@ buildtest
bundable
bundleable
bundleless
bundlenode
buttonpane
buttonset
buytaert
+18 −0
Original line number Diff line number Diff line
@@ -15,3 +15,21 @@ function block_removed_post_updates() {
    'block_post_update_fix_negate_in_conditions' => '9.0.0',
  ];
}

/**
 * Updates the node type visibility condition.
 */
function block_post_update_replace_node_type_condition() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory->listAll('block.block.') as $block_config_name) {
    $block = $config_factory->getEditable($block_config_name);

    if ($block->get('visibility.node_type')) {
      $configuration = $block->get('visibility.node_type');
      $configuration['id'] = 'entity_bundle:node';
      $block->set('visibility.entity_bundle:node', $configuration);
      $block->clear('visibility.node_type');
      $block->save(TRUE);
    }
  }
}
Loading