function block_content_query_entity_reference_alter

Same name in other branches
  1. 8.9.x core/modules/block_content/block_content.module \block_content_query_entity_reference_alter()
  2. 10 core/modules/block_content/block_content.module \block_content_query_entity_reference_alter()
  3. 11.x core/modules/block_content/block_content.module \block_content_query_entity_reference_alter()

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/block_content.module, line 140

Code

function block_content_query_entity_reference_alter(AlterableInterface $query) {
    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.