/** * Replace tokens with their values using the core token service. * * @param $string * @param $data * @param array $settings * @return mixed|string */ private function coreReplace($string, $data, $settings = array()) { // @TODO: Remove this temp code. // This is just here as a way to see all available tokens in debugger. $tokens = $this->coreToken->getInfo(); $options = array('clear' => TRUE); // Replace tokens with core Token service. $replaced = $this->coreToken->replace($string, $data, $options); // Ensure that there are no double-slash sequences due to empty token values. $replaced = preg_replace('/(?<!:)\\/+\\//', '/', $replaced); return $replaced; }
/** * {@inheritdoc} */ public function getInfo() { if (empty($this->tokenInfo)) { $token_info = parent::getInfo(); foreach (array_keys($token_info['types']) as $type_key) { if (isset($token_info['types'][$type_key]['type'])) { $base_type = $token_info['types'][$type_key]['type']; // If this token type extends another token type, then merge in // the base token type's tokens. if (isset($token_info['tokens'][$base_type])) { $token_info['tokens'] += [$type_key => []]; $token_info['tokens'][$type_key] += $token_info['tokens'][$base_type]; } } else { // Add a 'type' value to each token type information. $token_info['types'][$type_key]['type'] = $type_key; } } // Pre-sort tokens. uasort($token_info['types'], [$this, 'sortTokens']); foreach (array_keys($token_info['tokens']) as $type) { uasort($token_info['tokens'][$type], [$this, 'sortTokens']); } $this->tokenInfo = $token_info; } return $this->tokenInfo; }
/** * @covers ::getInfo */ public function testGetInfo() { $token_info = array('types' => array('foo' => array('name' => $this->randomMachineName()))); $this->language->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($this->randomMachineName())); $this->languageManager->expects($this->once())->method('getCurrentLanguage')->with(LanguageInterface::TYPE_CONTENT)->will($this->returnValue($this->language)); // The persistent cache must only be hit once, after which the info is // cached statically. $this->cache->expects($this->once())->method('get'); $this->cache->expects($this->once())->method('set')->with('token_info:' . $this->language->getId(), $token_info); $this->moduleHandler->expects($this->once())->method('invokeAll')->with('token_info')->will($this->returnValue($token_info)); $this->moduleHandler->expects($this->once())->method('alter')->with('token_info', $token_info); // Get the information for the first time. The cache should be checked, the // hooks invoked, and the info should be set to the cache should. $this->token->getInfo(); // Get the information for the second time. The data must be returned from // the static cache, so the persistent cache must not be accessed and the // hooks must not be invoked. $this->token->getInfo(); }