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.
Attributes
See also
\Drupal\block_content\BlockContentAccessControlHandler
File
-
core/
modules/ block_content/ src/ Hook/ BlockContentHooks.php, line 111
Class
- BlockContentHooks
- Hook implementations for block_content.
Namespace
Drupal\block_content\HookCode
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);
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.