function BlockContentHooks::queryEntityReferenceAlter

Implements hook_query_TAG_alter().

Alters any 'entity_reference' query where the entity type is 'block_content' and the query has the tag 'block_content_access'.

These queries should only return reusable blocks unless a condition on 'reusable' is explicitly set.

Block_content entities that are not reusable should by default not be selectable as entity reference values. A module can still create an instance of \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface that will allow selection of non-reusable blocks by explicitly setting a condition on the 'reusable' field.

See also

\Drupal\block_content\BlockContentAccessControlHandler

File

core/modules/block_content/src/Hook/BlockContentHooks.php, line 102

Class

BlockContentHooks
Hook implementations for block_content.

Namespace

Drupal\block_content\Hook

Code

public function queryEntityReferenceAlter(AlterableInterface $query) : void {
    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())) {
            $query->condition("{$data_table}.reusable", TRUE);
        }
    }
}

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