Mercurial > hg > cmmr2012-drupal-site
diff core/modules/block_content/block_content.module @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children | 12f9dff5fda9 |
line wrap: on
line diff
--- a/core/modules/block_content/block_content.module Thu Feb 28 11:14:44 2019 +0000 +++ b/core/modules/block_content/block_content.module Thu Feb 28 13:11:55 2019 +0000 @@ -8,6 +8,9 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\Core\Database\Query\SelectInterface; +use Drupal\Core\Database\Query\AlterableInterface; +use Drupal\Core\Database\Query\ConditionInterface; /** * Implements hook_help(). @@ -105,3 +108,73 @@ return $field; } + +/** + * 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 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 \Drupal\block_content\BlockContentAccessControlHandler + */ +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); + } + } +} + +/** + * Utility function to find nested conditions using the reusable field. + * + * @todo Replace this function with a call to the API in + * https://www.drupal.org/project/drupal/issues/2984930 + * + * @param array $condition + * The condition or condition group to check. + * @param array $tables + * The tables from the related select query. + * + * @see \Drupal\Core\Database\Query\SelectInterface::getTables + * + * @return bool + * Whether the conditions contain any condition using the reusable field. + */ +function _block_content_has_reusable_condition(array $condition, array $tables) { + // If this is a condition group call this function recursively for each nested + // condition until a condition is found that return TRUE. + if (isset($condition['#conjunction'])) { + foreach (array_filter($condition, 'is_array') as $nested_condition) { + if (_block_content_has_reusable_condition($nested_condition, $tables)) { + return TRUE; + } + } + return FALSE; + } + if (isset($condition['field'])) { + $field = $condition['field']; + if (is_object($field) && $field instanceof ConditionInterface) { + return _block_content_has_reusable_condition($field->conditions(), $tables); + } + $field_parts = explode('.', $field); + $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable(); + foreach ($tables as $table) { + if ($table['table'] === $data_table && $field_parts[0] === $table['alias'] && $field_parts[1] === 'reusable') { + return TRUE; + } + } + + } + return FALSE; +}