Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Render / MainContent / DialogRenderer.php
1 <?php
2
3 namespace Drupal\Core\Render\MainContent;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Ajax\OpenDialogCommand;
8 use Drupal\Core\Controller\TitleResolverInterface;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Symfony\Component\HttpFoundation\Request;
11
12 /**
13  * Default main content renderer for dialog requests.
14  */
15 class DialogRenderer implements MainContentRendererInterface {
16
17   /**
18    * The title resolver.
19    *
20    * @var \Drupal\Core\Controller\TitleResolverInterface
21    */
22   protected $titleResolver;
23
24   /**
25    * Constructs a new DialogRenderer.
26    *
27    * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
28    *   The title resolver.
29    */
30   public function __construct(TitleResolverInterface $title_resolver) {
31     $this->titleResolver = $title_resolver;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
38     $response = new AjaxResponse();
39
40     // First render the main content, because it might provide a title.
41     $content = drupal_render_root($main_content);
42
43     // Attach the library necessary for using the OpenDialogCommand and set the
44     // attachments for this Ajax response.
45     $main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
46     $response->setAttachments($main_content['#attached']);
47
48     // Determine the title: use the title provided by the main content if any,
49     // otherwise get it from the routing information.
50     $title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject());
51
52     // Determine the dialog options and the target for the OpenDialogCommand.
53     $options = $request->request->get('dialogOptions', []);
54     $target = $this->determineTargetSelector($options, $route_match);
55
56     $response->addCommand(new OpenDialogCommand($target, $title, $content, $options));
57     return $response;
58   }
59
60   /**
61    * Determine the target selector for the OpenDialogCommand.
62    *
63    * @param array &$options
64    *   The 'target' option, if set, is used, and then removed from $options.
65    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
66    *   When no 'target' option is set in $options, $route_match is used instead
67    *   to determine the target.
68    *
69    * @return string
70    *   The target selector.
71    */
72   protected function determineTargetSelector(array &$options, RouteMatchInterface $route_match) {
73     // Generate the target wrapper for the dialog.
74     if (isset($options['target'])) {
75       // If the target was nominated in the incoming options, use that.
76       $target = $options['target'];
77       // Ensure the target includes the #.
78       if (substr($target, 0, 1) != '#') {
79         $target = '#' . $target;
80       }
81       // This shouldn't be passed on to jQuery.ui.dialog.
82       unset($options['target']);
83     }
84     else {
85       // Generate a target based on the route id.
86       $route_name = $route_match->getRouteName();
87       $target = '#' . Html::getUniqueId("drupal-dialog-$route_name");
88     }
89     return $target;
90   }
91
92 }