You are here

public function Library::buildRoutes in Layout builder library 8

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

src/Plugin/SectionStorage/Library.php, line 119

Class

Library
Defines a class for library based layout storage.

Namespace

Drupal\layout_library\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;
    }

    // Add a layout-library URL off the tail of each manage display.
    $path = $entity_route
      ->getPath() . '/layout-library/{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;
    $options['parameters']['layout']['type'] = 'entity:layout';
    $this
      ->buildLayoutRoutes($collection, $this
      ->getPluginDefinition(), $path, $defaults, $requirements, $options, $entity_type_id, 'layout');
  }
}