diff --git a/src/Token.php b/src/Token.php index c488e2e..5b02f56 100644 --- a/src/Token.php +++ b/src/Token.php @@ -9,7 +9,15 @@ namespace Drupal\token; use Drupal\Core\Utility\Token as TokenBase; -class Token extends TokenBase { +/** + * Service to retrieve token information. + * + * This service replaces the core's token service and provides the same + * functionality by extending it. It also provides additional functionality + * commonly required by the additional support provided by token module and + * other modules. + */ +class Token extends TokenBase implements TokenInterface { /** * Token definitions. @@ -24,8 +32,8 @@ class Token extends TokenBase { /** * {@inheritdoc} */ - public function getInfo($token_type = NULL, $token = NULL) { - if (is_null($this->tokenInfo)) { + public function getInfo() { + if (empty($this->tokenInfo)) { $token_info = parent::getInfo(); foreach (array_keys($token_info['types']) as $type_key) { @@ -54,22 +62,33 @@ class Token extends TokenBase { $this->tokenInfo = $token_info; } - if (isset($token_type) && isset($token)) { - return isset($this->tokenInfo['tokens'][$token_type][$token]) ? $this->tokenInfo['tokens'][$token_type][$token] : NULL; - } - elseif (isset($token_type)) { - return isset($this->tokenInfo['types'][$token_type]) ? $this->tokenInfo['types'][$token_type] : NULL; + return $this->tokenInfo; + } + + /** + * {@inheritdoc} + */ + public function getTokenTypeInfo($token_type, $token) { + if (empty($this->tokenInfo)) { + $this->getInfo(); } - else { - return $this->tokenInfo; + + return isset($this->tokenInfo['tokens'][$token_type][$token]) ? $this->tokenInfo['tokens'][$token_type][$token] : NULL; + } + + /** + * {@inheritdoc} + */ + public function getTypeInfo($token_type) { + if (empty($this->tokenInfo)) { + $this->getInfo(); } + + return isset($this->tokenInfo['types'][$token_type]) ? $this->tokenInfo['types'][$token_type] : NULL; } /** - * Get a list of token types that can be used without any context (global). - * - * @return array[] - * An array of global token types. + * {@inheritdoc} */ public function getGlobalTokenTypes() { if (empty($this->globalTokenTypes)) { @@ -87,15 +106,7 @@ class Token extends TokenBase { } /** - * Validate an array of tokens based on their token type. - * - * @param string $type - * The type of tokens to validate (e.g. 'node', etc.) - * @param string[] $tokens - * A keyed array of tokens, and their original raw form in the source text. - * - * @return string[] - * An array with the invalid tokens in their original raw forms. + * {@inheritdoc} */ function getInvalidTokens($type, $tokens) { $token_info = $this->getInfo(); @@ -135,17 +146,7 @@ class Token extends TokenBase { } /** - * Validate tokens in raw text based on possible contexts. - * - * @param string|string[] $value - * A string with the raw text containing the raw tokens, or an array of - * tokens from token_scan(). - * @param string[] $valid_types - * An array of token types that will be used when token replacement is - * performed. - * - * @return string[] - * An array with the invalid tokens in their original raw forms. + * {@inheritdoc} */ public function getInvalidTokensByContext($value, array $valid_types = []) { if (in_array('all', $valid_types)) { diff --git a/src/TokenInterface.php b/src/TokenInterface.php index 1caadad..76cb61d 100644 --- a/src/TokenInterface.php +++ b/src/TokenInterface.php @@ -2,11 +2,79 @@ /** * @file + * Contains \Drupal\token\TokenInterface */ namespace Drupal\token; - interface TokenInterface { + /** + * Returns metadata describing supported token types. + * + * @param $token_type + * The token type for which the metadata is required. + * + * @return array[] + * An array of token type information from hook_token_info() for the + * specified token type. + * + * @see hook_token_info() + * @see hook_token_info_alter() + */ + public function getTypeInfo($token_type); + + /** + * Returns metadata describing supported a token. + * + * @param $token_type + * The token type for which the metadata is required. + * @param $token + * The token name for which the metadata is required. + * + * @return array[] + * An array of information from hook_token_info() for the specified token. + * + * @see hook_token_info() + * @see hook_token_info_alter() + * + * @deprecated + */ + public function getTokenTypeInfo($token_type, $token); + + /** + * Get a list of token types that can be used without any context (global). + * + * @return array[] + * An array of global token types. + */ + public function getGlobalTokenTypes(); + + /** + * Validate an array of tokens based on their token type. + * + * @param string $type + * The type of tokens to validate (e.g. 'node', etc.) + * @param string[] $tokens + * A keyed array of tokens, and their original raw form in the source text. + * + * @return string[] + * An array with the invalid tokens in their original raw forms. + */ + function getInvalidTokens($type, $tokens); + + /** + * Validate tokens in raw text based on possible contexts. + * + * @param string|string[] $value + * A string with the raw text containing the raw tokens, or an array of + * tokens from token_scan(). + * @param string[] $valid_types + * An array of token types that will be used when token replacement is + * performed. + * + * @return string[] + * An array with the invalid tokens in their original raw forms. + */ + public function getInvalidTokensByContext($value, array $valid_types = []); } diff --git a/src/TokenServiceProvider.php b/src/TokenServiceProvider.php index 7009e7e..85130c0 100644 --- a/src/TokenServiceProvider.php +++ b/src/TokenServiceProvider.php @@ -10,6 +10,9 @@ namespace Drupal\token; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceProviderBase; +/** + * Replace core's token service with our own. + */ class TokenServiceProvider extends ServiceProviderBase { /** diff --git a/token.module b/token.module index 27c38a6..8414eec 100644 --- a/token.module +++ b/token.module @@ -356,7 +356,15 @@ function token_module_implements_alter(&$implementations, $hook) { * @deprecated */ function token_get_info($token_type = NULL, $token = NULL) { - return \Drupal::token()->getInfo($token_type, $token); + if (isset($token_type) && isset($token)) { + return \Drupal::token()->getTokenInfo($token_type, $token); + } + elseif (isset($token_type)) { + return \Drupal::token()->getTypeInfo($token_type); + } + else { + return \Drupal::token()->getInfo(); + } } /**