0

I've write small custom function in my .theme file. Is it possible to call that function in twig template files?

I would like to fetch description value for a field in views template file, so can we add a function like function getFieldDescription($field) and access in views template file ?

Ex: https://www.drupal.org/node/2486991 has url() function, which we can use in any twig template file.

1 Answer 1

1

Depending on the nature of your function, maybe but probably not.

As Clive pointed out, you can use certain functions in Twig. These functions however are mostly variable filters (formatters) for last minute alterations on variables. These can be used to translate, link, or in any way alter a provided variable. These functions are a combination of those provided by twig and some additions by Drupal. You can register your own Twig function, by extending on the TwigExtension class (example).

However most functions will probably be for adding additional data to the template and not to alter existing data. Instead you should place your custom function in a preprocessor and define a variable there. Then display this variable in the template.

Custom preprocessor (replace my_theme with your theme or custom module name):

function my_theme_preprocess_node(&$variables) {
  $variables['my_variable'] = 'value';
}

Twig:

{{ my_variable }}

Here is some reading on twig best practices: Twig best practices.

Some example code on preprocessors is here: template_preprocess_node(). Which is in fact systems invocation of hook_preprocess_HOOK().

5
  • It's not quite as cut-and-dry as that - the best practice docs you linked to actually state: To provide raw data to templates for as long as possible, theme developers should call filters such as t and utility functions such as url() from within Twig templates. Calling these functions in the Twig template rather than the preprocess function can cut down on function calls since variables passed to the template might not get printed in the template at all. So depending on what the function does, wrapping it up in an extension might be the right thing to do from a best-practice point of view
    – Clive
    Commented Feb 23, 2016 at 22:56
  • Thanks @clive, I learned twig in a codeigniter project some years ago and the only logic I was allowed to do was for-loops, assuming it would be twigs limitation not to be able to run functions. But your explanation makes sense as well. Thanks. Commented Feb 24, 2016 at 0:35
  • Updated the answer to be more complete. Commented Feb 24, 2016 at 1:09
  • Thanks for the info, I would like to fetch description value for a field in views template file, so can we add a function like function getFieldDescription($field) and access in views template file ? Ex: drupal.org/node/2486991 has url() function, which we can use in any twig template file.
    – srini
    Commented Feb 24, 2016 at 3:55
  • Yes, you can do that by providing a custom Twig extension. You'll need to write a custom class for that extending toe TwigExtension class. See linked example. (upper half of the answer) Commented Feb 24, 2016 at 6:46

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.