You are here

class TokenEntityMapper in Token 8

Service to provide mappings between entity and token types.

Why do we need this? Because when the token API was moved to core we did not reuse the entity type as the base name for taxonomy terms and vocabulary tokens.

Hierarchy

Expanded class hierarchy of TokenEntityMapper

1 string reference to 'TokenEntityMapper'
token.services.yml in ./token.services.yml
token.services.yml
1 service uses TokenEntityMapper
token.entity_mapper in ./token.services.yml
Drupal\token\TokenEntityMapper

File

src/TokenEntityMapper.php, line 15

Namespace

Drupal\token
View source
class TokenEntityMapper implements TokenEntityMapperInterface {

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * @var array
   */
  protected $entityMappings;
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityTypeMappings() {
    if (empty($this->entityMappings)) {
      foreach ($this->entityTypeManager
        ->getDefinitions() as $entity_type => $info) {
        $this->entityMappings[$entity_type] = $info
          ->get('token_type') ?: $entity_type;
      }

      // Allow modules to alter the mapping array.
      $this->moduleHandler
        ->alter('token_entity_mapping', $this->entityMappings);
    }
    return $this->entityMappings;
  }

  /**
   * {@inheritdoc}
   */
  function getEntityTypeForTokenType($token_type, $fallback = FALSE) {
    if (empty($this->entityMappings)) {
      $this
        ->getEntityTypeMappings();
    }
    $return = array_search($token_type, $this->entityMappings);
    return $return !== FALSE ? $return : ($fallback ? $token_type : FALSE);
  }

  /**
   * {@inheritdoc}
   */
  function getTokenTypeForEntityType($entity_type, $fallback = FALSE) {
    if (empty($this->entityMappings)) {
      $this
        ->getEntityTypeMappings();
    }
    return isset($this->entityMappings[$entity_type]) ? $this->entityMappings[$entity_type] : ($fallback ? $entity_type : FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public function resetInfo() {
    $this->entityMappings = NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TokenEntityMapper::$entityMappings protected property
TokenEntityMapper::$entityTypeManager protected property
TokenEntityMapper::$moduleHandler protected property
TokenEntityMapper::getEntityTypeForTokenType function Return the entity type of a particular token type. Overrides TokenEntityMapperInterface::getEntityTypeForTokenType
TokenEntityMapper::getEntityTypeMappings public function Return an array of entity type to token type mappings. Overrides TokenEntityMapperInterface::getEntityTypeMappings
TokenEntityMapper::getTokenTypeForEntityType function Return the token type of a particular entity type. Overrides TokenEntityMapperInterface::getTokenTypeForEntityType
TokenEntityMapper::resetInfo public function Resets metadata describing token and entity mappings. Overrides TokenEntityMapperInterface::resetInfo
TokenEntityMapper::__construct public function