X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fblock_content%2Fblock_content.module;fp=web%2Fcore%2Fmodules%2Fblock_content%2Fblock_content.module;h=98f0925ab96a9643dc3d9dfafc1a90de1e44d4b7;hp=3adc979d9f08feb3b33d56f85ac87bbd5a46c5fb;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/block_content/block_content.module b/web/core/modules/block_content/block_content.module index 3adc979d9..98f0925ab 100644 --- a/web/core/modules/block_content/block_content.module +++ b/web/core/modules/block_content/block_content.module @@ -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 @@ function block_content_add_body_field($block_type_id, $label = 'Body') { 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; +}