You are here

public function DefaultsSectionStorage::buildRoutes in Drupal 10

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()
  2. 9 core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage::buildRoutes()

File

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

Class

DefaultsSectionStorage
Defines the 'defaults' section storage type.

Namespace

Drupal\layout_builder\Plugin\SectionStorage

Code

public function buildRoutes(RouteCollection $collection) {
  if (!\Drupal::moduleHandler()
    ->moduleExists('field_ui')) {
    return;
  }
  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);
    }
  }
}