1

I am creating the following pseudo field in hook_node_view:

function user_guides_node_view($node, $view_mode, $langcode) {
    $node->content['field_mytest'] = array('#markup' => '<p>This is my test</p><p>[current-page:title]</p>');
}

This is the rendered result:

This is my test
[current-page:title]

How can I get the value of the token to be displayed instead of the token itself?

  • #markup is raw HTML markup (eg, the final product). you must 1st pass your token value through a render function. – tenken Oct 20 '14 at 18:26
  • Is there something already that I can pass a block of text through with the token somewhere in the middle, and it parses the token out of the text and replaces it with its value? – joshmmo Oct 20 '14 at 18:28
  • Question: Why do you need to use tokens in first place? You have the node object, and all the static variables that you can access with PHP. – AyeshK Oct 20 '14 at 19:13
2

You can use token_replace() for that. As you're using a global token (no context required) it's very simple:

$markup = token_replace('<p>This is my test</p><p>[current-page:title]</p>');
$node->content['field_mytest']['#markup'] = $markup;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.