DeleteComponentForm.php in Layout Paragraphs 2.0.x
File
src/Form/DeleteComponentForm.php
View source
<?php
namespace Drupal\layout_paragraphs\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_paragraphs\Ajax\LayoutParagraphsEventCommand;
use Drupal\layout_paragraphs\DialogHelperTrait;
use Drupal\layout_paragraphs\LayoutParagraphsLayout;
use Drupal\layout_paragraphs\LayoutParagraphsLayoutRefreshTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DeleteComponentForm extends FormBase {
use LayoutParagraphsLayoutRefreshTrait;
use DialogHelperTrait;
protected $tempstore;
protected $componentUuid;
protected function __construct($tempstore) {
$this->tempstore = $tempstore;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_paragraphs.tempstore_repository'));
}
public function getFormId() {
return 'layout_paragraphs_delete_component_form';
}
public function buildForm(array $form, FormStateInterface $form_state, LayoutParagraphsLayout $layout_paragraphs_layout = NULL, string $component_uuid = NULL) {
$this
->setLayoutParagraphsLayout($layout_paragraphs_layout);
$this->componentUuid = $component_uuid;
$component = $this->layoutParagraphsLayout
->getComponentByUuid($this->componentUuid);
$type = $component
->getEntity()
->getParagraphType()
->label();
$form['#title'] = $this
->t('Delete @type', [
'@type' => $type,
]);
$form['confirm'] = [
'#markup' => $this
->t('Really delete this @type? There is no undo.', [
'@type' => $type,
]),
];
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this
->t('Delete'),
'#ajax' => [
'callback' => '::deleteComponent',
],
],
'cancel' => [
'#type' => 'button',
'#value' => $this
->t('Cancel'),
'#ajax' => [
'callback' => '::closeForm',
],
],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->layoutParagraphsLayout
->deleteComponent($this->componentUuid, TRUE);
$this->tempstore
->set($this->layoutParagraphsLayout);
}
public function deleteComponent(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new CloseDialogCommand($this
->dialogSelector($this->layoutParagraphsLayout)));
if ($this
->needsRefresh()) {
return $this
->refreshLayout($response);
}
$response
->addCommand(new RemoveCommand('[data-uuid="' . $this->componentUuid . '"]'));
$response
->addCommand(new LayoutParagraphsEventCommand($this->layoutParagraphsLayout, $this->componentUuid, 'component:delete'));
return $response;
}
public function closeForm(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand($this
->closeDialogCommand($this->layoutParagraphsLayout));
return $response;
}
}