/**
  * Converts a render array into an HtmlFragment object.
  *
  * @param array|string $page_content
  *   The page content area to display.
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request object.
  *
  * @return \Drupal\Core\Page\HtmlPage
  *   A page object.
  */
 protected function createHtmlFragment($page_content, Request $request)
 {
     // Allow controllers to return a HtmlFragment or a Response object directly.
     if ($page_content instanceof HtmlFragment || $page_content instanceof Response) {
         return $page_content;
     }
     if (!is_array($page_content)) {
         $page_content = array('main' => array('#markup' => $page_content));
     }
     $content = $this->drupalRender($page_content);
     $cache = !empty($page_content['#cache']['tags']) ? array('tags' => $page_content['#cache']['tags']) : array();
     $fragment = new HtmlFragment($content, $cache);
     // A title defined in the return always wins.
     if (isset($page_content['#title'])) {
         $fragment->setTitle($page_content['#title'], Title::FILTER_XSS_ADMIN);
     } else {
         if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
             $fragment->setTitle($this->titleResolver->getTitle($request, $route), Title::PASS_THROUGH);
         }
     }
     // Add feed links from the page content.
     $attached = drupal_render_collect_attached($page_content, TRUE);
     if (!empty($attached['drupal_add_feed'])) {
         foreach ($attached['drupal_add_feed'] as $feed) {
             $feed_link = new FeedLinkElement($feed[1], $this->urlGenerator->generateFromPath($feed[0]));
             $fragment->addLinkElement($feed_link);
         }
     }
     return $fragment;
 }
 /**
  * Tests a dynamic title.
  *
  * @see \Drupal\Core\Controller\TitleResolver::getTitle()
  */
 public function testDynamicTitle()
 {
     $request = new Request();
     $route = new Route('/test-route', array('_title' => 'static title', '_title_callback' => 'Drupal\\Tests\\Core\\Controller\\TitleCallback::example'));
     $callable = array(new TitleCallback(), 'example');
     $this->controllerResolver->expects($this->once())->method('getControllerFromDefinition')->with('Drupal\\Tests\\Core\\Controller\\TitleCallback::example')->will($this->returnValue($callable));
     $this->controllerResolver->expects($this->once())->method('getArguments')->with($request, $callable)->will($this->returnValue(array('example')));
     $this->assertEquals('test example', $this->titleResolver->getTitle($request, $route));
 }
 /**
  * Displays content in a dialog.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request object.
  * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  *   The route match.
  * @param mixed $_content
  *   A controller definition string, or a callable object/closure.
  * @param bool $modal
  *   (optional) TRUE to render a modal dialog. Defaults to FALSE.
  *
  * @return \Drupal\Core\Ajax\AjaxResponse
  *   AjaxResponse to return the content wrapper in a dialog.
  */
 public function dialog(Request $request, RouteMatchInterface $route_match, $_content, $modal = FALSE)
 {
     $page_content = $this->getContentResult($request, $_content);
     // Allow controllers to return a HtmlPage or a Response object directly.
     if ($page_content instanceof HtmlPage) {
         $page_content = $page_content->getContent();
     }
     if ($page_content instanceof Response) {
         $page_content = $page_content->getContent();
     }
     // Most controllers return a render array, but some return a string.
     if (!is_array($page_content)) {
         $page_content = array('#markup' => $page_content);
     }
     $content = drupal_render($page_content);
     drupal_process_attached($page_content);
     $title = isset($page_content['#title']) ? $page_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject());
     $response = new AjaxResponse();
     // Fetch any modal options passed in from data-dialog-options.
     $options = $request->request->get('dialogOptions', array());
     // Set modal flag and re-use the modal ID.
     if ($modal) {
         $options['modal'] = TRUE;
         $target = '#drupal-modal';
     } else {
         // Generate the target wrapper for the dialog.
         if (isset($options['target'])) {
             // If the target was nominated in the incoming options, use that.
             $target = $options['target'];
             // Ensure the target includes the #.
             if (substr($target, 0, 1) != '#') {
                 $target = '#' . $target;
             }
             // This shouldn't be passed on to jQuery.ui.dialog.
             unset($options['target']);
         } else {
             // Generate a target based on the route id.
             $route_name = $route_match->getRouteName();
             $target = '#' . drupal_html_id("drupal-dialog-{$route_name}");
         }
     }
     $response->addCommand(new OpenDialogCommand($target, $title, $content, $options));
     return $response;
 }