3

I have a Webform form (formerly the YAMLForm module) on which I need to show the title of the form. The template I am using is based on page.html.twig (because of twig debug suggestion) page--webform--stories-submission.html.twig.

According to the tokens listed in both in the form creation area and in admin/help/token, I have several tokens available including [webform:title], [webform:url], and [webform:id] to name a few.

When I use

    {{ drupal_token('webform:title') }}

on my template, all that prints out on the page is [webform:title].

Just to make sure I'm doing it right, I put all of the following smack into the middle of my template:

    {{ drupal_token('current-date:long') }}
    {{ drupal_token('site:name') }}
    {{ drupal_token('webform:title') }}
    {{ drupal_token('webform:id') }}

I get this in return:

Wednesday, January 18, 2017 - 09:00 ECLKC [webform:title] [webform:id]

The first two prove that Twig Tweak is a wonderful thing, and the second two prove that I'm still not doing it right.

Can anyone recommend a next step for me?

If I can't get Tweak working, I guess I might need to preprocess myself a variable? Kint says that my title is right there as a page variable:

'page' => array(25)
~
  '#title' => string(29) "Submit your Head Start Story!"

In that case, I think I would need to follow this advice: How do I add a variable for a Twig template in my preprocess function?

To sum up:

  1. Can I get Twig Tweak to show my title for me?
  2. If not, should I use that advice about adding a variable to my template preprocess?

2 Answers 2

2

Currently, it looks like the Twig Tweak module's drupal_token Twig function does not support passing in the $data argument, which would contain the webform entity.

You might want to create an issue in the Twig Tweak module's issue queue.

2
1

Can I get Twig Tweak to show my title for me?

Tokens don't work that way. The only tokens available in all places are the global ones, like the examples you have in your question. The specific tokens work only in their own environment and it would be impossible for twig tweak to provide all of these environments.

If not, should I use that advice about adding a variable to my template preprocess?

In D7 the title was a variable in the page template. This has changed in D8. See Page title is now a block

2
  • Thank you 4k4. That was a helpful explanation of why I can't use it. I definitely do not want to go the route of placing a title block through the admin menu and trying to restrict/allow page titles on the myriad of use cases that come up.
    – LeraA
    Jan 20, 2017 at 14:54
  • If you want the title variable back in the page template, see the first removed line in this patch drupal.org/files/issues/…
    – 4uk4
    Jan 20, 2017 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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