Unverified Commit 329a3d9d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2955442 by amateescu, plach, jibran, Berdir: Add a way to get all the...

Issue #2955442 by amateescu, plach, jibran, Berdir: Add a way to get all the tables in which a field is stored from TableMappingInterface
parent 63c85aca
Loading
Loading
Loading
Loading
+9 −0
Changes for core/lib/Drupal/Core/Entity/Sql/DefaultTableMapping.php: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -394,6 +394,15 @@ public function getFieldTableName($field_name) {
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllFieldTableNames($field_name) {
    return array_keys(array_filter($this->fieldNames, function ($table_fields) use ($field_name) {
      return in_array($field_name, $table_fields, TRUE);
    }));
  }

  /**
   * {@inheritdoc}
   */
+22 −0
Changes for core/lib/Drupal/Core/Entity/Sql/TableMappingInterface.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -120,4 +120,26 @@ public function getFieldColumnName(FieldStorageDefinitionInterface $storage_defi
   */
  public function getFieldTableName($field_name);

  /**
   * Gets all the table names in which an entity field is stored.
   *
   * The returned table names are ordered by the amount of data stored in each
   * table. For example, a revisionable and translatable entity type which uses
   * core's default table mapping strategy would return the table names for the
   * entity ID field in the following order:
   * - base table
   * - data table
   * - revision table
   * - revision data table
   *
   * @param string $field_name
   *   The name of the entity field to return the tables names for.
   *
   * @return string[]
   *   An array of table names in which the given field is stored.
   *
   * @throws \Drupal\Core\Entity\Sql\SqlContentEntityStorageException
   */
  public function getAllFieldTableNames($field_name);

}
+37 −0
Changes for core/tests/Drupal/KernelTests/Core/Entity/DefaultTableMappingIntegrationTest.php: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -77,6 +77,43 @@ public function testGetFieldTableName() {
    $this->assertEquals($this->tableMapping->getFieldTableName('multivalued_base_field'), $expected);
  }

  /**
   * @covers ::getAllFieldTableNames
   */
  public function testGetAllFieldTableNames() {
    // Check a field that is stored in all the shared tables.
    $expected = [
      'entity_test_mulrev',
      'entity_test_mulrev_property_data',
      'entity_test_mulrev_revision',
      'entity_test_mulrev_property_revision',
    ];
    $this->assertEquals($expected, $this->tableMapping->getAllFieldTableNames('id'));

    // Check a field that is stored only in the base table.
    $expected = ['entity_test_mulrev'];
    $this->assertEquals($expected, $this->tableMapping->getAllFieldTableNames('uuid'));

    // Check a field that is stored only in the revision table.
    $expected = ['entity_test_mulrev_revision'];
    $this->assertEquals($expected, $this->tableMapping->getAllFieldTableNames('revision_default'));

    // Check a field that field that is stored in the data and revision data
    // tables.
    $expected = [
      'entity_test_mulrev_property_data',
      'entity_test_mulrev_property_revision',
    ];
    $this->assertEquals($expected, $this->tableMapping->getAllFieldTableNames('name'));

    // Check a field that is stored in dedicated data and revision data tables.
    $expected = [
      'entity_test_mulrev__multivalued_base_field',
      'entity_test_mulrev_r__f86e511394',
    ];
    $this->assertEquals($expected, $this->tableMapping->getAllFieldTableNames('multivalued_base_field'));
  }

  /**
   * Tests DefaultTableMapping::getTableNames().
   *