gin_lp-1.0.x-dev/src/Controller/GinLpChooseComponentController.php
src/Controller/GinLpChooseComponentController.php
<?php
namespace Drupal\gin_lp\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenOffCanvasDialogCommand;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\layout_paragraphs\Utility\Dialog;
use Symfony\Component\HttpFoundation\Request;
use Drupal\layout_paragraphs\LayoutParagraphsLayout;
use Drupal\layout_paragraphs\Controller\ChooseComponentController;
/**
* ChooseComponentController controller class.
*
* Returns a list of links for available component types that can
* be added to a layout region.
*/
class GinLpChooseComponentController extends ChooseComponentController {
/**
* Returns the insert component form class.
*/
protected function getInsertComponentFormClass() {
return '\Drupal\gin_lp\Form\GinLpInsertComponentForm';
}
/**
* Returns a layout paragraphs component form using Ajax if appropriate.
*
* @param string $type_name
* The component (paragraph) type.
* @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout
* The layout paragraphs layout object.
* @param array $context
* The context for the new component.
*
* @return \Drupal\Core\Ajax\AjaxResponse|array
* An ajax response or form render array.
*/
protected function componentForm(string $type_name, LayoutParagraphsLayout $layout_paragraphs_layout, array $context) {
$type = $this->entityTypeManager()->getStorage('paragraphs_type')->load($type_name);
$form = $this->formBuilder()->getForm(
$this->getInsertComponentFormClass(),
$layout_paragraphs_layout,
$type,
$context['parent_uuid'],
$context['region'],
$context['sibling_uuid'],
$context['placement']
);
if ($this->isAjax()) {
$response = new AjaxResponse();
$selector = Dialog::dialogSelector($layout_paragraphs_layout);
$response->addCommand(new OpenOffCanvasDialogCommand($form['#title'], $form, ['width' => 600]));
return $response;
}
return $form;
}
}
