You are here

trait TaxonomyStorages in Client-side Hierarchical Select 8.2

Same name and namespace in other branches
  1. 8.3 src/TaxonomyStorages.php \Drupal\cshs\TaxonomyStorages
  2. 8 src/TaxonomyStorages.php \Drupal\cshs\TaxonomyStorages

The taxonomy storages.

Hierarchy

1 file declares its use of TaxonomyStorages
CshsFormatterBase.php in src/Plugin/Field/FieldFormatter/CshsFormatterBase.php

File

src/TaxonomyStorages.php, line 14

Namespace

Drupal\cshs
View source
trait TaxonomyStorages {

  /**
   * An instance of the "entity.repository" service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * An instance of the "entity_type.manager" service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The state of whether content translation should be used.
   *
   * @var bool
   */
  private $needsTranslatedContent;

  /**
   * Returns storage of the "taxonomy_term" entities.
   *
   * @return \Drupal\taxonomy\TermStorageInterface
   *   The storage of the "taxonomy_term" entities.
   */
  protected function getTermStorage() : TermStorageInterface {
    return $this
      ->getStorage('taxonomy_term');
  }

  /**
   * Returns storage of the "taxonomy_vocabulary" entities.
   *
   * @return \Drupal\taxonomy\VocabularyStorageInterface
   *   The storage of the "taxonomy_vocabulary" entities.
   */
  protected function getVocabularyStorage() : VocabularyStorageInterface {
    return $this
      ->getStorage('taxonomy_vocabulary');
  }

  /**
   * Returns the entity translation to be used in the given context.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity whose translation will be returned.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The translated entity.
   */
  protected function getTranslationFromContext(EntityInterface $entity) : EntityInterface {
    if (NULL === $this->entityRepository) {
      $this->entityRepository = \Drupal::service('entity.repository');
    }
    return $this->entityRepository
      ->getTranslationFromContext($entity);
  }

  /**
   * Returns the state of whether content translation needed.
   *
   * @return bool
   *   The state.
   */
  protected function needsTranslatedContent() : bool {
    if (NULL === $this->needsTranslatedContent) {

      /* @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
      $language_manager = \Drupal::service('language_manager');
      $default_language = $language_manager
        ->getDefaultLanguage();
      $content_language = $language_manager
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
      $this->needsTranslatedContent = $default_language
        ->getId() !== $content_language
        ->getId();
    }
    return $this->needsTranslatedContent;
  }

  /**
   * Returns the entity storage.
   *
   * @param string $entity_type
   *   The entity type ID.
   *
   * @return \Drupal\Core\Entity\EntityStorageInterface
   *   The entity storage.
   */
  private function getStorage($entity_type) : EntityStorageInterface {
    if (NULL === $this->entityTypeManager) {
      $this->entityTypeManager = \Drupal::service('entity_type.manager');
    }
    return $this->entityTypeManager
      ->getStorage($entity_type);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TaxonomyStorages::$entityRepository protected property An instance of the "entity.repository" service.
TaxonomyStorages::$entityTypeManager protected property An instance of the "entity_type.manager" service.
TaxonomyStorages::$needsTranslatedContent private property The state of whether content translation should be used.
TaxonomyStorages::getStorage private function Returns the entity storage.
TaxonomyStorages::getTermStorage protected function Returns storage of the "taxonomy_term" entities. 1
TaxonomyStorages::getTranslationFromContext protected function Returns the entity translation to be used in the given context.
TaxonomyStorages::getVocabularyStorage protected function Returns storage of the "taxonomy_vocabulary" entities. 1
TaxonomyStorages::needsTranslatedContent protected function Returns the state of whether content translation needed.