You are here

public function EntityEmbedFilterTranslationTest::providerTranslationSituations in Entity Embed 8

Data provider for testTranslationSelection().

File

tests/src/Kernel/EntityEmbedFilterTranslationTest.php, line 72

Class

EntityEmbedFilterTranslationTest
Tests that entity embeds are translated based on host and `data-langcode`.

Namespace

Drupal\Tests\entity_embed\Kernel

Code

public function providerTranslationSituations() {
  $embedded_entity_translation_languages = [
    'en',
    'pt-br',
  ];
  foreach ([
    'en',
    'pt-br',
    'nl',
  ] as $text_langcode) {

    // When no `data-langcode` attribute is specified, the text language
    // (which is set to the host entity's language) is respected. If that
    // translation does not exist, it falls back to the default translation of
    // the embedded entity.
    $match_or_fallback_langcode = in_array($text_langcode, $embedded_entity_translation_languages) ? $text_langcode : 'en';
    (yield "text_langcode={$text_langcode} (✅) ⇒ {$match_or_fallback_langcode}" => [
      $text_langcode,
      [],
      $match_or_fallback_langcode,
    ]);

    // When the embedded entity has a translation for the language code in the
    // `data-langcode` attribute, that translation is used, regardless of the
    // language of the text (which is set to the language of the host entity).
    foreach ($embedded_entity_translation_languages as $data_langcode) {
      (yield "text_langcode={$text_langcode} (✅); data-langcode={$data_langcode} (✅) ⇒ {$data_langcode}" => [
        $text_langcode,
        [
          'data-langcode' => $data_langcode,
        ],
        $data_langcode,
      ]);
    }

    // When specifying a (valid) language code but the embedded entity has no
    // translation for that language, it falls back to the default translation
    // of the embedded entity.
    (yield "text_langcode={$text_langcode} (✅); data-langcode=nl (🚫) ⇒ en" => [
      $text_langcode,
      [
        'data-langcode' => 'nl',
      ],
      'en',
    ]);

    // When specifying a invalid language code, it falls back to the default
    // translation of the embedded entity.
    (yield "text_langcode={$text_langcode} (✅); data-langcode=non-existing-and-even-invalid-langcode (🚫) ⇒ en" => [
      $text_langcode,
      [
        'data-langcode' => 'non-existing-and-even-invalid-langcode',
      ],
      'en',
    ]);
  }
}