diff --git a/token_example/src/Controller/TokemExampleController.php b/token_example/src/Controller/TokemExampleController.php deleted file mode 100644 index b4efbed..0000000 --- a/token_example/src/Controller/TokemExampleController.php +++ /dev/null @@ -1,8 +0,0 @@ - $token_service->replace("

This is sample text with simple token: [token_example:simple]

") + ); + $elements['with-type'] = array( + '#markup' => $token_service->replace("

This is sample text with token of date type. Current timestamp is: [token_example:with-type]

") + ); + $elements['dynamic'] = array( + '#markup' => $token_service->replace("

This is sample text with dynamic token with random number as suffix: [token_example:dynamic:bar]

") + ); + $elements['token_example_type'] = array( + '#markup' => $token_service->replace("

Token example data type: [token_example_type:bar]

") + ); + + + return $elements; + } + +} diff --git a/token_example/token_example.info.yml b/token_example/token_example.info.yml index 329ff86..6987c6a 100644 --- a/token_example/token_example.info.yml +++ b/token_example/token_example.info.yml @@ -2,7 +2,4 @@ name: 'Token example' type: module description: 'An example module showing how to define and use tokens.' package: 'Example modules' -version: 1.0 core: 8.x -dependencies: - - token \ No newline at end of file diff --git a/token_example/token_example.module b/token_example/token_example.module new file mode 100644 index 0000000..20698eb --- /dev/null +++ b/token_example/token_example.module @@ -0,0 +1,136 @@ +'; + $output .= '
' . t('Provides examples for creating & consuming tokens.') . '
'; + $output .= '
' . t('Contains implementation of simple token, token with return type, dynamic token and custom token data type.') . '
'; + $output .= ''; + return $output; + } + +} + +/** + * Implements hook_token_info(). + */ +function token_example_token_info() { + $types['token_example'] = array( + 'name' => t('Token Example'), + 'description' => t('Example of tokens'), + 'needs-data' => 'token_example', + ); + $types['token_example_type'] = array( + 'name' => t('Token data type'), + 'description' => t('Creates new data type token_example.'), + 'type' => 'token_example_foo', + ); + + $token_example['simple'] = array( + 'name' => t('Simple'), + 'description' => t("A simple token"), + ); + $token_example['with-type'] = array( + 'name' => t("Token with type"), + 'description' => t("Token with type of value as UNIX timestamp."), + 'type' => 'date' + ); + $token_example['dynamic'] = array( + 'name' => t('Dynamic'), + 'description' => t("A token with dynamic value"), + ); + + return array( + 'types' => $types, + 'tokens' => array('token_example' => $token_example), + ); +} + +/** + * Implements hook_tokens(). + */ +function token_example_tokens($type, $tokens, array $data = array(), array $options = array()) { + + $token_service = \Drupal::token(); + + if (isset($options['langcode'])) { + $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); + $langcode = $options['langcode']; + } + else { + $langcode = NULL; + } + $sanitize = !empty($options['sanitize']); + $replacements = array(); + + if ($type == 'token_example') { + foreach ($tokens as $name => $original) { + switch ($name) { + case 'simple': + $string = 'foo'; + $replacements[$original] = $sanitize ? String::checkPlain($string) : $string; + break; + case 'with-type': + $replacements[$original] = time(); + break; + } + } + if ($registered_tokens = $token_service->findWithPrefix($tokens, 'with-type')) { + $replacements += $token_service->generate('date', $registered_tokens, array('date' => time()), $options); + } + + // [token_example:dynamic:*] dynamic token. + if ($hash_tokens = $token_service->findWithPrefix($tokens, 'dynamic')) { + foreach ($hash_tokens as $name => $original) { + $replacements[$original] = $name . rand(1, 1000); + } + } + } + + if ($type == 'token_example_type') { + // generate replacement of type token_example. + $replacements = $token_service->generate('token_example_foo', $tokens, array('token_example_foo' => array('bar' => 'baz')), $options); + } + + if ($type == 'token_example_foo' && !empty($data['token_example_foo']) && is_array($data['token_example_foo'])) { + $token_example_type = $data['token_example_foo']; + foreach ($tokens as $name => $original) { + if (isset($token_example_type[$name])) { + $replacements[$original] = $sanitize ? String::checkPlain($token_example_type[$name]) : $token_example_type[$name]; + } + } + } + + return $replacements; +} + +/** + * Implements hook_tokens_alter(). + */ +function token_example_tokens_alter(array &$replacements, array $context) { + // @TODO: Add token value alter. +} + +/** + * Implements hook_token_info_alter(). + */ +function token_example_token_info_alter(&$data) { + // @TODO: Add token info alter. +} diff --git a/token_example/token_example.routing.yml b/token_example/token_example.routing.yml index 0c63ed5..16cc867 100644 --- a/token_example/token_example.routing.yml +++ b/token_example/token_example.routing.yml @@ -1,6 +1,6 @@ -token_example_description: - path: 'examples/token_example' - defaults: - _content: '\Drupal\token_example\Controller\TokenExampleController::description' - requirements: - _access: 'TRUE' \ No newline at end of file +token_example.example: + path: 'examples/token_example' + defaults: + _content: '\Drupal\token_example\Controller\TokenExampleController::getToken' + requirements: + _access: 'TRUE'