3 namespace Drupal\token;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
9 * Service to provide mappings between entity and token types.
11 * Why do we need this? Because when the token API was moved to core we did not
12 * reuse the entity type as the base name for taxonomy terms and vocabulary
15 class TokenEntityMapper implements TokenEntityMapperInterface {
18 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
20 protected $entityTypeManager;
23 * @var \Drupal\Core\Extension\ModuleHandlerInterface
25 protected $moduleHandler;
30 protected $entityMappings;
32 public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
33 $this->entityTypeManager = $entity_type_manager;
34 $this->moduleHandler = $module_handler;
40 public function getEntityTypeMappings() {
41 if (empty($this->entityMappings)) {
42 foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $info) {
43 $this->entityMappings[$entity_type] = $info->get('token_type') ?: $entity_type;
45 // Allow modules to alter the mapping array.
46 $this->moduleHandler->alter('token_entity_mapping', $this->entityMappings);
49 return $this->entityMappings;
55 function getEntityTypeForTokenType($token_type, $fallback = FALSE) {
56 if (empty($this->entityMappings)) {
57 $this->getEntityTypeMappings();
60 $return = array_search($token_type, $this->entityMappings);
61 return $return !== FALSE ? $return : ($fallback ? $token_type : FALSE);
67 function getTokenTypeForEntityType($entity_type, $fallback = FALSE) {
68 if (empty($this->entityMappings)) {
69 $this->getEntityTypeMappings();
72 return isset($this->entityMappings[$entity_type]) ? $this->entityMappings[$entity_type] : ($fallback ? $entity_type : FALSE);
78 public function resetInfo() {
79 $this->entityMappings = NULL;