You are here

protected function ContentEntityNormalizer::verifyNormalizedLanguage in Default Content for D8 2.0.x

Verifies that the site knows the default language of the normalized entity.

Will attempt to switch to an alternative translation or just import it with the site default language.

Parameters

array $data: The normalized entity data.

Return value

array The normalized entity data, possibly with altered default language and translations.

1 call to ContentEntityNormalizer::verifyNormalizedLanguage()
ContentEntityNormalizer::denormalize in src/Normalizer/ContentEntityNormalizer.php
Converts the normalized data back into a content entity.

File

src/Normalizer/ContentEntityNormalizer.php, line 492

Class

ContentEntityNormalizer
Normalizes and denormalizes content entities.

Namespace

Drupal\default_content\Normalizer

Code

protected function verifyNormalizedLanguage(array $data) {

  // Check the language. If the default language isn't known, import as one
  // of the available translations if one exists with those values. If none
  // exists, create the entity in the default language.
  // During the installer, when installing with an alternative language,
  // EN is still when modules are installed so check the default language
  // instead.
  if (!$this->languageManager
    ->getLanguage($data['_meta']['default_langcode']) || InstallerKernel::installationAttempted() && $this->languageManager
    ->getDefaultLanguage()
    ->getId() != $data['_meta']['default_langcode']) {
    $use_default = TRUE;
    if (isset($data['translations'])) {
      foreach ($data['translations'] as $langcode => $translation_data) {
        if ($this->languageManager
          ->getLanguage($langcode)) {
          $data['_meta']['default_langcode'] = $langcode;
          $data['default'] = \array_merge($data['default'], $translation_data);
          unset($data['translations'][$langcode]);
          $use_default = FALSE;
          break;
        }
      }
    }
    if ($use_default) {
      $data['_meta']['default_langcode'] = $this->languageManager
        ->getDefaultLanguage()
        ->getId();
    }
  }
  return $data;
}