diff --git a/layout_paragraphs.routing.yml b/layout_paragraphs.routing.yml index ca8361e7eee0df9e591106fdebec58f15ba6116d..80c6c4fb893b3a7f3ccc5f50c4005cb525499e9d 100644 --- a/layout_paragraphs.routing.yml +++ b/layout_paragraphs.routing.yml @@ -59,7 +59,7 @@ layout_paragraphs.builder.edit_item: _layout_paragraphs_builder_access: 'TRUE' 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: _controller: '\Drupal\layout_paragraphs\Controller\ComponentFormController::insertForm' operation: 'create' @@ -67,8 +67,6 @@ layout_paragraphs.builder.insert: parameters: layout_paragraphs_layout: layout_paragraphs_layout_tempstore: TRUE - paragraph_type: - type: entity:paragraphs_type requirements: _layout_paragraphs_builder_access: 'TRUE' diff --git a/src/Controller/ChooseComponentController.php b/src/Controller/ChooseComponentController.php index a8b4376d5d47b14a1d05a34e348fd62ce2b82404..222cf69e9a2f91f39384660e35845a2d08ea3568 100644 --- a/src/Controller/ChooseComponentController.php +++ b/src/Controller/ChooseComponentController.php @@ -13,7 +13,8 @@ use Symfony\Component\HttpFoundation\Request; use Drupal\layout_paragraphs\LayoutParagraphsLayout; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; 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\Event\LayoutParagraphsAllowedTypesEvent; @@ -38,7 +39,7 @@ class ChooseComponentController extends ControllerBase { /** * The event dispatcher service. * - * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */ protected $eventDispatcher; @@ -47,12 +48,12 @@ class ChooseComponentController extends ControllerBase { * * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * 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. */ - 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->eventDispatcher = $event_dispatcher; + $this->eventDispatcher = $event_dispatcher_service; } /** @@ -114,7 +115,16 @@ class ChooseComponentController extends ControllerBase { * 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); + + // 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( $this->getInsertComponentFormClass(), $layout_paragraphs_layout, @@ -122,7 +132,8 @@ class ChooseComponentController extends ControllerBase { $context['parent_uuid'], $context['region'], $context['sibling_uuid'], - $context['placement'] + $context['placement'], + $event->getDefaultValues() ); if ($this->isAjax()) { $response = new AjaxResponse(); @@ -211,15 +222,13 @@ class ChooseComponentController extends ControllerBase { * * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout * The layout object. - * @param string $parent_uuid - * The parent uuid of paragraph we are inserting a component into. - * @param string $region - * The region we are inserting a component into. + * @param array $context + * The context to be passed to the event. * * @return array[] * 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. $component_types = $this->getComponentTypes($layout, $context); @@ -259,7 +268,7 @@ class ChooseComponentController extends ControllerBase { } $route_params = [ 'layout_paragraphs_layout' => $layout->id(), - 'paragraph_type' => $paragraphs_type->id(), + 'paragraph_type_id' => $paragraphs_type->id(), ]; $query_params = $context; $types[$bundle] = [ diff --git a/src/Controller/ComponentFormController.php b/src/Controller/ComponentFormController.php index 91274ba73b161c8fb3bd1631796d393adb594569..0fd36ff3b857097d3b54828cce1091f33536a1e8 100644 --- a/src/Controller/ComponentFormController.php +++ b/src/Controller/ComponentFormController.php @@ -6,10 +6,12 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AjaxHelperTrait; use Drupal\Core\Ajax\OpenDialogCommand; use Drupal\Core\Controller\ControllerBase; +use Drupal\layout_paragraphs\Utility\Dialog; use Symfony\Component\HttpFoundation\Request; -use Drupal\paragraphs\ParagraphsTypeInterface; 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. @@ -18,6 +20,32 @@ class ComponentFormController extends ControllerBase { 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. * @@ -25,20 +53,37 @@ class ComponentFormController extends ControllerBase { * The request. * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout * The layout paragraphs layout object. - * @param \Drupal\paragraphs\ParagraphsTypeInterface $paragraph_type - * The Paragraph Type to insert. + * @param string $paragraph_type_id + * The paragraph type id. * * @return array|\Drupal\Core\Ajax\AjaxResponse * 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'); $region = $request->query->get('region'); $sibling_uuid = $request->query->get('sibling_uuid'); $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); } @@ -63,4 +108,11 @@ class ComponentFormController extends ControllerBase { return $form; } + /** + * Returns the insert component form class. + */ + protected function getInsertComponentFormClass() { + return '\Drupal\layout_paragraphs\Form\InsertComponentForm'; + } + } diff --git a/src/Event/LayoutParagraphsComponentDefaultsEvent.php b/src/Event/LayoutParagraphsComponentDefaultsEvent.php new file mode 100644 index 0000000000000000000000000000000000000000..200d0d0c9281413fc07c7f686aeea83621e21c29 --- /dev/null +++ b/src/Event/LayoutParagraphsComponentDefaultsEvent.php @@ -0,0 +1,74 @@ +<?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; + } + +}