Chris@0: getOwner()), Chris@0: * implementations of this hook must add the corresponding metadata. Chris@0: * For example: Chris@0: * @code Chris@0: * $bubbleable_metadata->addCacheableDependency(\Drupal::config('system.site')); Chris@0: * $bubbleable_metadata->addCacheableDependency($node->getOwner()); Chris@0: * @endcode Chris@0: * Chris@0: * Additionally, implementations of this hook, must forward Chris@0: * $bubbleable_metadata to the chained tokens that they invoke. Chris@0: * For example: Chris@0: * @code Chris@0: * if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) { Chris@0: * $replacements = $token_service->generate('date', $created_tokens, array('date' => $node->getCreatedTime()), $options, $bubbleable_metadata); Chris@0: * } Chris@0: * @endcode Chris@0: * Chris@0: * @return array Chris@0: * An associative array of replacement values, keyed by the raw [type:token] Chris@0: * strings from the original text. The returned values must be either plain Chris@0: * text strings, or an object implementing MarkupInterface if they are Chris@0: * HTML-formatted. Chris@0: * Chris@0: * @see hook_token_info() Chris@0: * @see hook_tokens_alter() Chris@0: */ Chris@0: function hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) { Chris@0: $token_service = \Drupal::token(); Chris@0: Chris@0: $url_options = ['absolute' => TRUE]; Chris@0: if (isset($options['langcode'])) { Chris@0: $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); Chris@0: $langcode = $options['langcode']; Chris@0: } Chris@0: else { Chris@0: $langcode = NULL; Chris@0: } Chris@0: $replacements = []; Chris@0: Chris@0: if ($type == 'node' && !empty($data['node'])) { Chris@0: /** @var \Drupal\node\NodeInterface $node */ Chris@0: $node = $data['node']; Chris@0: Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: // Simple key values on the node. Chris@0: case 'nid': Chris@0: $replacements[$original] = $node->nid; Chris@0: break; Chris@0: Chris@0: case 'title': Chris@0: $replacements[$original] = $node->getTitle(); Chris@0: break; Chris@0: Chris@0: case 'edit-url': Chris@18: $replacements[$original] = $node->toUrl('edit-form', $url_options)->toString(); Chris@0: break; Chris@0: Chris@0: // Default values for the chained tokens handled below. Chris@0: case 'author': Chris@0: $account = $node->getOwner() ? $node->getOwner() : User::load(0); Chris@0: $replacements[$original] = $account->label(); Chris@0: $bubbleable_metadata->addCacheableDependency($account); Chris@0: break; Chris@0: Chris@0: case 'created': Chris@18: $replacements[$original] = \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'medium', '', NULL, $langcode); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($author_tokens = $token_service->findWithPrefix($tokens, 'author')) { Chris@0: $replacements += $token_service->generate('user', $author_tokens, ['user' => $node->getOwner()], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) { Chris@0: $replacements += $token_service->generate('date', $created_tokens, ['date' => $node->getCreatedTime()], $options, $bubbleable_metadata); Chris@0: } Chris@0: } Chris@0: Chris@0: return $replacements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alter replacement values for placeholder tokens. Chris@0: * Chris@0: * @param $replacements Chris@0: * An associative array of replacements returned by hook_tokens(). Chris@0: * @param $context Chris@0: * The context in which hook_tokens() was called. An associative array with Chris@0: * the following keys, which have the same meaning as the corresponding Chris@0: * parameters of hook_tokens(): Chris@0: * - 'type' Chris@0: * - 'tokens' Chris@0: * - 'data' Chris@0: * - 'options' Chris@0: * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata Chris@0: * The bubbleable metadata. In case you alter an existing token based upon Chris@0: * a data source that isn't in $context['data'], you must add that Chris@0: * dependency to $bubbleable_metadata. Chris@0: * Chris@0: * @see hook_tokens() Chris@0: */ Chris@0: function hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) { Chris@0: $options = $context['options']; Chris@0: Chris@0: if (isset($options['langcode'])) { Chris@0: $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); Chris@0: $langcode = $options['langcode']; Chris@0: } Chris@0: else { Chris@0: $langcode = NULL; Chris@0: } Chris@0: Chris@0: if ($context['type'] == 'node' && !empty($context['data']['node'])) { Chris@0: $node = $context['data']['node']; Chris@0: Chris@0: // Alter the [node:title] token, and replace it with the rendered content Chris@0: // of a field (field_title). Chris@0: if (isset($context['tokens']['title'])) { Chris@0: $title = $node->field_title->view('default'); Chris@0: $replacements[$context['tokens']['title']] = drupal_render($title); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provide information about available placeholder tokens and token types. Chris@0: * Chris@0: * Tokens are placeholders that can be put into text by using the syntax Chris@0: * [type:token], where type is the machine-readable name of a token type, and Chris@0: * token is the machine-readable name of a token within this group. This hook Chris@0: * provides a list of types and tokens to be displayed on text editing screens, Chris@0: * so that people editing text can see what their token options are. Chris@0: * Chris@0: * The actual token replacement is done by Chris@0: * \Drupal\Core\Utility\Token::replace(), which invokes hook_tokens(). Your Chris@0: * module will need to implement that hook in order to generate token Chris@0: * replacements from the tokens defined here. Chris@0: * Chris@0: * @return Chris@0: * An associative array of available tokens and token types. The outer array Chris@0: * has two components: Chris@0: * - types: An associative array of token types (groups). Each token type is Chris@0: * an associative array with the following components: Chris@0: * - name: The translated human-readable short name of the token type. Chris@0: * - description (optional): A translated longer description of the token Chris@0: * type. Chris@0: * - needs-data: The type of data that must be provided to Chris@0: * \Drupal\Core\Utility\Token::replace() in the $data argument (i.e., the Chris@0: * key name in $data) in order for tokens of this type to be used in the Chris@0: * $text being processed. For instance, if the token needs a node object, Chris@0: * 'needs-data' should be 'node', and to use this token in Chris@0: * \Drupal\Core\Utility\Token::replace(), the caller needs to supply a Chris@0: * node object as $data['node']. Some token data can also be supplied Chris@0: * indirectly; for instance, a node object in $data supplies a user object Chris@0: * (the author of the node), allowing user tokens to be used when only Chris@0: * a node data object is supplied. Chris@0: * - tokens: An associative array of tokens. The outer array is keyed by the Chris@0: * group name (the same key as in the types array). Within each group of Chris@0: * tokens, each token item is keyed by the machine name of the token, and Chris@0: * each token item has the following components: Chris@0: * - name: The translated human-readable short name of the token. Chris@0: * - description (optional): A translated longer description of the token. Chris@0: * - type (optional): A 'needs-data' data type supplied by this token, which Chris@0: * should match a 'needs-data' value from another token type. For example, Chris@0: * the node author token provides a user object, which can then be used Chris@0: * for token replacement data in \Drupal\Core\Utility\Token::replace() Chris@0: * without having to supply a separate user object. Chris@0: * Chris@0: * @see hook_token_info_alter() Chris@0: * @see hook_tokens() Chris@0: */ Chris@0: function hook_token_info() { Chris@0: $type = [ Chris@0: 'name' => t('Nodes'), Chris@0: 'description' => t('Tokens related to individual nodes.'), Chris@0: 'needs-data' => 'node', Chris@0: ]; Chris@0: Chris@0: // Core tokens for nodes. Chris@0: $node['nid'] = [ Chris@0: 'name' => t("Node ID"), Chris@0: 'description' => t("The unique ID of the node."), Chris@0: ]; Chris@0: $node['title'] = [ Chris@0: 'name' => t("Title"), Chris@0: ]; Chris@0: $node['edit-url'] = [ Chris@0: 'name' => t("Edit URL"), Chris@0: 'description' => t("The URL of the node's edit page."), Chris@0: ]; Chris@0: Chris@0: // Chained tokens for nodes. Chris@0: $node['created'] = [ Chris@0: 'name' => t("Date created"), Chris@0: 'type' => 'date', Chris@0: ]; Chris@0: $node['author'] = [ Chris@0: 'name' => t("Author"), Chris@0: 'type' => 'user', Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'types' => ['node' => $type], Chris@0: 'tokens' => ['node' => $node], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alter the metadata about available placeholder tokens and token types. Chris@0: * Chris@0: * @param $data Chris@0: * The associative array of token definitions from hook_token_info(). Chris@0: * Chris@0: * @see hook_token_info() Chris@0: */ Chris@0: function hook_token_info_alter(&$data) { Chris@0: // Modify description of node tokens for our site. Chris@0: $data['tokens']['node']['nid'] = [ Chris@0: 'name' => t("Node ID"), Chris@0: 'description' => t("The unique ID of the article."), Chris@0: ]; Chris@0: $data['tokens']['node']['title'] = [ Chris@0: 'name' => t("Title"), Chris@0: 'description' => t("The title of the article."), Chris@0: ]; Chris@0: Chris@0: // Chained tokens for nodes. Chris@0: $data['tokens']['node']['created'] = [ Chris@0: 'name' => t("Date created"), Chris@0: 'description' => t("The date the article was posted."), Chris@0: 'type' => 'date', Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @} End of "addtogroup hooks". Chris@0: */