function AttributeRouteDiscovery::createFormRouteCollection

Same name and namespace in other branches
  1. main core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php \Drupal\Core\Routing\AttributeRouteDiscovery::createFormRouteCollection()

Creates a route collection from a form class's attributed methods.

Parameters

\ReflectionClass<object> $class: The reflection object of the class to generate a route collection for.

Return value

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

1 call to AttributeRouteDiscovery::createFormRouteCollection()
AttributeRouteDiscovery::collectRoutes in core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php
Creates a collection of routes to add to the route builder.

File

core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php, line 255

Class

AttributeRouteDiscovery
Discovers routes using Symfony's Route attribute.

Namespace

Drupal\Core\Routing

Code

private function createFormRouteCollection(\ReflectionClass $class) : RouteCollection {
  $collection = new RouteCollection();
  if (!$class->implementsInterface(FormInterface::class)) {
    return $collection;
  }
  foreach ($this->getAttributes($class) as $attribute) {
    $attribute->defaults = [
      '_form' => $class->getName(),
    ] + $attribute->defaults;
    $this->addRoute($collection, $attribute, $this->resetGlobals(), $class);
    $formRouteName = $attribute->name;
  }
  // If there is only one route defined for the form class, add the class name
  // as an alias for the route.
  if (count($collection) === 1 && isset($formRouteName)) {
    $collection->addAlias($class->getName(), $formRouteName);
  }
  // Route attributes on form class methods are not supported.
  assert(Inspector::assertAll(fn($method) => $this->getAttributes($method)
    ->key() === NULL, $class->getMethods()), sprintf('Route attributes can not target methods on class %s. Use the attribute on the form class itself.', $class->getName()));
  return $collection;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.