You are here

public function MenuTokenContextManager::prepareContextualLinks in Menu Token 9.1.x

Same name and namespace in other branches
  1. 8 src/Service/MenuTokenContextManager.php \Drupal\menu_token\Service\MenuTokenContextManager::prepareContextualLinks()

File

src/Service/MenuTokenContextManager.php, line 61

Class

MenuTokenContextManager
Menu token context manager service.

Namespace

Drupal\menu_token\Service

Code

public function prepareContextualLinks($relevantLink, $config) {
  $this->contextualReplacementLinks = unserialize($this->state
    ->get('menu_token_links_contextual_replacements'));
  $text_tokens = $this->tokenService
    ->scan($relevantLink["url"]);
  $text_tokens = array_merge($text_tokens, $this->tokenService
    ->scan($relevantLink["title"]));
  $use_in_context = FALSE;
  foreach ($text_tokens as $token_type => $tokens) {
    $entity_type = $this->tokenEntityMapper
      ->getEntityTypeForTokenType($token_type);
    if (empty($config[$entity_type][0]) || $config[$entity_type][0] === "context") {
      $use_in_context = TRUE;
    }
    if ($entity_type === FALSE) {
      $use_in_context = TRUE;
    }
  }
  if ($use_in_context) {
    $this->contextualReplacementLinks[$relevantLink['id']] = [
      "link" => $relevantLink,
      "config" => $config,
    ];
  }
  else {
    unset($this->contextualReplacementLinks[$relevantLink['id']]);
  }
  $this->state
    ->set('menu_token_links_contextual_replacements', serialize($this->contextualReplacementLinks));
}