0

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

2 Answers 2

1

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.

1
  • 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. Mar 7 at 10:50
0

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

6
  • That module is not part of Drupal core.
    – apaderno
    Mar 7 at 10:44
  • 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 Mar 7 at 10:56
  • 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.
    – apaderno
    Mar 7 at 10:58
  • Feel free to try what I suggested. I have and it worked perfectly well with a contrib module. Mar 7 at 11:00
  • 1
    How to define dependencies is explained in How would we add Drupal Core modules as dependencies on custom modules post 8.6.2? which also quotes documentation with an example for a module that is not part of Drupal core.
    – apaderno
    Mar 7 at 11:56

Your Answer

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

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