function FieldTranslationSqlStorageTest::assertFieldStorageLangcode

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldTranslationSqlStorageTest::assertFieldStorageLangcode()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldTranslationSqlStorageTest::assertFieldStorageLangcode()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php \Drupal\KernelTests\Core\Entity\FieldTranslationSqlStorageTest::assertFieldStorageLangcode()

Checks whether field languages are correctly stored for the given entity.

@internal

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity fields are attached to.

string $message: (optional) A message to display with the assertion.

1 call to FieldTranslationSqlStorageTest::assertFieldStorageLangcode()
FieldTranslationSqlStorageTest::testFieldSqlStorage in core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php
Tests field SQL storage.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldTranslationSqlStorageTest.php, line 78

Class

FieldTranslationSqlStorageTest
Tests Field translation SQL Storage.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function assertFieldStorageLangcode(FieldableEntityInterface $entity, string $message = '') : void {
    $status = TRUE;
    $entity_type = $entity->getEntityTypeId();
    $id = $entity->id();
    $langcode = $entity->getUntranslated()
        ->language()
        ->getId();
    $fields = [
        $this->fieldName,
        $this->untranslatableFieldName,
    ];
    
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = \Drupal::entityTypeManager()->getStorage($entity_type)
        ->getTableMapping();
    foreach ($fields as $field_name) {
        $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
        $table = $table_mapping->getDedicatedDataTableName($field_storage);
        $record = \Drupal::database()->select($table, 'f')
            ->fields('f')
            ->condition('f.entity_id', $id)
            ->condition('f.revision_id', $id)
            ->execute()
            ->fetchObject();
        if ($record->langcode != $langcode) {
            $status = FALSE;
            break;
        }
    }
    $this->assertTrue($status, $message);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.