You are here

protected function JsonapiHelper::getEntityLanguageLabel in Entity Share 8

Same name and namespace in other branches
  1. 8.2 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

\Drupal\Core\Entity\ContentEntityInterface $entity: An unserialized entity.

Return value

string The language of the entity.

1 call to JsonapiHelper::getEntityLanguageLabel()
JsonapiHelper::addOption 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 573

Class

JsonapiHelper
Class JsonapiHelper.

Namespace

Drupal\entity_share_client\Service

Code

protected function getEntityLanguageLabel(ContentEntityInterface $entity) {
  $langcode = $entity
    ->get('langcode')->value;
  $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;
}