Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Query / Sql / Tables.php
index e21824732736856649b9bd6d2e2c7651e15c2876..1f99d0f7716b74c734e6ffb99d7fa750964fee5a 100644 (file)
@@ -8,6 +8,7 @@ use Drupal\Core\Entity\Query\QueryException;
 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
 use Drupal\Core\Entity\Sql\TableMappingInterface;
 use Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface;
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
 use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
 
 /**
@@ -21,10 +22,13 @@ class Tables implements TablesInterface {
   protected $sqlQuery;
 
   /**
-   * Entity table array, key is table name, value is alias.
+   * Entity table array.
    *
    * This array contains at most two entries: one for the data, one for the
-   * properties.
+   * properties. Its keys are unique references to the tables, values are
+   * aliases.
+   *
+   * @see \Drupal\Core\Entity\Query\Sql\Tables::ensureEntityTable().
    *
    * @var array
    */
@@ -90,9 +94,11 @@ class Tables implements TablesInterface {
       $specifier = $specifiers[$key];
       if (isset($field_storage_definitions[$specifier])) {
         $field_storage = $field_storage_definitions[$specifier];
+        $column = $field_storage->getMainPropertyName();
       }
       else {
         $field_storage = FALSE;
+        $column = NULL;
       }
 
       // If there is revision support, only the current revisions are being
@@ -117,8 +123,6 @@ class Tables implements TablesInterface {
       // Check whether this field is stored in a dedicated table.
       if ($field_storage && $table_mapping->requiresDedicatedTableStorage($field_storage)) {
         $delta = NULL;
-        // Find the field column.
-        $column = $field_storage->getMainPropertyName();
 
         if ($key < $count) {
           $next = $specifiers[$key + 1];
@@ -172,10 +176,6 @@ class Tables implements TablesInterface {
         }
         $table = $this->ensureFieldTable($index_prefix, $field_storage, $type, $langcode, $base_table, $entity_id_field, $field_id_field, $delta);
         $sql_column = $table_mapping->getFieldColumnName($field_storage, $column);
-        $property_definitions = $field_storage->getPropertyDefinitions();
-        if (isset($property_definitions[$column])) {
-          $this->caseSensitiveFields[$field] = $property_definitions[$column]->getSetting('case_sensitive');
-        }
       }
       // The field is stored in a shared table.
       else {
@@ -184,6 +184,7 @@ class Tables implements TablesInterface {
         // finds the property first. The data table is preferred, which is why
         // it gets added before the base table.
         $entity_tables = [];
+        $revision_table = NULL;
         if ($all_revisions && $field_storage && $field_storage->isRevisionable()) {
           $data_table = $entity_type->getRevisionDataTable();
           $entity_base_table = $entity_type->getRevisionTable();
@@ -191,11 +192,18 @@ class Tables implements TablesInterface {
         else {
           $data_table = $entity_type->getDataTable();
           $entity_base_table = $entity_type->getBaseTable();
+
+          if ($field_storage && $field_storage->isRevisionable() && in_array($field_storage->getName(), $entity_type->getRevisionMetadataKeys())) {
+            $revision_table = $entity_type->getRevisionTable();
+          }
         }
         if ($data_table) {
           $this->sqlQuery->addMetaData('simple_query', FALSE);
           $entity_tables[$data_table] = $this->getTableMapping($data_table, $entity_type_id);
         }
+        if ($revision_table) {
+          $entity_tables[$revision_table] = $this->getTableMapping($revision_table, $entity_type_id);
+        }
         $entity_tables[$entity_base_table] = $this->getTableMapping($entity_base_table, $entity_type_id);
         $sql_column = $specifier;
 
@@ -235,18 +243,17 @@ class Tables implements TablesInterface {
         }
 
         $table = $this->ensureEntityTable($index_prefix, $sql_column, $type, $langcode, $base_table, $entity_id_field, $entity_tables);
+      }
 
-        // If there is a field storage (some specifiers are not), check for case
-        // sensitivity.
-        if ($field_storage) {
-          $column = $field_storage->getMainPropertyName();
-          $base_field_property_definitions = $field_storage->getPropertyDefinitions();
-          if (isset($base_field_property_definitions[$column])) {
-            $this->caseSensitiveFields[$field] = $base_field_property_definitions[$column]->getSetting('case_sensitive');
-          }
+      // If there is a field storage (some specifiers are not) and a field
+      // column, check for case sensitivity.
+      if ($field_storage && $column) {
+        $property_definitions = $field_storage->getPropertyDefinitions();
+        if (isset($property_definitions[$column])) {
+          $this->caseSensitiveFields[$field] = $property_definitions[$column]->getSetting('case_sensitive');
         }
-
       }
+
       // If there are more specifiers to come, it's a relationship.
       if ($field_storage && $key < $count) {
         // Computed fields have prepared their property definition already, do
@@ -273,7 +280,7 @@ class Tables implements TablesInterface {
           $entity_type = $this->entityManager->getDefinition($entity_type_id);
           $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
           // Add the new entity base table using the table and sql column.
-          $base_table = $this->addNextBaseTable($entity_type, $table, $sql_column);
+          $base_table = $this->addNextBaseTable($entity_type, $table, $sql_column, $field_storage);
           $propertyDefinitions = [];
           $key++;
           $index_prefix .= "$next_index_prefix.";
@@ -296,21 +303,49 @@ class Tables implements TablesInterface {
   }
 
   /**
-   * Join entity table if necessary and return the alias for it.
+   * Joins the entity table, if necessary, and returns the alias for it.
    *
+   * @param string $index_prefix
+   *   The table array index prefix. For a base table this will be empty,
+   *   for a target entity reference like 'field_tags.entity:taxonomy_term.name'
+   *   this will be 'entity:taxonomy_term.target_id.'.
    * @param string $property
+   *   The field property/column.
+   * @param string $type
+   *   The join type, can either be INNER or LEFT.
+   * @param string $langcode
+   *   The langcode we use on the join.
+   * @param string $base_table
+   *   The table to join to. It can be either the table name, its alias or the
+   *   'base_table' placeholder.
+   * @param string $id_field
+   *   The name of the ID field/property for the current entity. For instance:
+   *   tid, nid, etc.
+   * @param array $entity_tables
+   *   Array of entity tables (data and base tables) where decide the entity
+   *   property will be queried from. The first table containing the property
+   *   will be used, so the order is important and the data table is always
+   *   preferred.
    *
    * @return string
+   *   The alias of the joined table.
    *
    * @throws \Drupal\Core\Entity\Query\QueryException
+   *   When an invalid property has been passed.
    */
   protected function ensureEntityTable($index_prefix, $property, $type, $langcode, $base_table, $id_field, $entity_tables) {
     foreach ($entity_tables as $table => $mapping) {
       if (isset($mapping[$property])) {
-        if (!isset($this->entityTables[$index_prefix . $table])) {
-          $this->entityTables[$index_prefix . $table] = $this->addJoin($type, $table, "%alias.$id_field = $base_table.$id_field", $langcode);
+        // Ensure a table joined multiple times through different index prefixes
+        // has unique entityTables entries by concatenating the index prefix
+        // and the base table alias. In this way i.e. if we join to the same
+        // entity table several times for different entity reference fields,
+        // each join gets a separate alias.
+        $key = $index_prefix . ($base_table === 'base_table' ? $table : $base_table);
+        if (!isset($this->entityTables[$key])) {
+          $this->entityTables[$key] = $this->addJoin($type, $table, "%alias.$id_field = $base_table.$id_field", $langcode);
         }
-        return $this->entityTables[$index_prefix . $table];
+        return $this->entityTables[$key];
       }
     }
     throw new QueryException("'$property' not found");
@@ -339,6 +374,23 @@ class Tables implements TablesInterface {
     return $this->fieldTables[$index_prefix . $field_name];
   }
 
+  /**
+   * Adds a join to a given table.
+   *
+   * @param string $type
+   *   The join type.
+   * @param string $table
+   *   The table to join to.
+   * @param string $join_condition
+   *   The condition on which to join to.
+   * @param string $langcode
+   *   The langcode we use on the join.
+   * @param string|null $delta
+   *   (optional) A delta which should be used as additional condition.
+   *
+   * @return string
+   *   Returns the alias of the joined table.
+   */
   protected function addJoin($type, $table, $join_condition, $langcode, $delta = NULL) {
     $arguments = [];
     if ($langcode) {
@@ -365,8 +417,10 @@ class Tables implements TablesInterface {
    * @param string $table
    *   The table name.
    *
-   * @return array|bool
-   *   The table field mapping for the given table or FALSE if not available.
+   * @return array|false
+   *   An associative array of table field mapping for the given table, keyed by
+   *   columns name and values are just incrementing integers. If the table
+   *   mapping is not available, FALSE is returned.
    */
   protected function getTableMapping($table, $entity_type_id) {
     $storage = $this->entityManager->getStorage($entity_type_id);
@@ -395,11 +449,13 @@ class Tables implements TablesInterface {
    *   This is the table being joined, in the above example, {users}.
    * @param string $sql_column
    *   This is the SQL column in the existing table being joined to.
+   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage
+   *   The field storage definition for the field referencing this column.
    *
    * @return string
    *   The alias of the next entity table joined in.
    */
-  protected function addNextBaseTable(EntityType $entity_type, $table, $sql_column) {
+  protected function addNextBaseTable(EntityType $entity_type, $table, $sql_column, FieldStorageDefinitionInterface $field_storage) {
     $join_condition = '%alias.' . $entity_type->getKey('id') . " = $table.$sql_column";
     return $this->sqlQuery->leftJoin($entity_type->getBaseTable(), NULL, $join_condition);
   }