interface BlockPluginInterface implements ConfigurableInterface, DependentPluginInterface, ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, CacheableDependencyInterface, DerivativeInspectionInterface (View source)

Defines the required interface for all block plugins.

Add detailed documentation here explaining the block system's architecture and the relationships between the various objects, including brief references to the important components that are not coupled to the interface.

Constants

BLOCK_LABEL_VISIBLE

Indicates the block label (title) should be displayed to end users.

Methods

array
getConfiguration()

Gets this plugin's configuration.

setConfiguration(array $configuration)

Sets the configuration for this plugin instance.

array
defaultConfiguration()

Gets default configuration for this plugin.

array
calculateDependencies()

Calculates dependencies for the configured plugin.

array
buildConfigurationForm(array $form, FormStateInterface $form_state)

Form constructor.

validateConfigurationForm(array $form, FormStateInterface $form_state)

Form validation handler.

submitConfigurationForm(array $form, FormStateInterface $form_state)

Form submission handler.

string
getPluginId()

Gets the plugin_id of the plugin instance.

array
getPluginDefinition()

Gets the definition of the plugin implementation.

string[]
getCacheContexts()

The cache contexts associated with this object.

string[]
getCacheTags()

The cache tags associated with this object.

int
getCacheMaxAge()

The maximum age for which this object may be cached.

string
getBaseId()

Gets the base_plugin_id of the plugin instance.

string|null
getDerivativeId()

Gets the derivative_id of the plugin instance.

string
label()

Returns the user-facing block label.

access(AccountInterface $account, bool $return_as_object = FALSE)

Indicates whether the block should be shown.

array
build()

Builds and returns the renderable array for this block plugin.

setConfigurationValue(string $key, mixed $value)

Sets a particular value in the block settings.

array
blockForm(array $form, FormStateInterface $form_state)

Returns the configuration form elements specific to this block plugin.

blockValidate(array $form, FormStateInterface $form_state)

Adds block type-specific validation for the block form.

blockSubmit(array $form, FormStateInterface $form_state)

Adds block type-specific submission handling for the block form.

string
getMachineNameSuggestion()

Suggests a machine name to identify an instance of this block.

Details

array getConfiguration()

Gets this plugin's configuration.

Return Value

array

An array of this plugin's configuration.

setConfiguration(array $configuration)

Sets the configuration for this plugin instance.

Parameters

array $configuration

An associative array containing the plugin's configuration.

array defaultConfiguration()

Gets default configuration for this plugin.

Return Value

array

An associative array with the default configuration.

array calculateDependencies()

Calculates dependencies for the configured plugin.

Dependencies are saved in the plugin's configuration entity and are used to determine configuration synchronization order. For example, if the plugin integrates with specific user roles, this method should return an array of dependencies listing the specified roles.

Return Value

array

An array of dependencies grouped by type (config, content, module, theme). For example: @code array( 'config' => array('user.role.anonymous', 'user.role.authenticated'), 'content' => array('node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'), 'module' => array('node', 'user'), 'theme' => array('seven'), ); @endcode

See also

ConfigDependencyManager
EntityInterface::getConfigDependencyName

array buildConfigurationForm(array $form, FormStateInterface $form_state)

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and

array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form

An associative array containing the initial structure of the plugin form.

FormStateInterface $form_state

The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return Value

array

The form structure.

validateConfigurationForm(array $form, FormStateInterface $form_state)

Form validation handler.

Parameters

array $form

An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

FormStateInterface $form_state

The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

submitConfigurationForm(array $form, FormStateInterface $form_state)

Form submission handler.

Parameters

array $form

An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

FormStateInterface $form_state

The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

string getPluginId()

Gets the plugin_id of the plugin instance.

Return Value

string

The plugin_id of the plugin instance.

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.

string[] getCacheContexts()

The cache contexts associated with this object.

These identify a specific variation/representation of the object.

Cache contexts are tokens: placeholders that are converted to cache keys by the @cache_contexts_manager service. The replacement value depends on the request context (the current URL, language, and so on). They're converted before storing an object in cache.

Return Value

string[]

An array of cache context tokens, used to generate a cache ID.

See also

CacheContextsManager::convertTokensToKeys

string[] getCacheTags()

The cache tags associated with this object.

When this object is modified, these cache tags will be invalidated.

Return Value

string[]

A set of cache tags.

int getCacheMaxAge()

The maximum age for which this object may be cached.

Return Value

int

The maximum time in seconds that this object may be cached.

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.

string label()

Returns the user-facing block label.

Provide other specific label-related methods in https://www.drupal.org/node/2025649.

Return Value

string

The block label.

bool|AccessResultInterface access(AccountInterface $account, bool $return_as_object = FALSE)

Indicates whether the block should be shown.

This method allows base implementations to add general access restrictions that should apply to all extending block plugins.

Parameters

AccountInterface $account

The user session for which to check access.

bool $return_as_object

(optional) Defaults to FALSE.

Return Value

bool|AccessResultInterface

The access result. Returns a boolean if $return_as_object is FALSE (this is the default) and otherwise an AccessResultInterface object. When a boolean is returned, the result of AccessInterface::isAllowed() is returned, i.e. TRUE means access is explicitly allowed, FALSE means access is either explicitly forbidden or "no opinion".

See also

BlockAccessControlHandler

array build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return Value

array

A renderable array representing the content of the block.

See also

BlockViewBuilder

setConfigurationValue(string $key, mixed $value)

Sets a particular value in the block settings.

This doesn't belong here. Move this into a new base class in https://www.drupal.org/node/1764380. This does not set a value in \Drupal::config(), so the name is confusing.

Parameters

string $key

The key of PluginBase::$configuration to set.

mixed $value

The value to set for the provided key.

See also

PluginBase::$configuration

array blockForm(array $form, FormStateInterface $form_state)

Returns the configuration form elements specific to this block plugin.

Blocks that need to add form elements to the normal block configuration form should implement this method.

Parameters

array $form

The form definition array for the block configuration form.

FormStateInterface $form_state

The current state of the form.

Return Value

array

The renderable form array representing the entire configuration form.

blockValidate(array $form, FormStateInterface $form_state)

Adds block type-specific validation for the block form.

Note that this method takes the form structure and form state for the full block configuration form as arguments, not just the elements defined in BlockPluginInterface::blockForm().

Parameters

array $form

The form definition array for the full block configuration form.

FormStateInterface $form_state

The current state of the form.

See also

BlockPluginInterface::blockForm
BlockPluginInterface::blockSubmit

blockSubmit(array $form, FormStateInterface $form_state)

Adds block type-specific submission handling for the block form.

Note that this method takes the form structure and form state for the full block configuration form as arguments, not just the elements defined in BlockPluginInterface::blockForm().

Parameters

array $form

The form definition array for the full block configuration form.

FormStateInterface $form_state

The current state of the form.

See also

BlockPluginInterface::blockForm
BlockPluginInterface::blockValidate

string getMachineNameSuggestion()

Suggests a machine name to identify an instance of this block.

The block plugin need not verify that the machine name is at all unique. It is only responsible for providing a baseline suggestion; calling code is responsible for ensuring whatever uniqueness is required for the use case.

Return Value

string

The suggested machine name.