View source
<?php
namespace Drupal\draggable_dashboard\Form;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\draggable_dashboard\Entity\DashboardEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DashboardBlockFormBase extends FormBase {
protected $blockManager;
protected $contextRepository;
protected $pluginFormFactory;
protected $dashboard;
protected $block;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->blockManager = $container
->get('plugin.manager.block');
$instance->contextRepository = $container
->get('context.repository');
$instance->pluginFormFactory = $container
->get('plugin_form.factory');
return $instance;
}
protected function init(FormStateInterface $form_state, DashboardEntityInterface $dashboard_entity) {
$form_state
->set('form_initialized', TRUE);
$this->dashboard = $dashboard_entity;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$block_instance = $this->blockManager
->createInstance($this->block['settings']['id'], $this->block['settings']);
$form_state
->setTemporaryValue('gathered_contexts', $this->contextRepository
->getAvailableContexts());
$form['settings'] = [
'#tree' => TRUE,
];
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$form['settings'] = $this
->getPluginForm($block_instance)
->buildConfigurationForm($form['settings'], $subform_state);
if (empty($this->block['settings']['provider'])) {
$form['id'] = [
'#type' => 'machine_name',
'#maxlength' => 64,
'#description' => $this
->t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'),
'#machine_name' => [
'exists' => [
$this,
'blockIdExists',
],
'replace_pattern' => '[^a-z0-9_]+',
'source' => [
'settings',
'label',
],
],
'#required' => TRUE,
'#weight' => -10,
];
if (empty($form['settings']['label']) || !empty($form['settings']['label']['#access']) && !$form['settings']['label']['#access'] && !empty($form['settings']['views_label'])) {
$form['id']['#machine_name']['source'] = [
'settings',
'views_label',
];
}
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $this->dashboard
->toUrl('edit-form'),
];
return $form;
}
protected function getPluginForm(BlockPluginInterface $block) {
if ($block instanceof PluginWithFormsInterface) {
return $this->pluginFormFactory
->createInstance($block, 'configure');
}
return $block;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$block_instance = $this->blockManager
->createInstance($this->block['settings']['id']);
$this
->getPluginForm($block_instance)
->validateConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$block = $this->blockManager
->createInstance($this->block['settings']['id']);
$subform_state = SubformState::createForSubform($form['settings'], $form_state
->getCompleteForm(), $form_state);
$block
->submitConfigurationForm($form['settings'], $subform_state);
if ($block instanceof ContextAwarePluginInterface && $block
->getContextDefinitions()) {
$context_mapping = $subform_state
->getValue('context_mapping', []);
$block
->setContextMapping($context_mapping);
}
$settings = $block
->getConfiguration();
$form_state
->setValue('settings', $settings);
}
}