You are here

class FlexiformTreeBuilder in Flexiform 8

Hierarchy

Expanded class hierarchy of FlexiformTreeBuilder

1 string reference to 'FlexiformTreeBuilder'
flexiform.services.yml in ./flexiform.services.yml
flexiform.services.yml
1 service uses FlexiformTreeBuilder
flexiform.token.tree_builder in ./flexiform.services.yml
Drupal\flexiform\FlexiformTreeBuilder

File

src/FlexiformTreeBuilder.php, line 10

Namespace

Drupal\flexiform
View source
class FlexiformTreeBuilder extends TreeBuilder {

  /**
   * {@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 {
        if (empty($options['parents'])) {
          $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlexiformTreeBuilder::buildTree public function Build a tree array of tokens used for themeing or information. Overrides TreeBuilder::buildTree
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TreeBuilder::$builtTrees protected property Cache already built trees.
TreeBuilder::$cacheBackend protected property
TreeBuilder::$entityMapper protected property
TreeBuilder::$languageManager protected property
TreeBuilder::$tokenService protected property
TreeBuilder::buildAllRenderable public function Build a render array with token tree containing all possible tokens. Overrides TreeBuilderInterface::buildAllRenderable
TreeBuilder::buildRenderable public function Build a render array with token tree built as per specified options. Overrides TreeBuilderInterface::buildRenderable
TreeBuilder::flattenTree public function Flatten a token tree. Overrides TreeBuilderInterface::flattenTree
TreeBuilder::getTokenData protected function Generate a token tree.
TreeBuilder::__construct public function
TreeBuilderInterface::MAX_DEPTH constant The maximum depth for token tree recursion.