PluginInterface

Type
Interface
Namespace
craft\base
Implemented by
craft\base\Plugin
Since
3.0.0

PluginInterface defines the common interface to be implemented by plugin classes.

A class implementing this interface should also use craft\base\PluginTrait.

View source (opens new window)

# Public Methods

Method Description
afterSaveSettings() Performs actions after the plugin’s settings are saved.
beforeSaveSettings() Performs actions before the plugin’s settings are saved.
config() Returns the base config that the plugin should be instantiated with.
editions() Returns supported plugin editions (lowest to highest).
getCpNavItem() Returns the control panel nav item definition for this plugin, if it has a section in the control panel.
getHandle() Returns the plugin’s handle (really just an alias of yii\base\Module::$id (opens new window)).
getMigrator() Returns the plugin’s migration manager
getSettings() Returns the model that the plugin’s settings should be stored on, if the plugin has settings.
getSettingsResponse() Returns the settings page response.
install() Installs the plugin.
setSettings() Sets the plugin settings
uninstall() Uninstalls the plugin.

# afterSaveSettings()

Since
3.0.16

Performs actions after the plugin’s settings are saved.

View source (opens new window)

# beforeSaveSettings()

Since
3.0.16

Performs actions before the plugin’s settings are saved.

View source (opens new window)

Returns

boolean (opens new window) – Whether the plugin’s settings should be saved.

# config()

Since
4.0.0

Returns the base config that the plugin should be instantiated with.

It is recommended that plugins define their internal components from here:

public static function config(): array
{
    return [
        'components' => [
            'myComponent' => ['class' => MyComponent::class],
            // ...
        ],
    ];
}

Doing that enables projects to customize the components as needed, by overriding \craft\services\Plugins::$pluginConfigs in config/app.php:

return [
    'components' => [
        'plugins' => [
            'pluginConfigs' => [
                'my-plugin' => [
                    'components' => [
                        'myComponent' => [
                            'myProperty' => 'foo',
                            // ...
                        ],
                    ],
                ],
            ],
        ],
    ],
];

The resulting config will be passed to \Craft::createObject() to instantiate the plugin.

View source (opens new window)

Returns

array (opens new window)

# editions()

Since
3.1.0

Returns supported plugin editions (lowest to highest).

View source (opens new window)

Returns

string (opens new window)[]

# getCpNavItem()

Returns the control panel nav item definition for this plugin, if it has a section in the control panel.

The returned array should contain the following keys:

  • label – The human-facing nav item label
  • url – The URL the nav item should link to
  • id – The HTML id attribute the nav item should have (optional)
  • icon – The path to an SVG file that should be used as the nav item icon (optional)
  • fontIcon – A character/ligature from Craft’s font icon set (optional)
  • badgeCount – A number that should be displayed beside the nav item when unselected
  • subnav – A sub-array of subnav items

The subnav array should be associative, with identifiable keys set to sub-arrays with the following keys:

  • label – The human-facing subnav item label
  • url – The URL the subnav item should link to

For example:

return [
    'label' => 'Commerce',
    'url' => 'commerce',
    'subnav' => [
        'orders' => ['label' => 'Orders', 'url' => 'commerce/orders',
        'discounts' => ['label' => 'Discounts', 'url' => 'commerce/discounts',
    ],
];

Control panel templates can specify which subnav item is selected by defining a selectedSubnavItem variable.

{% set selectedSubnavItem = 'orders' %}

See also:

View source (opens new window)

Returns

array (opens new window), null (opens new window)

# getHandle()

Returns the plugin’s handle (really just an alias of yii\base\Module::$id (opens new window)).

View source (opens new window)

Returns

string (opens new window) – The plugin’s handle

# getMigrator()

Returns the plugin’s migration manager

View source (opens new window)

Returns

craft\db\MigrationManager – The plugin’s migration manager

# getSettings()

Returns the model that the plugin’s settings should be stored on, if the plugin has settings.

View source (opens new window)

Returns

craft\base\Model, null (opens new window) – The model that the plugin’s settings should be stored on, if the plugin has settings

# getSettingsResponse()

Returns the settings page response.

View source (opens new window)

Returns

mixed – The result that should be returned from craft\controllers\PluginsController::actionEditPluginSettings()

# install()

Installs the plugin.

View source (opens new window)

# setSettings()

Sets the plugin settings

View source (opens new window)

Arguments

# uninstall()

Uninstalls the plugin.

View source (opens new window)