TokenEntityMapper.php in Token 8
File
src/TokenEntityMapper.php
View source
<?php
namespace Drupal\token;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class TokenEntityMapper implements TokenEntityMapperInterface {
protected $entityTypeManager;
protected $moduleHandler;
protected $entityMappings;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
}
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;
}
$this->moduleHandler
->alter('token_entity_mapping', $this->entityMappings);
}
return $this->entityMappings;
}
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);
}
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);
}
public function resetInfo() {
$this->entityMappings = NULL;
}
}