abstract class BlockStyleBase in Block Style Plugins 8.2
Same name and namespace in other branches
- 8 src/Plugin/BlockStyleBase.php \Drupal\block_style_plugins\Plugin\BlockStyleBase
Base class for Block style plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\block_style_plugins\Plugin\BlockStyleBase implements BlockStyleInterface, ContainerFactoryPluginInterface, PluginFormInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of BlockStyleBase
4 files declare their use of BlockStyleBase
- CheckboxWithExclude.php in tests/
modules/ block_style_plugins_test/ src/ Plugin/ BlockStyle/ CheckboxWithExclude.php - DropdownWithInclude.php in tests/
modules/ block_style_plugins_test/ src/ Plugin/ BlockStyle/ DropdownWithInclude.php - MockBlockStyleBase.php in tests/
src/ Unit/ Plugin/ MockBlockStyleBase.php - SimpleClass.php in tests/
modules/ block_style_plugins_test/ src/ Plugin/ BlockStyle/ SimpleClass.php
File
- src/
Plugin/ BlockStyleBase.php, line 16
Namespace
Drupal\block_style_plugins\PluginView source
abstract class BlockStyleBase extends PluginBase implements BlockStyleInterface, ContainerFactoryPluginInterface, PluginFormInterface {
/**
* Plugin ID for the Block being configured.
*
* @var string
*/
protected $pluginId;
/**
* Instance of the Entity Type Manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Construct method for BlockStyleBase.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* An Entity Type Manager instance.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
// Store our dependencies.
$this->entityTypeManager = $entityTypeManager;
// Store the plugin ID.
$this->pluginId = $plugin_id;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return [];
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function build(array $variables) {
// Is the Layout Builder being used for block components?
$layout_builder_block = empty($variables['elements']['#id']) && empty($variables['layout']) ? TRUE : FALSE;
$styles = $this
->getStylesFromVariables($variables);
if (!$styles) {
return $variables;
}
// Layout Builder needs a '#'.
$hash = $layout_builder_block ? '#' : '';
// Add styles to the configuration array so that they can be accessed in a
// preprocess $variables['configuration']['block_styles'] or in a twig
// template as {{ configuration.block_styles.plugin_id.field_name }}.
$variables[$hash . 'configuration']['block_styles'][$this->pluginId] = $styles;
// Automaticlly add each style value as a class.
if (empty($this->pluginDefinition['disable_auto_classes'])) {
foreach ($styles as $class) {
// Don't put a boolean from a checkbox as a class.
if (is_int($class)) {
continue;
}
// Create the attributes class array if it doesn't exist yet.
if (!isset($variables[$hash . 'attributes']['class'])) {
$variables[$hash . 'attributes']['class'] = [];
}
// Add the class into an array unless it is already an array of classes.
if (!is_array($class)) {
$class = [
$class,
];
}
// Add single or multiple classes.
$variables[$hash . 'attributes']['class'] = array_merge($variables[$hash . 'attributes']['class'], $class);
}
}
return $variables;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
/**
* {@inheritdoc}
*/
public function themeSuggestion(array $suggestions, array $variables) {
return $suggestions;
}
/**
* Get styles for a block set in a preprocess $variables array.
*
* @param array $variables
* Block variables coming from a preprocess hook.
*
* @return array|false
* Return the styles array or FALSE
*/
protected function getStylesFromVariables(array $variables) {
$styles = FALSE;
// Ensure that we have a block id. If not, then the Layout Builder is used.
if (empty($variables['elements']['#id'])) {
$styles = $this
->getConfiguration();
// Style config might not be set if this is happening in a hook so we will
// check if a block_styles variable is set and get the config.
if (empty($styles) && isset($variables['elements']['#configuration']['block_styles'][$this->pluginId])) {
$this
->setConfiguration($variables['elements']['#configuration']['block_styles'][$this->pluginId]);
$styles = $this
->getConfiguration();
}
}
else {
// Load the block config entity.
/** @var \Drupal\block\Entity\Block $block */
$block = $this->entityTypeManager
->getStorage('block')
->load($variables['elements']['#id']);
// In the case that this block is placed via Context, it's not a "real"
// block so it will be null. Attempt to get the block via variables.
if (empty($block) && !empty($variables['elements']['#block'])) {
$block = $variables['elements']['#block'];
}
// Check that we loaded a block.
if (!empty($block)) {
$styles = $block
->getThirdPartySetting('block_style_plugins', $this->pluginId);
if ($styles) {
$this
->setConfiguration($styles);
}
}
}
return $styles;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockStyleBase:: |
protected | property | Instance of the Entity Type Manager service. | |
BlockStyleBase:: |
protected | property |
Plugin ID for the Block being configured. Overrides PluginBase:: |
|
BlockStyleBase:: |
public | function |
Builds and returns the renderable array for this block style plugin. Overrides BlockStyleInterface:: |
|
BlockStyleBase:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
4 |
BlockStyleBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
BlockStyleBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
5 |
BlockStyleBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
BlockStyleBase:: |
protected | function | Get styles for a block set in a preprocess $variables array. | |
BlockStyleBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
BlockStyleBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
BlockStyleBase:: |
public | function |
Add theme suggestions for the block. Overrides BlockStyleInterface:: |
1 |
BlockStyleBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
BlockStyleBase:: |
public | function |
Construct method for BlockStyleBase. Overrides PluginBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |