abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface (View source)

Base class for plugins that are context aware.

Constants

DERIVATIVE_SEPARATOR

A string which is used to separate base plugin IDs from the derivative ID.

Properties

protected string $pluginId

The plugin_id.

from  PluginBase
protected array $pluginDefinition

The plugin implementation definition.

from  PluginBase
protected array $configuration

Configuration information passed into the plugin.

from  PluginBase
protected ContextInterface[] $context

The data objects representing the context of this plugin.

Methods

__construct(array $configuration, string $plugin_id, mixed $plugin_definition)

Overrides \Drupal\Component\Plugin\PluginBase::__construct().

string
getPluginId()

Gets the plugin_id of the plugin instance.

string
getBaseId()

Gets the base_plugin_id of the plugin instance.

string|null
getDerivativeId()

Gets the derivative_id of the plugin instance.

array
getPluginDefinition()

Gets the definition of the plugin implementation.

bool
isConfigurable()

Determines if the plugin is configurable.

createContextFromConfiguration(array $context_configuration)

Creates context objects from any context mappings in configuration.

getContextDefinitions()

Gets the context definitions of the plugin.

getContextDefinition(string $name)

Gets a specific context definition of the plugin.

array
getContexts()

Gets the defined contexts.

getContext(string $name)

Gets a defined context.

setContext(string $name, ContextInterface $context)

Set a context on this plugin.

array
getContextValues()

Gets the values for all defined contexts.

mixed
getContextValue(string $name)

Gets the value for a defined context.

$this
setContextValue(string $name, mixed $value)

Sets the value for a defined context.

ConstraintViolationListInterface
validateContexts()

Validates the set values for the defined contexts.

Details

__construct(array $configuration, string $plugin_id, mixed $plugin_definition)

Overrides \Drupal\Component\Plugin\PluginBase::__construct().

Overrides the construction of context aware plugins to allow for unvalidated constructor based injection of contexts.

Parameters

array $configuration

A configuration array containing information about the plugin instance.

string $plugin_id

The plugin_id for the plugin instance.

mixed $plugin_definition

The plugin implementation definition.

string getPluginId()

Gets the plugin_id of the plugin instance.

Return Value

string

The plugin_id of the plugin instance.

string getBaseId()

Gets the base_plugin_id of the plugin instance.

Return Value

string

The base_plugin_id of the plugin instance.

string|null getDerivativeId()

Gets the derivative_id of the plugin instance.

Return Value

string|null

The derivative_id of the plugin instance NULL otherwise.

array getPluginDefinition()

Gets the definition of the plugin implementation.

Return Value

array

The plugin definition, as returned by the discovery object used by the plugin manager.

bool isConfigurable()

Determines if the plugin is configurable.

Return Value

bool

A boolean indicating whether the plugin is configurable.

protected ContextInterface[] createContextFromConfiguration(array $context_configuration)

Creates context objects from any context mappings in configuration.

Parameters

array $context_configuration

An associative array of context names and values.

Return Value

ContextInterface[]

An array of context objects.

ContextDefinitionInterface[] getContextDefinitions()

Gets the context definitions of the plugin.

Return Value

ContextDefinitionInterface[]

The array of context definitions, keyed by context name.

ContextDefinitionInterface getContextDefinition(string $name)

Gets a specific context definition of the plugin.

Parameters

string $name

The name of the context in the plugin definition.

Return Value

ContextDefinitionInterface

The definition against which the context value must validate.

Exceptions

ContextException

array getContexts()

Gets the defined contexts.

Return Value

array

The set context objects.

Exceptions

ContextException

ContextInterface getContext(string $name)

Gets a defined context.

Parameters

string $name

The name of the context in the plugin definition.

Return Value

ContextInterface

The context object.

Exceptions

ContextException

setContext(string $name, ContextInterface $context)

Set a context on this plugin.

Parameters

string $name

The name of the context in the plugin configuration.

ContextInterface $context

The context object to set.

array getContextValues()

Gets the values for all defined contexts.

Return Value

array

An array of set context values, keyed by context name. If a context is unset its value is returned as NULL.

mixed getContextValue(string $name)

Gets the value for a defined context.

Parameters

string $name

The name of the context in the plugin configuration.

Return Value

mixed

The currently set context value.

Exceptions

ContextException

$this setContextValue(string $name, mixed $value)

Sets the value for a defined context.

Parameters

string $name

The name of the context in the plugin definition.

mixed $value

The value to set the context to. The value has to validate against the provided context definition.

Return Value

$this

A context aware plugin object for chaining.

Exceptions

ContextException

ConstraintViolationListInterface validateContexts()

Validates the set values for the defined contexts.

Return Value

ConstraintViolationListInterface

A list of constraint violations. If the list is empty, validation succeeded.