5

I want to display webform title in a webform.html.twig but I did not find anything in variables array regarding the title of the webform

function THEMENAME_preprocess_webform($variables){
  $title = "?";
}

How do I get webform title in a preprocess function ? Any suggestions or other approach ?

1 Answer 1

7

Here is what I came up with

function THEMENAME_preprocess_webform(&$vars){
  $wfid = $vars["element"]["#webform_id"];
  $wf = \Drupal\webform\Entity\Webform::load($wfid);
  $vars["title"] = $wf->label();
}

Then in your theme's webform.html.twig you can use {{title}}

4
  • That is the best solution.
    – jrockowitz
    Commented Feb 23, 2018 at 1:54
  • Thanks coming from you I take it its true ... wouldn't it make sense for webform to include it in the variables array for preprocessors to consume ?
    – GiorgosK
    Commented Feb 23, 2018 at 6:13
  • The webform.twig.html template just renders the form does not really need to include the {{ title }}. cgit.drupalcode.org/webform/tree/templates/webform.html.twig
    – jrockowitz
    Commented Feb 23, 2018 at 10:50
  • It doesn't need it, but if you dump(_context|keys) you'll see that it does have all the other makings necessary to have a working {{title}} field including {{ title_attributes }} as well as the less related misnomer {{ title_prefix }} and {{ title_suffix }}, so there is no reason not to pass the variable to the template, even if not used by default. I want to file this as a TX bug. Commented Jun 20, 2018 at 20:10

Your Answer

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

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