Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Twig: How to find out if plugin is installed?

Solved by pamtbaau View solution

Started by p 3 years ago · 3 replies · 780 views
3 years ago

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.

last edited 01/24/23 by p
3 years ago Solution

@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

TWIG
{% 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:

TWIG
{% 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.

last edited 01/25/23 by pamtbaau
3 years ago

@pamtbaau:
By the way, which problem do you wish to solve?

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.

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

Thanks for that. It works!

3 years ago

@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.
    TWIG
    {% 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.

last edited 01/25/23 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 49 1 week ago
Plugins · by Xavier, 4 weeks ago
2 57 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1184 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 50 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 75 2 months ago