/**
  * {@inheritdoc}
  */
 public function enhance(array $defaults, Request $request)
 {
     if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
         $defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
     }
     return $defaults;
 }
Пример #2
0
 /**
  * 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.
  *
  * @param mixed $name
  * @param array $parameters which should contain a content field containing
  *                          a RouteReferrersReadInterface object
  *
  * @return SymfonyRoute the route instance
  *
  * @throws RouteNotFoundException if no route can be determined
  */
 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);
 }