View source
<?php
namespace Drupal\Core\Block;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Plugin\PluginWithFormsTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
trait BlockPluginTrait {
use StringTranslationTrait;
use MessengerTrait;
use PluginWithFormsTrait;
protected $inPreview = FALSE;
protected $transliteration;
public function label() {
if (!empty($this->configuration['label'])) {
return $this->configuration['label'];
}
$definition = $this
->getPluginDefinition();
return (string) $definition['admin_label'];
}
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->baseConfigurationDefaults(), $this
->defaultConfiguration(), $configuration);
}
protected function baseConfigurationDefaults() {
return [
'id' => $this
->getPluginId(),
'label' => '',
'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
'provider' => $this->pluginDefinition['provider'],
];
}
public function defaultConfiguration() {
return [];
}
public function setConfigurationValue($key, $value) {
$this->configuration[$key] = $value;
}
public function calculateDependencies() {
return [];
}
public function access(AccountInterface $account, $return_as_object = FALSE) {
$access = $this
->blockAccess($account);
return $return_as_object ? $access : $access
->isAllowed();
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowed();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$definition = $this
->getPluginDefinition();
$form['provider'] = [
'#type' => 'value',
'#value' => $definition['provider'],
];
$form['admin_label'] = [
'#type' => 'item',
'#title' => $this
->t('Block description'),
'#plain_text' => $definition['admin_label'],
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#maxlength' => 255,
'#default_value' => $this
->label(),
'#required' => TRUE,
];
$form['label_display'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display title'),
'#default_value' => $this->configuration['label_display'] === BlockPluginInterface::BLOCK_LABEL_VISIBLE,
'#return_value' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
];
$form += $this
->blockForm($form, $form_state);
return $form;
}
public function blockForm($form, FormStateInterface $form_state) {
return [];
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$form_state
->unsetValue('admin_label');
$this
->blockValidate($form, $form_state);
}
public function blockValidate($form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$this->configuration['label'] = $form_state
->getValue('label');
$this->configuration['label_display'] = $form_state
->getValue('label_display');
$this->configuration['provider'] = $form_state
->getValue('provider');
$this
->blockSubmit($form, $form_state);
}
}
public function blockSubmit($form, FormStateInterface $form_state) {
}
public function getMachineNameSuggestion() {
$definition = $this
->getPluginDefinition();
$admin_label = $definition['admin_label'];
$transliterated = $this
->transliteration()
->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_');
$transliterated = mb_strtolower($transliterated);
$transliterated = preg_replace('@[^a-z0-9_.]+@', '', $transliterated);
return $transliterated;
}
public function getPreviewFallbackString() {
return $this
->t('"@block" block', [
'@block' => $this
->label(),
]);
}
protected function transliteration() {
if (!$this->transliteration) {
$this->transliteration = \Drupal::transliteration();
}
return $this->transliteration;
}
public function setTransliteration(TransliterationInterface $transliteration) {
$this->transliteration = $transliteration;
}
public function setInPreview(bool $in_preview) : void {
$this->inPreview = $in_preview;
}
}