BlockContentSelection.php

Namespace

Drupal\block_content\Plugin\EntityReferenceSelection

File

core/modules/block_content/src/Plugin/EntityReferenceSelection/BlockContentSelection.php

View source
<?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.
 */
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());
  }

}

Classes

Title Deprecated Summary
BlockContentSelection Provides specific selection control for the block_content entity type.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.