Score:0

How do I check if TwigTweak is enabled before declaring a function in a template?

us flag

I am cretaing a theme which uses the twigtweak module to display some views/blocks in some of the templates.

If a person enables the theme before enabling twigtweak module they get an error. For example if I put the following in a template, the site gets a php error 'drupal_entity is an unknown function'

{{ drupal_entity('block_content', 1) }}

So I try

{% if drupal_entity ?? false %}
    {{ drupal_entity('block_content', 1) }}
{% else %}
    <p>You should enable Twig Tweak module for this functionality</p>
{% endif %}

But I am still getting similar errors. How can I check if Twig Tweak is enabled or if the Twig Tweak function is valid/exists?

Thanks

Score:1
us flag

On Drupal 8.9.x and successive Drupal versions, a theme can define its dependencies from modules. When that theme is installed, Drupal core will require that module to be installed, before allowing the theme installation. This means that when a theme template file is used, the require to use it.

Themes can declare dependencies on modules gives more details, but essentially that is done as it is done with modules. In the .info.yml file for the theme, it is sufficient to add lines like the following ones. (Replace <module> with the module name, name-spaced, for example drupal:user for the User module that is part of Drupal core, or twig_tweak:twig_tweak for the Twig Tweak module.)

dependencies:
  - <module>

If instead you want the theme to optionally depend on that module, you need to implement a preprocess function that is used for that template file. Since the question does not make clear which template file is used, I will make an example that is valid for the user.html.twig template file, but that can be adapted to other template files. (Replace <theme_name> with the theme machine name.)

function <theme_name>_preprocess_user(&$variables) {
  $variables['twig_tweak'] = \Drupal::moduleHandler()->moduleExists('twig_tweak');
}

Then, the template file should check the value of that variable.

{% if twig_tweak %}
  {{ drupal_entity('block_content', 1) }}
{% else %}
  { # Show the content in a different way #}
{% endif %}

Since that module is an optional dependency, users should not see a message telling them to install a module when the module is not installed, which would probably be seen even from users who cannot install new modules on that site. Instead, the template file should shown alternative content, or show the same content in a different way.

If the same variable is required from more template files, instead of a preprocess function that is called for a single template file, I would implement <theme_name>_preprocess(), which is called for every template file.

Jay Williams avatar
us flag
Hi, thanks but I am trying to make it optional. So added functionality if the module is enabled but the theme still works if it isn't.
Score:0
aq flag

If your theme depends on a module to function you should add the module as a dependancy in your themes .info.yml file. e.g.:

dependencies:
  - drupal:twig_tweak

Then people won't be able to install your theme without it

apaderno avatar
us flag
That module is not part of Drupal core.
unusedspoon avatar
aq flag
I know but what's your point? That's how the module is named in Drupal's package list and it's how to add it as a dependency
apaderno avatar
us flag
With `drupal:twig_tweak`, Drupal will search the module in the Drupal core modules. That is not the namespaced name of that module, which must follow the `<project name>:<module name>` schema.
unusedspoon avatar
aq flag
Feel free to try what I suggested. I have and it worked perfectly well with a contrib module.
apaderno avatar
us flag
It works when the module is already installed. If Drupal needs to give a link to the missing module, it needs the correct project name.
apaderno avatar
us flag
How to define dependencies is explained in [How would we add Drupal Core modules as dependencies on custom modules post 8.6.2?](https://drupal.stackexchange.com/questions/272106/how-would-we-add-drupal-core-modules-as-dependencies-on-custom-modules-post-8-6) which also quotes documentation with an example for a module that is not part of Drupal core.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.