Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / block_content.module
index 3adc979d9f08feb3b33d56f85ac87bbd5a46c5fb..98f0925ab96a9643dc3d9dfafc1a90de1e44d4b7 100644 (file)
@@ -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;
+}