Commit 00d7504c authored by catch's avatar catch
Browse files

fix: #2987159 Create an entity reference selection plugin for custom blocks...

fix: #2987159 Create an entity reference selection plugin for custom blocks that filters out non-reusable blocks

By: tedbow
By: smustgrave
By: amateescu
By: berdir
By: acbramley
(cherry picked from commit 58df9d1ef672270dcc235665fd4e6608ca7f153e)
parent f3b06df7
Loading
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\block\BlockConfigUpdater;
use Drupal\block\BlockInterface;
use Drupal\block_content\Plugin\EntityReferenceSelection\BlockContentSelection;
use Drupal\Core\Entity\EntityInterface;
use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Database\Query\SelectInterface;
@@ -106,9 +107,16 @@ public function entityTypeAlter(array &$entity_types) : void {
   */
  #[Hook('query_entity_reference_alter')]
  public function queryEntityReferenceAlter(AlterableInterface $query): void {
    if (($query->alterMetaData['entity_reference_selection_handler'] ?? NULL) instanceof BlockContentSelection) {
      // The entity reference selection plugin module provided by this module
      // already filters out non-reusable blocks so no altering of the query is
      // needed.
      return;
    }
    if ($query instanceof SelectInterface && $query->getMetaData('entity_type') === 'block_content' && $query->hasTag('block_content_access')) {
      $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable();
      if (array_key_exists($data_table, $query->getTables()) && !_block_content_has_reusable_condition($query->conditions(), $query->getTables())) {
        @trigger_error('Automatically filtering block_content entity reference selection queries to only reusable blocks is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Either add the condition manually in buildEntityQuery, or extend \Drupal\block_content\Plugin\EntityReferenceSelection\BlockContentSelection. See https://www.drupal.org/node/3521459', E_USER_DEPRECATED);
        $query->condition("{$data_table}.reusable", TRUE);
      }
    }
+43 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\block_content\Plugin\EntityReferenceSelection;

use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Entity\Attribute\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides specific selection control for the block_content entity type.
 */
#[EntityReferenceSelection(
  id: "default:block_content",
  label: new TranslatableMarkup("Block content selection"),
  group: "default",
  weight: 1,
  entity_types: ["block_content"],
)]
class BlockContentSelection extends DefaultSelection {

  /**
   * {@inheritdoc}
   */
  protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
    $query = parent::buildEntityQuery($match, $match_operator);
    // Only reusable blocks should be able to be referenced.
    $query->condition('reusable', TRUE);
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().
    return array_filter($entities, static fn (BlockContentInterface $blockContent) => $blockContent->isReusable());
  }

}
+3 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
@@ -146,7 +147,9 @@ public function testQueriesNotAltered(): void {
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  #[IgnoreDeprecations]
  public function testNoConditions(): void {
    $this->expectDeprecation('Automatically filtering block_content entity reference selection queries to only reusable blocks is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Either add the condition manually in buildEntityQuery, or extend \Drupal\block_content\Plugin\EntityReferenceSelection\BlockContentSelection. See https://www.drupal.org/node/3521459');
    $this->assertEquals(
      $this->expectations['block_reusable'],
      $this->selectionHandler->getReferenceableEntities()