DeleteStyles.php in Block Style Plugins 8.2
File
src/Form/DeleteStyles.php
View source
<?php
namespace Drupal\block_style_plugins\Form;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenOffCanvasDialogCommand;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\layout_builder\Controller\LayoutRebuildTrait;
use Drupal\layout_builder\LayoutBuilderHighlightTrait;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DeleteStyles extends ConfirmFormBase {
use AjaxFormHelperTrait;
use LayoutBuilderHighlightTrait;
use LayoutRebuildTrait;
protected $layoutTempstoreRepository;
protected $formBuilder;
protected $sectionStorage;
protected $delta;
protected $uuid;
protected $pluginId;
public function __construct(FormBuilderInterface $form_builder, LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
$this->formBuilder = $form_builder;
$this->layoutTempstoreRepository = $layout_tempstore_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('form_builder'), $container
->get('layout_builder.tempstore_repository'));
}
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $uuid = NULL, $plugin_id = NULL) {
$this->sectionStorage = $section_storage;
$this->delta = $delta;
$this->uuid = $uuid;
$this->pluginId = $plugin_id;
$form = parent::buildForm($form, $form_state);
$form['actions']['cancel'] = $this
->buildCancelLink();
$form['#attributes']['data-layout-builder-target-highlight-id'] = $this
->blockUpdateHighlightId($this->uuid);
if ($this
->isAjax()) {
$form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
}
return $form;
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete this style?');
}
public function getCancelUrl() {
$parameters = $this
->getParameters();
return new Url('block_style_plugins.layout_builder.styles', $parameters);
}
public function getFormId() {
return 'block_style_plugins_layout_builder_delete_styles';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$component = $this->sectionStorage
->getSection($this->delta)
->getComponent($this->uuid);
$component
->unsetThirdPartySetting('block_style_plugins', $this->pluginId);
$this->layoutTempstoreRepository
->set($this->sectionStorage);
$form_state
->setRedirectUrl($this->sectionStorage
->getLayoutBuilderUrl());
}
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
return $this
->rebuildAndClose($this->sectionStorage);
}
protected function buildCancelLink() {
return [
'#type' => 'button',
'#value' => $this
->getCancelText(),
'#ajax' => [
'callback' => '::ajaxCancel',
],
];
}
public function ajaxCancel(array &$form, FormStateInterface $form_state) {
$parameters = $this
->getParameters();
$new_form = $this->formBuilder
->getForm(BlockStyleForm::class, $this->sectionStorage, $parameters['delta'], $parameters['uuid']);
$new_form['#action'] = $this
->getCancelUrl()
->toString();
$response = new AjaxResponse();
$response
->addCommand(new OpenOffCanvasDialogCommand($this
->t('Configure Styles'), $new_form));
return $response;
}
protected function getParameters() {
return [
'section_storage_type' => $this->sectionStorage
->getStorageType(),
'section_storage' => $this->sectionStorage
->getStorageId(),
'delta' => $this->delta,
'uuid' => $this->uuid,
];
}
}