function Route::getTitleClosure

Gets the title closure that a "_title_closure" route default refers to.

Closures cannot be serialized into the router table, so route discovery stores a reference to the attribute instead of the closure itself.

Parameters

string $class: The class declaring the attributed method.

string $method: The attributed method.

int $index: The position of the Route attribute among the route attributes on the method.

Return value

\Closure The title closure.

Throws

\InvalidArgumentException Thrown when the reference does not point at a Route attribute with a closure title, which means the router needs rebuilding.

See also

\Drupal\Core\Routing\AttributeRouteDiscovery::getAttributes()

3 calls to Route::getTitleClosure()
TitleClosureTest::testReferenceToNonClosureTitle in core/tests/Drupal/KernelTests/Core/Routing/TitleClosureTest.php
Tests that a reference to an attribute without a closure is reported.
TitleClosureTest::testStaleReference in core/tests/Drupal/KernelTests/Core/Routing/TitleClosureTest.php
Tests that a stale reference to a missing attribute is reported.
TitleResolver::getTitle in core/lib/Drupal/Core/Controller/TitleResolver.php

File

core/lib/Drupal/Core/Routing/Attribute/Route.php, line 140

Class

Route
Adds Drupal-specific properties to the Symfony Route attribute.

Namespace

Drupal\Core\Routing\Attribute

Code

public static function getTitleClosure(string $class, string $method, int $index = 0) : \Closure {
  // Filter on the Symfony attribute, so that the index matches the one
  // recorded by route discovery.
  $reflection = new \ReflectionMethod($class, $method);
  $attributes = $reflection->getAttributes(SymfonyRoute::class, \ReflectionAttribute::IS_INSTANCEOF);
  if (!isset($attributes[$index])) {
    throw new \InvalidArgumentException(sprintf('There is no Route attribute at index %d on %s::%s().', $index, $class, $method));
  }
  $title = $attributes[$index]->newInstance()->defaults['_title'] ?? NULL;
  if (!$title instanceof \Closure) {
    throw new \InvalidArgumentException(sprintf('The Route attribute at index %d on %s::%s() does not have a closure title.', $index, $class, $method));
  }
  return $title;
}

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