BlockStyleBase.php in Block Style Plugins 8.2
File
src/Plugin/BlockStyleBase.php
View source
<?php
namespace Drupal\block_style_plugins\Plugin;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\PluginFormInterface;
abstract class BlockStyleBase extends PluginBase implements BlockStyleInterface, ContainerFactoryPluginInterface, PluginFormInterface {
protected $pluginId;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->pluginId = $plugin_id;
}
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'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return [];
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function build(array $variables) {
$layout_builder_block = empty($variables['elements']['#id']) && empty($variables['layout']) ? TRUE : FALSE;
$styles = $this
->getStylesFromVariables($variables);
if (!$styles) {
return $variables;
}
$hash = $layout_builder_block ? '#' : '';
$variables[$hash . 'configuration']['block_styles'][$this->pluginId] = $styles;
if (empty($this->pluginDefinition['disable_auto_classes'])) {
foreach ($styles as $class) {
if (is_int($class)) {
continue;
}
if (!isset($variables[$hash . 'attributes']['class'])) {
$variables[$hash . 'attributes']['class'] = [];
}
if (!is_array($class)) {
$class = [
$class,
];
}
$variables[$hash . 'attributes']['class'] = array_merge($variables[$hash . 'attributes']['class'], $class);
}
}
return $variables;
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function themeSuggestion(array $suggestions, array $variables) {
return $suggestions;
}
protected function getStylesFromVariables(array $variables) {
$styles = FALSE;
if (empty($variables['elements']['#id'])) {
$styles = $this
->getConfiguration();
if (empty($styles) && isset($variables['elements']['#configuration']['block_styles'][$this->pluginId])) {
$this
->setConfiguration($variables['elements']['#configuration']['block_styles'][$this->pluginId]);
$styles = $this
->getConfiguration();
}
}
else {
$block = $this->entityTypeManager
->getStorage('block')
->load($variables['elements']['#id']);
if (empty($block) && !empty($variables['elements']['#block'])) {
$block = $variables['elements']['#block'];
}
if (!empty($block)) {
$styles = $block
->getThirdPartySetting('block_style_plugins', $this->pluginId);
if ($styles) {
$this
->setConfiguration($styles);
}
}
}
return $styles;
}
}