You are here

protected function JsonapiHelper::getEntityLanguageLabel in Entity Share 8.2

Same name and namespace in other branches
  1. 8 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper::getEntityLanguageLabel()

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 JsonapiHelper::getEntityLanguageLabel()
JsonapiHelper::addOptionFromJson in modules/entity_share_client/src/Service/JsonapiHelper.php
Helper function to add an option.

File

modules/entity_share_client/src/Service/JsonapiHelper.php, line 778

Class

JsonapiHelper
Class JsonapiHelper.

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;
}