View source
<?php
namespace Drupal\ds\Form;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockFieldConfigForm extends FieldFormBase implements ContainerInjectionInterface {
use ContextAwarePluginAssignmentTrait;
protected $contextRepository;
public function __construct(ConfigFactory $config_factory, EntityTypeManagerInterface $entity_type_manager, CacheTagsInvalidatorInterface $cache_invalidator, ModuleHandler $module_handler, ContextRepositoryInterface $context_repository) {
parent::__construct($config_factory, $entity_type_manager, $cache_invalidator, $module_handler);
$this->contextRepository = $context_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('cache_tags.invalidator'), $container
->get('module_handler'), $container
->get('context.repository'));
}
public function buildForm(array $form, FormStateInterface $form_state, $field_key = '') {
$field = $this
->config('ds.field.' . $field_key)
->get();
$default_theme = $this
->config('system.theme')
->get('default');
$form_state
->set('block_theme', $default_theme);
$this->field = $field;
$manager = \Drupal::service('plugin.manager.block');
$block_id = $field['properties']['block'];
$block = $manager
->createInstance($block_id);
if (isset($field['properties']['config'])) {
$block
->setConfiguration($field['properties']['config']);
}
$form = $block
->blockForm($form, $form_state);
if ($block instanceof ContextAwarePluginInterface) {
$form['context_mapping'] = $this
->addContextAssignmentElement($block, $this->contextRepository
->getAvailableContexts());
}
if (!$form) {
return [
'#markup' => $this
->t("This block has no configuration options."),
];
}
$form['#tree'] = TRUE;
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#weight' => 100,
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$field = $this->field;
$manager = \Drupal::service('plugin.manager.block');
$block_id = $field['properties']['block'];
$block = $manager
->createInstance($block_id);
$block
->validateConfigurationForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$field = $this->field;
$manager = \Drupal::service('plugin.manager.block');
$block_id = $field['properties']['block'];
$block = $manager
->createInstance($block_id);
$block
->blockSubmit($form, $form_state);
if ($block instanceof ContextAwarePluginInterface && $block
->getContextDefinitions()) {
$context_mapping = $form_state
->getValue('context_mapping', []);
$block
->setContextMapping($context_mapping);
}
$block_config = $block
->getConfiguration();
$this->cacheInvalidator
->invalidateTags($block
->getCacheTags());
$this
->config('ds.field.' . $field['id'])
->set('properties.config', $block_config)
->save();
$this
->finishSubmitForm($form, $form_state);
}
}