0

How can I pass a variable from my custom module to a twig field template?

my field template is the "field--node--field-webform--parliament-visit.html.twig"

and my module is named as "custommodule"

I tried this function according to documentation but apparently I didnt understand smth correctly:

  function custommodule_preprocess_field_node_field_webform_parliament_visit(array &$variables) {
    $VARIABLE_NAME = 'my_variable';
    $variables['varname'] = $VARIABLE_NAME ;
  }
5
  • I think the hook function should be named custommodule_preprocess_field_node_field_webform_parliament_visit change logintosubmit by your module name custommodule and don't forget to flush all cache.
    – berramou
    Commented Apr 20, 2019 at 9:42
  • Thanks I ll try that...I was just wondering also where should I place this function. Is it ok if I place it inside my custommoduleBlock.php file or I have to place it only in .module file? Commented Apr 20, 2019 at 9:55
  • 1
    @DimitrisMarkopoulos all hooks must go in .module file, with the exception of preprocess & alter hooks. Those can altenatively go inside your theme's .theme file. Thus, you can place this one in .module file or in .theme file.
    – No Sssweat
    Commented Apr 20, 2019 at 10:34
  • 1
    You should have two underscores where there are two hyphens in the twig file name, so custommodule_preprocess_field__node__field_webform__parliament_visit. Commented Apr 20, 2019 at 18:27
  • @othermachines thanks! that did the trick! Commented Apr 21, 2019 at 10:15

0

Your Answer

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

Browse other questions tagged or ask your own question.