You are here

public function DefaultsSectionStorage::buildRoutes in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage::buildRoutes()

Provides the routes needed for Layout Builder UI.

Allows the plugin to add or alter routes during the route building process. \Drupal\layout_builder\Routing\LayoutBuilderRoutesTrait is provided for the typical use case of building a standard Layout Builder UI.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection.

Overrides SectionStorageInterface::buildRoutes

See also

\Drupal\Core\Routing\RoutingEvents::ALTER

File

core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php, line 147

Class

DefaultsSectionStorage
Defines the 'defaults' section storage type.

Namespace

Drupal\layout_builder\Plugin\SectionStorage

Code

public function buildRoutes(RouteCollection $collection) {
  foreach ($this
    ->getEntityTypes() as $entity_type_id => $entity_type) {

    // Try to get the route from the current collection.
    if (!($entity_route = $collection
      ->get($entity_type
      ->get('field_ui_base_route')))) {
      continue;
    }
    $path = $entity_route
      ->getPath() . '/display/{view_mode_name}/layout';
    $defaults = [];
    $defaults['entity_type_id'] = $entity_type_id;

    // If the entity type has no bundles and it doesn't use {bundle} in its
    // admin path, use the entity type.
    if (strpos($path, '{bundle}') === FALSE) {
      if (!$entity_type
        ->hasKey('bundle')) {
        $defaults['bundle'] = $entity_type_id;
      }
      else {
        $defaults['bundle_key'] = $entity_type
          ->getBundleEntityType();
      }
    }
    $requirements = [];
    $requirements['_field_ui_view_mode_access'] = 'administer ' . $entity_type_id . ' display';
    $options = $entity_route
      ->getOptions();
    $options['_admin_route'] = FALSE;
    $this
      ->buildLayoutRoutes($collection, $this
      ->getPluginDefinition(), $path, $defaults, $requirements, $options, $entity_type_id, 'entity_view_display');

    // Set field_ui.route_enhancer to run on the manage layout form.
    if (isset($defaults['bundle_key'])) {
      $collection
        ->get("layout_builder.defaults.{$entity_type_id}.view")
        ->setOption('_field_ui', TRUE)
        ->setDefault('bundle', '');
    }
    $route_names = [
      "entity.entity_view_display.{$entity_type_id}.default",
      "entity.entity_view_display.{$entity_type_id}.view_mode",
    ];
    foreach ($route_names as $route_name) {
      if (!($route = $collection
        ->get($route_name))) {
        continue;
      }
      $route
        ->addDefaults([
        'section_storage_type' => $this
          ->getStorageType(),
        'section_storage' => '',
      ] + $defaults);
      $parameters['section_storage']['layout_builder_tempstore'] = TRUE;
      $parameters = NestedArray::mergeDeep($parameters, $route
        ->getOption('parameters') ?: []);
      $route
        ->setOption('parameters', $parameters);
    }
  }
}