You are here

protected function ContentAwareGenerator::getRouteByContent in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony-cmf/routing/ContentAwareGenerator.php \Symfony\Cmf\Component\Routing\ContentAwareGenerator::getRouteByContent()

Get the route based on the $name that is an object implementing RouteReferrersReadInterface or a content found in the content repository with the content_id specified in parameters that is an instance of RouteReferrersReadInterface.

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 RouteReferrersReadInterface object

Return value

SymfonyRoute the route instance

Throws

RouteNotFoundException if no route can be determined

1 call to ContentAwareGenerator::getRouteByContent()
ContentAwareGenerator::generate in vendor/symfony-cmf/routing/ContentAwareGenerator.php

File

vendor/symfony-cmf/routing/ContentAwareGenerator.php, line 161

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 RouteReferrersReadInterface) {
    $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 RouteReferrersReadInterface) {
      throw new RouteNotFoundException('Content repository did not return a RouteReferrersReadInterface instance 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 RouteReferrersReadInterface instance 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, randomly return the first one
  if ($routes instanceof Collection) {
    return $routes
      ->first();
  }
  return reset($routes);
}