InsertComponentForm.php in Layout Paragraphs 2.0.x
File
src/Form/InsertComponentForm.php
View source
<?php
namespace Drupal\layout_paragraphs\Form;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\BeforeCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs\ParagraphsTypeInterface;
use Drupal\layout_paragraphs\LayoutParagraphsLayout;
use Drupal\layout_paragraphs\Ajax\LayoutParagraphsEventCommand;
class InsertComponentForm extends ComponentFormBase {
protected $domSelector;
protected $method = 'prepend';
protected $parentUuid;
protected $region;
protected $placement;
protected $siblingUuid;
public function buildForm(array $form, FormStateInterface $form_state, LayoutParagraphsLayout $layout_paragraphs_layout = NULL, ParagraphsType $paragraph_type = NULL, string $parent_uuid = NULL, string $region = NULL, string $sibling_uuid = NULL, string $placement = NULL) {
$this
->setLayoutParagraphsLayout($layout_paragraphs_layout);
$this->paragraph = $this
->newParagraph($paragraph_type);
$this->parentUuid = $parent_uuid;
$this->region = $region;
$this->siblingUuid = $sibling_uuid;
$this->placement = $placement;
if ($this->siblingUuid && $this->placement) {
$this->domSelector = '[data-uuid="' . $sibling_uuid . '"]';
$this->method = $placement;
}
elseif ($this->parentUuid && $this->region) {
$this->domSelector = '[data-region-uuid="' . $parent_uuid . '-' . $region . '"]';
}
else {
$this->domSelector = '[data-lpb-id="' . $this->layoutParagraphsLayout
->id() . '"]';
}
return $this
->buildComponentForm($form, $form_state);
}
protected function formTitle() {
return $this
->t('Create new @type', [
'@type' => $this->paragraph
->getParagraphType()
->label(),
]);
}
public function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$this
->ajaxCloseForm($response);
if ($this
->needsRefresh()) {
return $this
->refreshLayout($response);
}
$uuid = $this->paragraph
->uuid();
$rendered_item = $this
->renderParagraph($uuid);
switch ($this->method) {
case 'before':
$response
->addCommand(new BeforeCommand($this->domSelector, $rendered_item));
break;
case 'after':
$response
->addCommand(new AfterCommand($this->domSelector, $rendered_item));
break;
case 'append':
$response
->addCommand(new AppendCommand($this->domSelector, $rendered_item));
break;
case 'prepend':
$response
->addCommand(new PrependCommand($this->domSelector, $rendered_item));
break;
}
$response
->addCommand(new LayoutParagraphsEventCommand($this->layoutParagraphsLayout, $uuid, 'component:insert'));
return $response;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->insertComponent();
$this->tempstore
->set($this->layoutParagraphsLayout);
}
protected function insertComponent() {
if ($this->parentUuid && $this->region) {
$this->layoutParagraphsLayout
->insertIntoRegion($this->parentUuid, $this->region, $this->paragraph);
}
elseif ($this->siblingUuid && $this->placement) {
switch ($this->placement) {
case 'before':
$this->layoutParagraphsLayout
->insertBeforeComponent($this->siblingUuid, $this->paragraph);
break;
case 'after':
$this->layoutParagraphsLayout
->insertAfterComponent($this->siblingUuid, $this->paragraph);
break;
}
}
else {
$this->layoutParagraphsLayout
->appendComponent($this->paragraph);
}
return $this;
}
protected function newParagraph(ParagraphsTypeInterface $paragraph_type) {
$entity_type = $this->entityTypeManager
->getDefinition('paragraph');
$bundle_key = $entity_type
->getKey('bundle');
$paragraph = $this->entityTypeManager
->getStorage('paragraph')
->create([
$bundle_key => $paragraph_type
->id(),
]);
return $paragraph;
}
}