abstract class BlockStyleBase in Block Style Plugins 8
Same name and namespace in other branches
- 8.2 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
- 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 17
Namespace
Drupal\block_style_plugins\PluginView source
abstract class BlockStyleBase extends PluginBase implements BlockStyleInterface, ContainerFactoryPluginInterface {
/**
* Plugin ID for the Block being configured.
*
* @var string
*/
protected $pluginId;
/**
* Plugin instance for the Block being configured.
*
* @var object
*/
protected $blockPlugin;
/**
* Bundle type for 'Block Content' blocks.
*
* @var string
*/
protected $blockContentBundle;
/**
* Instance of the Entity Repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Instance of the Entity Type Manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Style settings for the block styles.
*
* @var array
*
* @deprecated in 8.x-1.3 and will be removed before 8.x-2.x.
* Instead, you should just use $configuration.
*/
protected $styles;
/**
* 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\EntityRepositoryInterface $entityRepository
* An Entity Repository instance.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* An Entity Type Manager instance.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entityRepository, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
// Store our dependencies.
$this->entityRepository = $entityRepository;
$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.repository'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// TODO: replace deprecated formElements() with empty array before 8.x-2.x.
return $this
->formElements($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function prepareForm(array $form, FormStateInterface $form_state) {
// Get the current block config entity.
/** @var \Drupal\block\Entity\Block $entity */
$entity = $form_state
->getFormObject()
->getEntity();
// Set properties and configuration.
$this->blockPlugin = $entity
->getPlugin();
$this
->setBlockContentBundle();
// Check to see if this should only apply to includes or if it has been
// excluded.
if ($this
->includeOnly() && !$this
->exclude()) {
// Create a fieldset to contain style fields.
if (!isset($form['block_styles'])) {
$form['block_styles'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Block Styles'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#weight' => 0,
];
}
$styles = $entity
->getThirdPartySetting('block_style_plugins', $this->pluginId);
$styles = is_array($styles) ? $styles : [];
$this
->setConfiguration($styles);
// Create containers to place each plugin style settings into the styles
// fieldset.
$form['third_party_settings']['block_style_plugins'][$this->pluginId] = [
'#type' => 'container',
'#group' => 'block_styles',
];
// Allow plugins to add field elements to this form.
$subform_state = SubformState::createForSubform($form['third_party_settings']['block_style_plugins'][$this->pluginId], $form, $form_state);
$form['third_party_settings']['block_style_plugins'][$this->pluginId] += $this
->buildConfigurationForm($form['third_party_settings']['block_style_plugins'][$this->pluginId], $subform_state);
// Allow plugins to alter this form.
$form = $this
->formAlter($form, $form_state);
// Add form Validation.
$form['#validate'][] = [
$this,
'validateForm',
];
// Add the submitForm method to the form.
array_unshift($form['actions']['submit']['#submit'], [
$this,
'submitForm',
]);
}
return $form;
}
/**
* Returns an array of field elements.
*
* @deprecated in 8.x-1.3 and will be removed before 8.x-2.x.
* Instead, you should just use buildConfigurationForm().
*/
public function formElements($form, FormStateInterface $form_state) {
return [];
}
/**
* {@inheritdoc}
*
* @deprecated in 8.x-1.4 and will be removed before 8.x-2.x.
* Instead, you should just use buildConfigurationForm().
*/
public function formAlter(array $form, FormStateInterface $form_state) {
@trigger_error('::formAlter() is deprecated in 8.x-1.4 and will be removed before 8.x-2.x. Instead, you should just use buildConfigurationForm(). See https://www.drupal.org/project/block_style_plugins/issues/3020109.', E_USER_DEPRECATED);
return $form;
}
/**
* Adds block style specific validation handling for the block form.
*
* TODO: Add this to the BlockStyleInterface before 8.x-2.x.
*
* @param array $form
* The form definition array for the full block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @deprecated in 8.x-1.4 and will be removed before 8.x-2.x.
* Instead, you should just use validateConfigurationForm().
*/
public function validateForm(array $form, FormStateInterface $form_state) {
@trigger_error('::validateForm() is deprecated in 8.x-1.4 and will be removed before 8.x-2.x. Instead, you should just use validateConfigurationForm(). See https://www.drupal.org/project/block_style_plugins/issues/3020109.', E_USER_DEPRECATED);
// Allow plugins to manipulate the validateForm.
$subform_state = SubformState::createForSubform($form['third_party_settings']['block_style_plugins'][$this->pluginId], $form, $form_state);
$this
->validateConfigurationForm($form['third_party_settings']['block_style_plugins'][$this->pluginId], $subform_state);
}
/**
* {@inheritdoc}
*
* @deprecated in 8.x-1.4 and will be removed before 8.x-2.x.
* Instead, you should just use submitConfigurationForm().
*/
public function submitForm($form, FormStateInterface $form_state) {
@trigger_error('::submitForm() is deprecated in 8.x-1.4 and will be removed before 8.x-2.x. Instead, you should just use submitConfigurationForm(). See https://www.drupal.org/project/block_style_plugins/issues/3020109.', E_USER_DEPRECATED);
// Allow plugins to manipulate the submitForm.
$subform_state = SubformState::createForSubform($form['third_party_settings']['block_style_plugins'][$this->pluginId], $form, $form_state);
$this
->submitConfigurationForm($form['third_party_settings']['block_style_plugins'][$this->pluginId], $subform_state);
}
/**
* {@inheritdoc}
*/
public function build(array $variables) {
$styles = $this
->getStylesFromVariables($variables);
if ($styles) {
// Add all styles config to the $variables array.
$variables['block_styles'][$this->pluginId] = $styles;
// Add each style value as a class.
foreach ($styles as $class) {
// Don't put a boolean from a checkbox as a class.
if (is_int($class)) {
continue;
}
$variables['attributes']['class'][] = $class;
}
}
return $variables;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
// TODO: Replace the deprecated defaultStyles() with defaultConfiguration()
// before 8.x-2.x.
$this->configuration = NestedArray::mergeDeep($this
->defaultStyles(), $configuration);
// Set the deprecated $styles property.
// TODO: Remove the deprecated $styles setting before 8.x-2.x.
$this->styles = $this->configuration;
}
/**
* Gets default style configuration for this plugin.
*
* @deprecated in 8.x-1.3 and will be removed before 8.x-2.x.
* Instead, you should just use defaultConfiguration().
*/
public function defaultStyles() {
return $this
->defaultConfiguration();
}
/**
* Gets this plugin's style configuration.
*
* @deprecated in 8.x-1.3 and will be removed before 8.x-2.x.
* Instead, you should just use getConfiguration().
*/
public function getStyles() {
@trigger_error('::getStyles() is deprecated in 8.x-1.3 and will be removed before 8.x-2.x. Instead, you should just use getConfiguration(). See https://www.drupal.org/project/block_style_plugins/issues/3016288.', E_USER_DEPRECATED);
return $this
->getConfiguration();
}
/**
* Sets the style configuration for this plugin instance.
*
* @deprecated in 8.x-1.3 and will be removed before 8.x-2.x.
* Instead, you should just use setConfiguration().
*/
public function setStyles(array $styles) {
@trigger_error('::setStyles() is deprecated in 8.x-1.3 and will be removed before 8.x-2.x. Instead, you should just use setConfiguration(). See https://www.drupal.org/project/block_style_plugins/issues/3016288.', E_USER_DEPRECATED);
$this
->setConfiguration($styles);
}
/**
* {@inheritdoc}
*/
public function exclude() {
$list = [];
if (isset($this->pluginDefinition['exclude'])) {
$list = $this->pluginDefinition['exclude'];
}
$block_plugin_id = $this->blockPlugin
->getPluginId();
if (!empty($list) && (in_array($block_plugin_id, $list) || $this
->baseIdMatch($block_plugin_id, $list) || in_array($this->blockContentBundle, $list))) {
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function includeOnly() {
$list = [];
if (isset($this->pluginDefinition['include'])) {
$list = $this->pluginDefinition['include'];
}
$block_plugin_id = $this->blockPlugin
->getPluginId();
if (empty($list) || (in_array($block_plugin_id, $list) || $this
->baseIdMatch($block_plugin_id, $list) || in_array($this->blockContentBundle, $list))) {
return TRUE;
}
return FALSE;
}
/**
* Determine if a plugin ID matches a Base ID in a list of include/exclude.
*
* @param string $plugin_id
* Plugin ID of a block.
* @param array $list
* List of include/exclude blocks.
*
* @return bool
* True if a plugin matches a base ID.
*/
protected function baseIdMatch($plugin_id, array $list) {
// Now check to see if this ID is a derivative on something in the list.
preg_match('/^([^:]+):?/', $plugin_id, $matches);
if ($matches && in_array($matches[1] . ':*', $list)) {
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function themeSuggestion(array $suggestions, array $variables) {
return $suggestions;
}
/**
* Set the block content bundle type.
*/
public function setBlockContentBundle() {
$base_id = $this->blockPlugin
->getBaseId();
$uuid = $this->blockPlugin
->getDerivativeId();
if ($base_id == 'block_content') {
$plugin = $this->entityRepository
->loadEntityByUuid('block_content', $uuid);
if ($plugin) {
$this->blockContentBundle = $plugin
->bundle();
}
}
}
/**
* 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) {
// Ensure that we have a block id.
if (empty($variables['elements']['#id'])) {
return FALSE;
}
// Load the block config entity.
/** @var \Drupal\block\Entity\Block $block */
$block = $this->entityTypeManager
->getStorage('block')
->load($variables['elements']['#id']);
$styles = $block
->getThirdPartySetting('block_style_plugins', $this->pluginId);
if ($styles) {
$this
->setConfiguration($styles);
return $styles;
}
else {
return FALSE;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockStyleBase:: |
protected | property | Bundle type for 'Block Content' blocks. | |
BlockStyleBase:: |
protected | property | Plugin instance for the Block being configured. | |
BlockStyleBase:: |
protected | property | Instance of the Entity Repository service. | |
BlockStyleBase:: |
protected | property | Instance of the Entity Type Manager service. | |
BlockStyleBase:: |
protected | property |
Plugin ID for the Block being configured. Overrides PluginBase:: |
|
BlockStyleBase:: |
protected | property | Style settings for the block styles. | |
BlockStyleBase:: |
protected | function | Determine if a plugin ID matches a Base ID in a list of include/exclude. | |
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 default style configuration for this plugin. | |
BlockStyleBase:: |
public | function |
Exclude styles from appearing on a block. Overrides BlockStyleInterface:: |
|
BlockStyleBase:: |
public | function |
Overrides BlockStyleInterface:: |
|
BlockStyleBase:: |
public | function | Returns an array of field elements. | |
BlockStyleBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
BlockStyleBase:: |
public | function | Gets this plugin's style configuration. | |
BlockStyleBase:: |
protected | function | Get styles for a block set in a preprocess $variables array. | |
BlockStyleBase:: |
public | function |
Only show styles on specific blocks. Overrides BlockStyleInterface:: |
|
BlockStyleBase:: |
public | function |
Returns the configuration form elements specific to a block configuration. Overrides BlockStyleInterface:: |
|
BlockStyleBase:: |
public | function | Set the block content bundle type. | |
BlockStyleBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
BlockStyleBase:: |
public | function | Sets the style configuration for this plugin instance. | |
BlockStyleBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
BlockStyleBase:: |
public | function |
Overrides BlockStyleInterface:: |
|
BlockStyleBase:: |
public | function |
Add theme suggestions for the block. Overrides BlockStyleInterface:: |
1 |
BlockStyleBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
BlockStyleBase:: |
public | function | Adds block style specific validation handling for the block form. | |
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. |