protected function ContentAwareGenerator::getRouteByContent

Get the route based on the $name that is a RouteAwareInterface or a RouteAwareInterface content found in the content repository with the content_id specified in parameters.

Called in generate when there is no route given in the parameters.

If there is more than one route for the content, tries to find the first one that matches the _locale (provided in $parameters or otherwise defaulting to the request locale).

If no route with matching locale is found, falls back to just return the first route.

Parameters

mixed $name:

array $parameters which should contain a content field containing a RouteAwareInterface object:

Return value

SymfonyRoute the route instance

Throws

RouteNotFoundException if no route can be determined

1 call to ContentAwareGenerator::getRouteByContent()
ContentAwareGenerator::generate in drupal/core/vendor/symfony-cmf/routing/Symfony/Cmf/Component/Routing/ContentAwareGenerator.php

File

drupal/core/vendor/symfony-cmf/routing/Symfony/Cmf/Component/Routing/ContentAwareGenerator.php, line 140

Class

ContentAwareGenerator
A generator that tries to generate routes from object, route names or content objects or names.

Namespace

Symfony\Cmf\Component\Routing

Code

protected function getRouteByContent($name, &$parameters) {
  if ($name instanceof RouteAwareInterface) {
    $content = $name;
  }
  elseif (isset($parameters['content_id']) && null !== $this->contentRepository) {
    $content = $this->contentRepository
      ->findById($parameters['content_id']);
    if (empty($content)) {
      throw new RouteNotFoundException('The content repository found nothing at id ' . $parameters['content_id']);
    }
    if (!$content instanceof RouteAwareInterface) {
      throw new RouteNotFoundException('Content repository did not return a RouteAwareInterface for id ' . $parameters['content_id']);
    }
  }
  else {
    $hint = is_object($name) ? get_class($name) : gettype($name);
    throw new RouteNotFoundException("The route name argument '{$hint}' is not RouteAwareInterface and there is no 'content_id' parameter");
  }
  $routes = $content
    ->getRoutes();
  if (empty($routes)) {
    $hint = $this->contentRepository && $this->contentRepository
      ->getContentId($content) ? $this->contentRepository
      ->getContentId($content) : get_class($content);
    throw new RouteNotFoundException('Content document has no route: ' . $hint);
  }
  unset($parameters['content_id']);
  $route = $this
    ->getRouteByLocale($routes, $this
    ->getLocale($parameters));
  if ($route) {
    return $route;
  }

  // if none matched, continue and randomly return the first one
  return reset($routes);
}