You are here

protected function FormHelper::getEntityLanguageLabel in Entity Share 8.3

Helper function to get the language from an extracted entity.

We can't use $entity->language() because if the entity is in a language not enabled, it is the site default language that is returned.

Parameters

array $data: The data from the JSON:API payload.

array $entity_keys: The entity keys from the entity definition.

Return value

string The language of the entity.

1 call to FormHelper::getEntityLanguageLabel()
FormHelper::addOptionFromJson in modules/entity_share_client/src/Service/FormHelper.php
Helper function to add an option.

File

modules/entity_share_client/src/Service/FormHelper.php, line 323

Class

FormHelper
Service to extract code out of the PullForm.

Namespace

Drupal\entity_share_client\Service

Code

protected function getEntityLanguageLabel(array $data, array $entity_keys) {
  if (!isset($entity_keys['langcode']) || empty($entity_keys['langcode'])) {
    return $this
      ->t('Untranslatable entity');
  }
  $parsed_type = explode('--', $data['type']);
  $resource_type = $this->resourceTypeRepository
    ->get($parsed_type[0], $parsed_type[1]);
  $langcode = $data['attributes'][$resource_type
    ->getPublicName($entity_keys['langcode'])];
  $language = $this->languageManager
    ->getLanguage($langcode);

  // Check if the entity is in an enabled language.
  if (is_null($language)) {
    $language_list = LanguageManager::getStandardLanguageList();
    if (isset($language_list[$langcode])) {
      $entity_language = $language_list[$langcode][0] . ' ' . $this
        ->t('(not enabled)', [], [
        'context' => 'language',
      ]);
    }
    else {
      $entity_language = $this
        ->t('Entity in an unsupported language.');
    }
  }
  else {
    $entity_language = $language
      ->getName();
  }
  return $entity_language;
}