I'm working on an input.html.twig file. I need the label name of the input for class and other reasons, but for some reason, I can't figure out how to grab it. I thought it would be part of the twig variables but I don't see it when I use kint().

So I tried using preprocess but it's not showing up.

function cougar_preprocess_form_element(&$vars){
$element =& $variables['element'];
$vars['test'] = 'test'; 
$vars['title'] = ['#markup' => $element['#title']];

I'm at a loss how to add the title to the input field twig file

  • 1
    If a class is all you need, would the input name work? {{ element['#name'] | clean_class }} – Prestosaurus May 14 '19 at 2:51
  • 1
    You almost there that you use ` $variables` instead of $vars ? change $element =& $variables['element']; by ` $element = $vars['element'];` you will get the the title from $element['#title'] – berramou May 14 '19 at 12:52
  • Okay, so I should have used $variables instead of $vars. So that part worked. Then I used clean_id and setAttribute to set it as the ID in the input field. Which is what I needed. – Tyler May 14 '19 at 13:43

Your Answer

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

Browse other questions tagged or ask your own question.