Skip to content
Snippets Groups Projects
Commit fa651336 authored by Justin Toupin's avatar Justin Toupin
Browse files

Issue #3468168: Support altering defaults for new paragraphs

parent 48701094
No related branches found
No related tags found
1 merge request!179Adds a LayoutParagraphsComponentDefaultsEvent and dipatches it from relevant...
Pipeline #267523 passed with warnings
...@@ -59,7 +59,7 @@ layout_paragraphs.builder.edit_item: ...@@ -59,7 +59,7 @@ layout_paragraphs.builder.edit_item:
_layout_paragraphs_builder_access: 'TRUE' _layout_paragraphs_builder_access: 'TRUE'
layout_paragraphs.builder.insert: layout_paragraphs.builder.insert:
path: '/layout-paragraphs-builder/{layout_paragraphs_layout}/insert/{paragraph_type}' path: '/layout-paragraphs-builder/{layout_paragraphs_layout}/insert/{paragraph_type_id}'
defaults: defaults:
_controller: '\Drupal\layout_paragraphs\Controller\ComponentFormController::insertForm' _controller: '\Drupal\layout_paragraphs\Controller\ComponentFormController::insertForm'
operation: 'create' operation: 'create'
...@@ -67,8 +67,6 @@ layout_paragraphs.builder.insert: ...@@ -67,8 +67,6 @@ layout_paragraphs.builder.insert:
parameters: parameters:
layout_paragraphs_layout: layout_paragraphs_layout:
layout_paragraphs_layout_tempstore: TRUE layout_paragraphs_layout_tempstore: TRUE
paragraph_type:
type: entity:paragraphs_type
requirements: requirements:
_layout_paragraphs_builder_access: 'TRUE' _layout_paragraphs_builder_access: 'TRUE'
......
...@@ -13,7 +13,8 @@ use Symfony\Component\HttpFoundation\Request; ...@@ -13,7 +13,8 @@ use Symfony\Component\HttpFoundation\Request;
use Drupal\layout_paragraphs\LayoutParagraphsLayout; use Drupal\layout_paragraphs\LayoutParagraphsLayout;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Drupal\layout_paragraphs\Event\LayoutParagraphsComponentDefaultsEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Drupal\layout_paragraphs\LayoutParagraphsLayoutRefreshTrait; use Drupal\layout_paragraphs\LayoutParagraphsLayoutRefreshTrait;
use Drupal\layout_paragraphs\Event\LayoutParagraphsAllowedTypesEvent; use Drupal\layout_paragraphs\Event\LayoutParagraphsAllowedTypesEvent;
...@@ -38,7 +39,7 @@ class ChooseComponentController extends ControllerBase { ...@@ -38,7 +39,7 @@ class ChooseComponentController extends ControllerBase {
/** /**
* The event dispatcher service. * The event dispatcher service.
* *
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/ */
protected $eventDispatcher; protected $eventDispatcher;
...@@ -47,12 +48,12 @@ class ChooseComponentController extends ControllerBase { ...@@ -47,12 +48,12 @@ class ChooseComponentController extends ControllerBase {
* *
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info service. * The entity type bundle info service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher_service
* The event dispatcher service. * The event dispatcher service.
*/ */
public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, EventDispatcherInterface $event_dispatcher) { public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, EventDispatcherInterface $event_dispatcher_service) {
$this->entityTypeBundleInfo = $entity_type_bundle_info; $this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->eventDispatcher = $event_dispatcher; $this->eventDispatcher = $event_dispatcher_service;
} }
/** /**
...@@ -114,7 +115,16 @@ class ChooseComponentController extends ControllerBase { ...@@ -114,7 +115,16 @@ class ChooseComponentController extends ControllerBase {
* An ajax response or form render array. * An ajax response or form render array.
*/ */
protected function componentForm(string $type_name, LayoutParagraphsLayout $layout_paragraphs_layout, array $context) { protected function componentForm(string $type_name, LayoutParagraphsLayout $layout_paragraphs_layout, array $context) {
$type = $this->entityTypeManager()->getStorage('paragraphs_type')->load($type_name);
// Dispatch a LayoutParagraphsComponentDefaultsEvent to allow other modules
// to alter the paragraph type and default values.
$event = new LayoutParagraphsComponentDefaultsEvent($type_name, []);
$this->eventDispatcher->dispatch($event, $event::EVENT_NAME);
$type = $this
->entityTypeManager()
->getStorage('paragraphs_type')
->load($event->getParagraphTypeId());
$form = $this->formBuilder()->getForm( $form = $this->formBuilder()->getForm(
$this->getInsertComponentFormClass(), $this->getInsertComponentFormClass(),
$layout_paragraphs_layout, $layout_paragraphs_layout,
...@@ -122,7 +132,8 @@ class ChooseComponentController extends ControllerBase { ...@@ -122,7 +132,8 @@ class ChooseComponentController extends ControllerBase {
$context['parent_uuid'], $context['parent_uuid'],
$context['region'], $context['region'],
$context['sibling_uuid'], $context['sibling_uuid'],
$context['placement'] $context['placement'],
$event->getDefaultValues()
); );
if ($this->isAjax()) { if ($this->isAjax()) {
$response = new AjaxResponse(); $response = new AjaxResponse();
...@@ -211,15 +222,13 @@ class ChooseComponentController extends ControllerBase { ...@@ -211,15 +222,13 @@ class ChooseComponentController extends ControllerBase {
* *
* @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout
* The layout object. * The layout object.
* @param string $parent_uuid * @param array $context
* The parent uuid of paragraph we are inserting a component into. * The context to be passed to the event.
* @param string $region
* The region we are inserting a component into.
* *
* @return array[] * @return array[]
* Returns an array of allowed component types. * Returns an array of allowed component types.
*/ */
public function getAllowedComponentTypes(LayoutParagraphsLayout $layout, $context) { public function getAllowedComponentTypes(LayoutParagraphsLayout $layout, array $context) {
// @todo Document and add tests for what is happening here. // @todo Document and add tests for what is happening here.
$component_types = $this->getComponentTypes($layout, $context); $component_types = $this->getComponentTypes($layout, $context);
...@@ -259,7 +268,7 @@ class ChooseComponentController extends ControllerBase { ...@@ -259,7 +268,7 @@ class ChooseComponentController extends ControllerBase {
} }
$route_params = [ $route_params = [
'layout_paragraphs_layout' => $layout->id(), 'layout_paragraphs_layout' => $layout->id(),
'paragraph_type' => $paragraphs_type->id(), 'paragraph_type_id' => $paragraphs_type->id(),
]; ];
$query_params = $context; $query_params = $context;
$types[$bundle] = [ $types[$bundle] = [
......
...@@ -6,10 +6,12 @@ use Drupal\Core\Ajax\AjaxResponse; ...@@ -6,10 +6,12 @@ use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AjaxHelperTrait; use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Ajax\OpenDialogCommand; use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\layout_paragraphs\Utility\Dialog;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\paragraphs\ParagraphsTypeInterface;
use Drupal\layout_paragraphs\LayoutParagraphsLayout; use Drupal\layout_paragraphs\LayoutParagraphsLayout;
use Drupal\layout_paragraphs\Utility\Dialog; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\layout_paragraphs\Event\LayoutParagraphsComponentDefaultsEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/** /**
* Class definition for ComponentFormController. * Class definition for ComponentFormController.
...@@ -18,6 +20,32 @@ class ComponentFormController extends ControllerBase { ...@@ -18,6 +20,32 @@ class ComponentFormController extends ControllerBase {
use AjaxHelperTrait; use AjaxHelperTrait;
/**
* The event dispatcher service.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Construct a Component Form Controller.
*
* @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher_service
* The event dispatcher service.
*/
public function __construct(EventDispatcherInterface $event_dispatcher_service) {
$this->eventDispatcher = $event_dispatcher_service;
}
/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('event_dispatcher')
);
}
/** /**
* Responds with a component insert form. * Responds with a component insert form.
* *
...@@ -25,20 +53,37 @@ class ComponentFormController extends ControllerBase { ...@@ -25,20 +53,37 @@ class ComponentFormController extends ControllerBase {
* The request. * The request.
* @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout
* The layout paragraphs layout object. * The layout paragraphs layout object.
* @param \Drupal\paragraphs\ParagraphsTypeInterface $paragraph_type * @param string $paragraph_type_id
* The Paragraph Type to insert. * The paragraph type id.
* *
* @return array|\Drupal\Core\Ajax\AjaxResponse * @return array|\Drupal\Core\Ajax\AjaxResponse
* A build array or Ajax respone. * A build array or Ajax respone.
*/ */
public function insertForm(Request $request, LayoutParagraphsLayout $layout_paragraphs_layout, ParagraphsTypeInterface $paragraph_type) { public function insertForm(Request $request, LayoutParagraphsLayout $layout_paragraphs_layout, string $paragraph_type_id) {
$parent_uuid = $request->query->get('parent_uuid'); $parent_uuid = $request->query->get('parent_uuid');
$region = $request->query->get('region'); $region = $request->query->get('region');
$sibling_uuid = $request->query->get('sibling_uuid'); $sibling_uuid = $request->query->get('sibling_uuid');
$placement = $request->query->get('placement'); $placement = $request->query->get('placement');
$form = $this->formBuilder()->getForm('\Drupal\layout_paragraphs\Form\InsertComponentForm', $layout_paragraphs_layout, $paragraph_type, $parent_uuid, $region, $sibling_uuid, $placement); // Dispatch a LayoutParagraphsComponentDefaultsEvent to allow other modules
// to alter the paragraph type and default values.
$event = new LayoutParagraphsComponentDefaultsEvent($paragraph_type_id, []);
$this->eventDispatcher->dispatch($event, $event::EVENT_NAME);
$paragraph_type = $this->entityTypeManager()
->getStorage('paragraphs_type')
->load($event->getParagraphTypeId());
$form = $this->formBuilder()->getForm(
$this->getInsertComponentFormClass(),
$layout_paragraphs_layout,
$paragraph_type,
$parent_uuid,
$region,
$sibling_uuid,
$placement,
$event->getDefaultValues());
return $this->openForm($form, $layout_paragraphs_layout); return $this->openForm($form, $layout_paragraphs_layout);
} }
...@@ -63,4 +108,11 @@ class ComponentFormController extends ControllerBase { ...@@ -63,4 +108,11 @@ class ComponentFormController extends ControllerBase {
return $form; return $form;
} }
/**
* Returns the insert component form class.
*/
protected function getInsertComponentFormClass() {
return '\Drupal\layout_paragraphs\Form\InsertComponentForm';
}
} }
<?php
namespace Drupal\layout_paragraphs\Event;
use Drupal\Component\EventDispatcher\Event;
/**
* An event for altering the type and default values for new components.
*/
class LayoutParagraphsComponentDefaultsEvent extends Event {
// This makes it easier for subscribers to reliably use our event name.
const EVENT_NAME = 'layout_paragraphs_component_defaults';
/**
* Constructs the object.
*
* @param string $paragraphTypeId
* The paragraph type.
* @param array $defaultValues
* The default values for the paragraph.
*/
public function __construct(
protected string $paragraphTypeId,
protected array $defaultValues) {
}
/**
* Sets the paragraph type.
*
* @param string $paragraph_type_id
* The paragraph type.
*
* @return $this
*/
public function setParagraphTypeId(string $paragraph_type_id): self {
$this->paragraphTypeId = $paragraph_type_id;
return $this;
}
/**
* Gets the paragraph type.
*
* @return string
* The paragraph type.
*/
public function getParagraphTypeId(): string {
return $this->paragraphTypeId;
}
/**
* Sets the default values for the paragraph.
*
* @param array $defaultValues
* The default values for the paragraph.
*
* @return $this
*/
public function setDefaultValues(array $defaultValues): self {
$this->defaultValues = $defaultValues;
return $this;
}
/**
* Gets the default values for the paragraph.
*
* @return array
* The default values for the paragraph.
*/
public function getDefaultValues(): array {
return $this->defaultValues;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment