3 namespace Drupal\layout_builder\Routing;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\layout_builder\OverridesSectionStorageInterface;
7 use Drupal\layout_builder\SectionStorage\SectionStorageDefinition;
8 use Symfony\Component\Routing\Route;
9 use Symfony\Component\Routing\RouteCollection;
12 * Provides a trait for building routes for a Layout Builder UI.
15 * Layout Builder is currently experimental and should only be leveraged by
16 * experimental modules and development releases of contributed modules.
17 * See https://www.drupal.org/core/experimental for more information.
19 trait LayoutBuilderRoutesTrait {
22 * Builds the layout routes for the given values.
24 * @param \Symfony\Component\Routing\RouteCollection $collection
25 * The route collection.
26 * @param \Drupal\layout_builder\SectionStorage\SectionStorageDefinition $definition
27 * The definition of the section storage.
29 * The path patten for the routes.
30 * @param array $defaults
31 * (optional) An array of default parameter values.
32 * @param array $requirements
33 * (optional) An array of requirements for parameters.
34 * @param array $options
35 * (optional) An array of options.
36 * @param string $route_name_prefix
37 * (optional) The prefix to use for the route name.
39 protected function buildLayoutRoutes(RouteCollection $collection, SectionStorageDefinition $definition, $path, array $defaults = [], array $requirements = [], array $options = [], $route_name_prefix = '') {
40 $type = $definition->id();
41 $defaults['section_storage_type'] = $type;
42 // Provide an empty value to allow the section storage to be upcast.
43 $defaults['section_storage'] = '';
44 // Trigger the layout builder access check.
45 $requirements['_has_layout_section'] = 'true';
46 // Trigger the layout builder RouteEnhancer.
47 $options['_layout_builder'] = TRUE;
48 // Trigger the layout builder param converter.
49 $parameters['section_storage']['layout_builder_tempstore'] = TRUE;
50 // Merge the passed in options in after Layout Builder's parameters.
51 $options = NestedArray::mergeDeep(['parameters' => $parameters], $options);
53 if ($route_name_prefix) {
54 $route_name_prefix = "layout_builder.$type.$route_name_prefix";
57 $route_name_prefix = "layout_builder.$type";
60 $main_defaults = $defaults;
61 $main_defaults['is_rebuilding'] = FALSE;
62 $main_defaults['_controller'] = '\Drupal\layout_builder\Controller\LayoutBuilderController::layout';
63 $main_defaults['_title_callback'] = '\Drupal\layout_builder\Controller\LayoutBuilderController::title';
64 $route = (new Route($path))
65 ->setDefaults($main_defaults)
66 ->setRequirements($requirements)
67 ->setOptions($options);
68 $collection->add("$route_name_prefix.view", $route);
70 $save_defaults = $defaults;
71 $save_defaults['_controller'] = '\Drupal\layout_builder\Controller\LayoutBuilderController::saveLayout';
72 $route = (new Route("$path/save"))
73 ->setDefaults($save_defaults)
74 ->setRequirements($requirements)
75 ->setOptions($options);
76 $collection->add("$route_name_prefix.save", $route);
78 $cancel_defaults = $defaults;
79 $cancel_defaults['_controller'] = '\Drupal\layout_builder\Controller\LayoutBuilderController::cancelLayout';
80 $route = (new Route("$path/cancel"))
81 ->setDefaults($cancel_defaults)
82 ->setRequirements($requirements)
83 ->setOptions($options);
84 $collection->add("$route_name_prefix.cancel", $route);
86 if (is_subclass_of($definition->getClass(), OverridesSectionStorageInterface::class)) {
87 $revert_defaults = $defaults;
88 $revert_defaults['_form'] = '\Drupal\layout_builder\Form\RevertOverridesForm';
89 $route = (new Route("$path/revert"))
90 ->setDefaults($revert_defaults)
91 ->setRequirements($requirements)
92 ->setOptions($options);
93 $collection->add("$route_name_prefix.revert", $route);