View source
<?php
namespace Drupal\block_style_plugins\Plugin;
use Drupal\Core\Form\SubformState;
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\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
abstract class BlockStyleBase extends PluginBase implements BlockStyleInterface, ContainerFactoryPluginInterface {
protected $pluginId;
protected $blockPlugin;
protected $blockContentBundle;
protected $entityRepository;
protected $entityTypeManager;
protected $styles;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entityRepository, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityRepository = $entityRepository;
$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.repository'), $container
->get('entity_type.manager'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $this
->formElements($form, $form_state);
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function prepareForm(array $form, FormStateInterface $form_state) {
$entity = $form_state
->getFormObject()
->getEntity();
$this->blockPlugin = $entity
->getPlugin();
$this
->setBlockContentBundle();
if ($this
->includeOnly() && !$this
->exclude()) {
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);
$form['third_party_settings']['block_style_plugins'][$this->pluginId] = [
'#type' => 'container',
'#group' => 'block_styles',
];
$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);
$form = $this
->formAlter($form, $form_state);
$form['#validate'][] = [
$this,
'validateForm',
];
array_unshift($form['actions']['submit']['#submit'], [
$this,
'submitForm',
]);
}
return $form;
}
public function formElements($form, FormStateInterface $form_state) {
return [];
}
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;
}
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);
$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);
}
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);
$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);
}
public function build(array $variables) {
$styles = $this
->getStylesFromVariables($variables);
if ($styles) {
$variables['block_styles'][$this->pluginId] = $styles;
foreach ($styles as $class) {
if (is_int($class)) {
continue;
}
$variables['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
->defaultStyles(), $configuration);
$this->styles = $this->configuration;
}
public function defaultStyles() {
return $this
->defaultConfiguration();
}
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();
}
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);
}
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;
}
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;
}
protected function baseIdMatch($plugin_id, array $list) {
preg_match('/^([^:]+):?/', $plugin_id, $matches);
if ($matches && in_array($matches[1] . ':*', $list)) {
return TRUE;
}
return FALSE;
}
public function themeSuggestion(array $suggestions, array $variables) {
return $suggestions;
}
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();
}
}
}
protected function getStylesFromVariables(array $variables) {
if (empty($variables['elements']['#id'])) {
return FALSE;
}
$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;
}
}
}