Twig: How to find out if plugin is installed?

Is it possible to find out (inside a twig script) if a specific plugin is currently installed? Checking {% if config.plugins.name.enabled is defined %} only works if the plugin has never been installed before. If the config file of the plugin already exists, this check returns true even if the plugin itself isn’t currently there.

@pikim, If there is a myplugin,yaml file lingering around in any of the config stream folders (e.g. /user/config/plugins/, or /user/env/*/config/plugins/, or user/plugins/myplugin/) and it contains variable enabled, then {% if config.plugins.myplugin.enabled is defined %} will be true.

The class for the plugin does not need to exist for above logical expression to be true.

To test whether a valid object has been instantiated for the plugin, you could test

{% if 'Grav\\Plugin\\<classname>' in grav.plugins|keys) %}

By the way, which problem do you wish to solve? Often the following statement is used before loading a template:

{% if config.plugins.myplugin.enabled %}
   {% include 'partials/myplugin.html.twig' %}
{% endif %}

If any myplugin.yaml is lingering around, an error will be thrown if myplugin.html.twig does not exist.

I want to contribute to a theme and added another plugin as dependency. The initial author wanted to make sure that everything also works without that plugin. Then we noticed, that just checking config.plugins.myplugin.enabled isn’t enough to be 100% sure.

Thanks for that. It works!

@pikim, Thanks for the context.

I want to contribute to a theme and added another plugin as dependency. The initial author wanted to make sure that everything also works without that plugin. Then we noticed, that just checking config.plugins.myplugin.enabled isn’t enough to be 100% sure.

  • When a plugin is added as a dependency to a theme, the plugin cannot be deleted using Admin. An error will be shown in Admin.
  • In case the plugin has been deleted manually and a config still exists, the following include statement will not throw an error and ignore the template.
    {% if config.plugins.myplugin.enabled %}
      {% include 'partials/myplugin.html.twig' ignore missing %}
    {% endif %}
    

I wouldn’t suggest to use my ugly suggestion, but instead rely on the above.