示例#1
0
 /**
  * Render the token tree for the specified entity.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity for which the token tree should be rendered.
  *
  * @return array
  *   Render array of the token tree for the $entity.
  *
  * @see static::entityLoad
  */
 protected function renderTokenTree(EntityInterface $entity)
 {
     $this->moduleHandler()->loadInclude('token', 'pages.inc');
     $entity_type = $entity->getEntityTypeId();
     $token_type = $this->entityMapper->getTokenTypeForEntityType($entity_type);
     $options = ['flat' => TRUE, 'values' => TRUE, 'data' => [$token_type => $entity]];
     $token_tree = [$token_type => ['tokens' => $this->treeBuilder->buildTree($token_type, $options)]];
     //    foreach ($tree as $token => $token_info) {
     //      if (!isset($token_info['value']) && !empty($token_info['parent']) && !isset($tree[$token_info['parent']]['value'])) {
     //        continue;
     //      }
     //    }
     $build['tokens'] = ['#type' => 'token_tree_table', '#show_restricted' => FALSE, '#skip_empty_values' => TRUE, '#token_tree' => $token_tree, '#columns' => ['token', 'value'], '#empty' => $this->t('No tokens available.')];
     return $build;
 }
 /**
  * Loads pathauto patterns for a given entity type ID
  *
  * @param string $entity_type_id
  *   An entity type ID.
  *
  * @return \Drupal\pathauto\PathautoPatternInterface[]
  *   A list of patterns, sorted by weight.
  */
 protected function getPatternByEntityType($entity_type_id)
 {
     if (!isset($this->patternsByEntityType[$entity_type_id])) {
         $ids = \Drupal::entityQuery('pathauto_pattern')->condition('type', array_keys(\Drupal::service('plugin.manager.alias_type')->getPluginDefinitionByType($this->tokenEntityMapper->getTokenTypeForEntityType($entity_type_id))))->condition('status', 1)->sort('weight')->execute();
         $this->patternsByEntityType[$entity_type_id] = \Drupal::entityTypeManager()->getStorage('pathauto_pattern')->loadMultiple($ids);
     }
     return $this->patternsByEntityType[$entity_type_id];
 }
 /**
  * {@inheritdoc}
  */
 public function getDerivativeDefinitions($base_plugin_definition)
 {
     foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
         // An entity type must have a canonical link template and support fields.
         if ($entity_type->hasLinkTemplate('canonical') && is_subclass_of($entity_type->getClass(), FieldableEntityInterface::class)) {
             $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
             if (!isset($base_fields['path'])) {
                 // The entity type does not have a path field and is therefore not
                 // supported.
                 continue;
             }
             $this->derivatives[$entity_type_id] = $base_plugin_definition;
             $this->derivatives[$entity_type_id]['label'] = $entity_type->getLabel();
             $this->derivatives[$entity_type_id]['types'] = [$this->tokenEntityMapper->getTokenTypeForEntityType($entity_type_id)];
             $this->derivatives[$entity_type_id]['provider'] = $entity_type->getProvider();
             $this->derivatives[$entity_type_id]['context'] = [$entity_type_id => new ContextDefinition("entity:{$entity_type_id}", $this->t('@label being aliased', ['@label' => $entity_type->getLabel()]))];
         }
     }
     return $this->derivatives;
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function buildTree($token_type, array $options = [])
 {
     $options += ['restricted' => FALSE, 'depth' => 4, 'data' => [], 'values' => FALSE, 'flat' => FALSE];
     // Do not allow past the maximum token information depth.
     $options['depth'] = min($options['depth'], static::MAX_DEPTH);
     // If $token_type is an entity, make sure we are using the actual token type.
     if ($entity_token_type = $this->entityMapper->getTokenTypeForEntityType($token_type)) {
         $token_type = $entity_token_type;
     }
     $langcode = $this->languageManager->getCurrentLanguage()->getId();
     $tree_cid = "token_tree:{$token_type}:{$langcode}:{$options['depth']}";
     // If we do not have this base tree in the static cache, check the cache
     // otherwise generate and store it in the cache.
     if (!isset($this->builtTrees[$tree_cid])) {
         if ($cache = $this->cacheBackend->get($tree_cid)) {
             $this->builtTrees[$tree_cid] = $cache->data;
         } else {
             $options['parents'] = [];
             $this->builtTrees[$tree_cid] = $this->getTokenData($token_type, $options);
             $this->cacheBackend->set($tree_cid, $this->builtTrees[$tree_cid], Cache::PERMANENT, [Token::TOKEN_INFO_CACHE_TAG]);
         }
     }
     $tree = $this->builtTrees[$tree_cid];
     // If the user has requested a flat tree, convert it.
     if (!empty($options['flat'])) {
         $tree = $this->flattenTree($tree);
     }
     // Fill in token values.
     if (!empty($options['values'])) {
         $token_values = [];
         foreach ($tree as $token => $token_info) {
             if (!empty($token_info['dynamic']) || !empty($token_info['restricted'])) {
                 continue;
             } elseif (!isset($token_info['value'])) {
                 $token_values[$token_info['token']] = $token;
             }
         }
         if (!empty($token_values)) {
             $token_values = $this->tokenService->generate($token_type, $token_values, $options['data'], [], new BubbleableMetadata());
             foreach ($token_values as $token => $replacement) {
                 $tree[$token]['value'] = $replacement;
             }
         }
     }
     return $tree;
 }