#903216: Fix legacy renderer to support new "add_css" method.
[project/panels.git] / plugins / display_renderers / panels_renderer_standard.class.php
1 <?php
2 // $Id$
3
4 /**
5 * The standard render pipeline for a Panels display object.
6 *
7 * Given a fully-loaded panels_display object, this class will turn its
8 * combination of layout, panes, and styles into HTML, invoking caching
9 * appropriately along the way. Interacting with the renderer externally is
10 * very simple - just pass it the display object and call the render() method:
11 *
12 * @code
13 * // given that $display is a fully loaded Panels display object
14 * $renderer = panels_get_renderer_handler('standard', $display)
15 * $html_output = $renderer->render();
16 * @endcode
17 *
18 * Internally, the render pipeline is divided into two phases, prepare and
19 * render:
20 * - The prepare phase transforms the skeletal data on the provided
21 * display object into a structure that is expected by the render phase.
22 * It is divided into a series of discrete sub-methods and operates
23 * primarily by passing parameters, all with the intention of making
24 * subclassing easier.
25 * - The render phase relies primarily on data stored in the renderer object's
26 * properties, presumably set in the prepare phase. It iterates through the
27 * rendering of each pane, pane styling, placement in panel regions, region
28 * styling, and finally the arrangement of rendered regions in the layout.
29 * Caching, if in use, is triggered per pane, or on the entire display.
30 *
31 * In short: prepare builds conf, render renders conf. Subclasses should respect
32 * this separation of responsibilities by adhering to these loose guidelines,
33 * given a loaded display object:
34 * - If your renderer needs to modify the datastructure representing what is
35 * to be rendered (panes and their conf, styles, caching, etc.), it should
36 * use the prepare phase.
37 * - If your renderer needs to modify the manner in which that renderable
38 * datastructure data is rendered, it should use the render phase.
39 *
40 * In the vast majority of use cases, this standard renderer will be sufficient
41 * and need not be switched out/subclassed; style and/or layout plugins can
42 * accommodate nearly every use case. If you think you might need a custom
43 * renderer, consider the following criteria/examples:
44 * - Some additional markup needs to be added to EVERY SINGLE panel.
45 * - Given a full display object, just render one pane.
46 * - Show a Panels admin interface.
47 *
48 * The system is almost functionally identical to the old procedural approach,
49 * with some exceptions (@see panels_renderer_legacy for details). The approach
50 * here differs primarily in its friendliness to tweaking in subclasses.
51 */
52 class panels_renderer_standard {
53 /**
54 * The fully-loaded Panels display object that is to be rendered. "Fully
55 * loaded" is defined as:
56 * 1. Having been produced by panels_load_displays(), whether or this page
57 * request or at some time in the past and the object was exported.
58 * 2. Having had some external code attach context data ($display->context),
59 * in the exact form expected by panes. Context matching is delicate,
60 * typically relying on exact string matches, so special attention must
61 * be taken.
62 *
63 * @var panels_display
64 */
65 var $display;
66
67 /**
68 * An associative array of loaded plugins. Used primarily as a central
69 * location for storing plugins that require additional loading beyond
70 * reading the plugin definition, which is already statically cached by
71 * ctools_get_plugins(). An example is layout plugins, which can optionally
72 * have a callback that determines the set of panel regions available at
73 * runtime.
74 *
75 * @var array
76 */
77 var $plugins = array();
78
79 /**
80 * A multilevel array of rendered data. The first level of the array
81 * indicates the type of rendered data, typically with up to three keys:
82 * 'layout', 'regions', and 'panes'. The relevant rendered data is stored as
83 * the value for each of these keys as it is generated:
84 * - 'panes' are an associative array of rendered output, keyed on pane id.
85 * - 'regions' are an associative array of rendered output, keyed on region
86 * name.
87 * - 'layout' is the whole of the rendered output.
88 *
89 * @var array
90 */
91 var $rendered = array();
92
93 /**
94 * A multilevel array of data prepared for rendering. The first level of the
95 * array indicates the type of prepared data. The standard renderer populates
96 * and uses two top-level keys, 'panes' and 'regions':
97 * - 'panes' are an associative array of pane objects to be rendered, keyed
98 * on pane id and sorted into proper rendering order.
99 * - 'regions' are an associative array of regions, keyed on region name,
100 * each of which is itself an indexed array of pane ids in the order in
101 * which those panes appear in that region.
102 *
103 * @var array
104 */
105 var $prepared = array();
106
107 /**
108 * Boolean state variable, indicating whether or not the prepare() method has
109 * been run.
110 *
111 * This state is checked in panels_renderer_standard::render_layout() to
112 * determine whether the prepare method should be automatically triggered.
113 *
114 * @var bool
115 */
116 var $prep_run = FALSE;
117
118 /**
119 * The plugin that defines this handler.
120 */
121 var $plugin = FALSE;
122
123 /**
124 * TRUE if this renderer is rendering in administrative mode
125 * which will allow layouts to have extra functionality.
126 *
127 * @var bool
128 */
129 var $admin = FALSE;
130
131 /**
132 * Where to add standard meta information. There are three possibilities:
133 * - standard: Put the meta information in the normal location. Default.
134 * - inline: Put the meta information directly inline. This will
135 * not work for javascript.
136 *
137 * @var string
138 */
139 var $meta_location = 'standard';
140
141 /**
142 * Include rendered HTML prior to the layout.
143 *
144 * @var string
145 */
146 var $prefix = '';
147
148 /**
149 * Include rendered HTML after the layout.
150 *
151 * @var string
152 */
153 var $suffix = '';
154
155 /**
156 * Receive and store the display object to be rendered.
157 *
158 * This is a psuedo-constructor that should typically be called immediately
159 * after object construction.
160 *
161 * @param array $plugin
162 * The definition of the renderer plugin.
163 * @param panels_display $display
164 * The panels display object to be rendered.
165 */
166 function init($plugin, &$display) {
167 $this->plugin = $plugin;
168 $layout = panels_get_layout($display->layout);
169 $this->display = &$display;
170 $this->plugins['layout'] = $layout;
171 if (!isset($layout['panels'])) {
172 $this->plugins['layout']['panels'] = panels_get_regions($layout, $display);
173 }
174
175 if (empty($this->plugins['layout'])) {
176 watchdog('panels', "Layout: @layout couldn't been found, maybe the theme is disabled.", array('@layout' => $display->layout));
177 }
178 }
179
180 /**
181 * Prepare the attached display for rendering.
182 *
183 * This is the outermost prepare method. It calls several sub-methods as part
184 * of the overall preparation process. This compartmentalization is intended
185 * to ease the task of modifying renderer behavior in child classes.
186 *
187 * If you override this method, it is important that you either call this
188 * method via parent::prepare(), or manually set $this->prep_run = TRUE.
189 *
190 * @param mixed $external_settings
191 * An optional parameter allowing external code to pass in additional
192 * settings for use in the preparation process. Not used in the default
193 * renderer, but included for interface consistency.
194 */
195 function prepare($external_settings = NULL) {
196 $this->prepare_panes($this->display->content);
197 $this->prepare_regions($this->display->panels, $this->display->panel_settings);
198 $this->prep_run = TRUE;
199 }
200
201 /**
202 * Prepare the list of panes to be rendered, accounting for visibility/access
203 * settings and rendering order.
204 *
205 * This method represents the standard approach for determining the list of
206 * panes to be rendered that is compatible with all parts of the Panels
207 * architecture. It first applies visibility & access checks, then sorts panes
208 * into their proper rendering order, and returns the result as an array.
209 *
210 * Inheriting classes should override this method if that renderer needs to
211 * regularly make additions to the set of panes that will be rendered.
212 *
213 * @param array $panes
214 * An associative array of pane data (stdClass objects), keyed on pane id.
215 * @return array
216 * An associative array of panes to be rendered, keyed on pane id and sorted
217 * into proper rendering order.
218 */
219 function prepare_panes($panes) {
220 ctools_include('content');
221 // Use local variables as writing to them is very slightly faster
222 $normal = $last = array();
223
224 // Prepare the list of panes to be rendered
225 foreach ($panes as $pid => $pane) {
226 if (empty($this->admin)) {
227 // TODO remove in 7.x and ensure the upgrade path weeds out any stragglers; it's been long enough
228 $pane->shown = !empty($pane->shown); // guarantee this field exists.
229 // If this pane is not visible to the user, skip out and do the next one
230 if (!$pane->shown || !panels_pane_access($pane, $this->display)) {
231 continue;
232 }
233 }
234
235 $ct_plugin_def = ctools_get_content_type($pane->type);
236
237 // If this pane wants to render last, add it to the $last array. We allow
238 // this because some panes need to be rendered after other panes,
239 // primarily so they can do things like the leftovers of forms.
240 if (!empty($ct_plugin_def['render last'])) {
241 $last[$pid] = $pane;
242 }
243 // Otherwise, render it in the normal order.
244 else {
245 $normal[$pid] = $pane;
246 }
247 }
248 $this->prepared['panes'] = $normal + $last;
249 return $this->prepared['panes'];
250 }
251
252 /**
253 * Prepare the list of regions to be rendered.
254 *
255 * This method is primarily about properly initializing the style plugin that
256 * will be used to render the region. This is crucial as regions cannot be
257 * rendered without a style plugin (in keeping with Panels' philosophy of
258 * hardcoding none of its output), but for most regions no style has been
259 * explicitly set. The logic here is what accommodates that situation:
260 * - If a region has had its style explicitly set, then we fetch that plugin
261 * and continue.
262 * - If the region has no explicit style, but a style was set at the display
263 * level, then inherit the style from the display.
264 * - If neither the region nor the dispay have explicitly set styles, then
265 * fall back to the hardcoded 'default' style, a very minimal style.
266 *
267 * The other important task accomplished by this method is ensuring that even
268 * regions without any panes are still properly prepared for the rendering
269 * process. This is essential because the way Panels loads display objects
270 * (@see panels_load_displays) results only in a list of regions that
271 * contain panes - not necessarily all the regions defined by the layout
272 * plugin, which can only be determined by asking the plugin at runtime. This
273 * method consults that retrieved list of regions and prepares all of those,
274 * ensuring none are inadvertently skipped.
275 *
276 * @param array $region_pane_list
277 * An associative array of pane ids, keyed on the region to which those pids
278 * are assigned. In the default case, this is $display->panels.
279 * @param array $settings
280 * All known region style settings, including both the top-level display's
281 * settings (if any) and all region-specific settings (if any).
282 * @return array
283 * An array of regions prepared for rendering.
284 */
285 function prepare_regions($region_pane_list, $settings) {
286 // Initialize defaults to be used for regions without their own explicit
287 // settings. Use display settings if they exist, else hardcoded defaults.
288 $default = array(
289 'style' => panels_get_style(!empty($settings['style']) ? $settings['style'] : 'default'),
290 'style settings' => isset($settings['style_settings']['default']) ? $settings['style_settings']['default'] : array(),
291 );
292
293 $regions = array();
294 if (empty($settings)) {
295 // No display/panel region settings exist, init all with the defaults.
296 foreach ($this->plugins['layout']['panels'] as $region_id => $title) {
297 // Ensure this region has at least an empty panes array.
298 $panes = !empty($region_pane_list[$region_id]) ? $region_pane_list[$region_id] : array();
299
300 $regions[$region_id] = $default;
301 $regions[$region_id]['pids'] = $panes;
302 }
303 }
304 else {
305 // Some settings exist; iterate through each region and set individually.
306 foreach ($this->plugins['layout']['panels'] as $region_id => $title) {
307 // Ensure this region has at least an empty panes array.
308 $panes = !empty($region_pane_list[$region_id]) ? $region_pane_list[$region_id] : array();
309
310 if (empty($settings[$region_id]['style']) || $settings[$region_id]['style'] == -1) {
311 $regions[$region_id] = $default;
312 }
313 else {
314 $regions[$region_id]['style'] = panels_get_style($settings[$region_id]['style']);
315 $regions[$region_id]['style settings'] = isset($settings['style_settings'][$region_id]) ? $settings['style_settings'][$region_id] : array();
316 }
317 $regions[$region_id]['pids'] = $panes;
318 }
319 }
320
321 $this->prepared['regions'] = $regions;
322 return $this->prepared['regions'];
323 }
324
325 /**
326 * Build inner content, then hand off to layout-specified theme function for
327 * final render step.
328 *
329 * This is the outermost method in the Panels render pipeline. It calls the
330 * inner methods, which return a content array, which is in turn passed to the
331 * theme function specified in the layout plugin.
332 *
333 * @return string
334 * Themed & rendered HTML output.
335 */
336 function render() {
337 // Attach out-of-band data first.
338 $this->add_meta();
339
340 if (empty($this->display->cache['method']) || !empty($this->display->skip_cache)) {
341 return $this->render_layout();
342 }
343 else {
344 $cache = panels_get_cached_content($this->display, $this->display->args, $this->display->context);
345 if ($cache === FALSE) {
346 $cache = new panels_cache_object();
347 $cache->set_content($this->render_layout());
348 panels_set_cached_content($cache, $this->display, $this->display->args, $this->display->context);
349 }
350 return $cache->content;
351 }
352 }
353
354 /**
355 * Perform display/layout-level render operations.
356 *
357 * This method triggers all the inner pane/region rendering processes, passes
358 * that to the layout plugin's theme callback, and returns the rendered HTML.
359 *
360 * If display-level caching is enabled and that cache is warm, this method
361 * will not be called.
362 *
363 * @return string
364 * The HTML string representing the entire rendered, themed panel.
365 */
366 function render_layout() {
367 if (empty($this->prep_run)) {
368 $this->prepare();
369 }
370 $this->render_panes();
371 $this->render_regions();
372
373 if ($this->admin && !empty($this->plugins['layout']['admin theme'])) {
374 $theme = $this->plugins['layout']['admin theme'];
375 }
376 else {
377 $theme = $this->plugins['layout']['theme'];
378 }
379 $this->rendered['layout'] = theme($theme, check_plain($this->display->css_id), $this->rendered['regions'], $this->display->layout_settings, $this->display, $this->plugins['layout'], $this);
380 return $this->prefix . $this->rendered['layout'] . $this->suffix;
381 }
382
383 /**
384 * Attach out-of-band page metadata (e.g., CSS and JS).
385 *
386 * This must be done before render, because panels-within-panels must have
387 * their CSS added in the right order: inner content before outer content.
388 */
389 function add_meta() {
390 if (!empty($this->plugins['layout']['css'])) {
391 if (file_exists(path_to_theme() . '/' . $this->plugins['layout']['css'])) {
392 $this->add_css(path_to_theme() . '/' . $this->plugins['layout']['css']);
393 }
394 else {
395 $this->add_css($this->plugins['layout']['path'] . '/' . $this->plugins['layout']['css']);
396 }
397 }
398
399 if ($this->admin && isset($this->plugins['layout']['admin css'])) {
400 $this->add_css($this->plugins['layout']['path'] . '/' . $this->plugins['layout']['admin css']);
401 }
402 }
403
404 /**
405 * Add CSS information to the renderer.
406 *
407 * To facilitate previews over Views, CSS can now be added in a manner
408 * that does not necessarily mean just using drupal_add_css. Therefore,
409 * during the panel rendering process, this method can be used to add
410 * css and make certain that ti gets to the proper location.
411 *
412 * The arguments should exactly match drupal_add_css().
413 *
414 * @see drupal_add_css
415 */
416 function add_css($filename, $type = 'module', $media = 'all', $preprocess = TRUE) {
417 $path = file_create_path($filename);
418 switch ($this->meta_location) {
419 case 'standard':
420 if ($path) {
421 // Use CTools CSS add because it can handle temporary CSS in private
422 // filesystem.
423 ctools_include('css');
424 ctools_css_add_css($filename, $type, $media, $preprocess);
425 }
426 else {
427 drupal_add_css($filename, $type, $media, $preprocess);
428 }
429 break;
430 case 'inline':
431 if ($path) {
432 $url = file_create_url($filename);
433 }
434 else {
435 $url = base_path() . $filename;
436 }
437
438 $this->prefix .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . $url . '" />'."\n";
439 break;
440 }
441 }
442
443 /**
444 * Render all prepared panes, first by dispatching to their plugin's render
445 * callback, then handing that output off to the pane's style plugin.
446 *
447 * @return array
448 * The array of rendered panes, keyed on pane pid.
449 */
450 function render_panes() {
451 ctools_include('content');
452
453 // First, render all the panes into little boxes.
454 $this->rendered['panes'] = array();
455 foreach ($this->prepared['panes'] as $pid => $pane) {
456 $content = $this->render_pane($pane);
457 if ($content) {
458 $this->rendered['panes'][$pid] = $content;
459 }
460 }
461 return $this->rendered['panes'];
462 }
463
464 /**
465 * Render a pane using its designated style.
466 *
467 * This method also manages 'title pane' functionality, where the title from
468 * an individual pane can be bubbled up to take over the title for the entire
469 * display.
470 *
471 * @param stdClass $pane
472 * A Panels pane object, as loaded from the database.
473 */
474 function render_pane(&$pane) {
475 $content = $this->render_pane_content($pane);
476 if ($this->display->hide_title == PANELS_TITLE_PANE && !empty($this->display->title_pane) && $this->display->title_pane == $pane->pid) {
477
478 // If the user selected to override the title with nothing, and selected
479 // this as the title pane, assume the user actually wanted the original
480 // title to bubble up to the top but not actually be used on the pane.
481 if (empty($content->title) && !empty($content->original_title)) {
482 $this->display->stored_pane_title = $content->original_title;
483 }
484 else {
485 $this->display->stored_pane_title = !empty($content->title) ? $content->title : '';
486 }
487 }
488
489 if (!empty($content->content)) {
490 if (!empty($pane->style['style'])) {
491 $style = panels_get_style($pane->style['style']);
492
493 if (isset($style) && isset($style['render pane'])) {
494 $output = theme($style['render pane'], $content, $pane, $this->display, $style);
495
496 // This could be null if no theme function existed.
497 if (isset($output)) {
498 return $output;
499 }
500 }
501 }
502
503 // fallback
504 return theme('panels_pane', $content, $pane, $this->display);
505 }
506 }
507
508 /**
509 * Render the interior contents of a single pane.
510 *
511 * This method retrieves pane content and produces a ready-to-render content
512 * object. It also manages pane-specific caching.
513 *
514 * @param stdClass $pane
515 * A Panels pane object, as loaded from the database.
516 * @return stdClass $content
517 * A renderable object, containing a subject, content, etc. Based on the
518 * renderable objects used by the block system.
519 */
520 function render_pane_content(&$pane) {
521 ctools_include('context');
522 // TODO finally safe to remove this check?
523 if (!is_array($this->display->context)) {
524 watchdog('panels', 'renderer::render_pane_content() hit with a non-array for the context', $this->display, WATCHDOG_DEBUG);
525 $this->display->context = array();
526 }
527
528 $content = FALSE;
529 $caching = !empty($pane->cache['method']) && empty($this->display->skip_cache);
530 if ($caching && ($cache = panels_get_cached_content($this->display, $this->display->args, $this->display->context, $pane))) {
531 $content = $cache->content;
532 }
533 else {
534 $content = ctools_content_render($pane->type, $pane->subtype, $pane->configuration, array(), $this->display->args, $this->display->context);
535 foreach (module_implements('panels_pane_content_alter') as $module) {
536 $function = $module . '_panels_pane_content_alter';
537 $function($content, $pane, $this->display->args, $this->display->context);
538 }
539 if ($caching) {
540 $cache = new panels_cache_object();
541 $cache->set_content($content);
542 panels_set_cached_content($cache, $this->display, $this->display->args, $this->display->context, $pane);
543 $content = $cache->content;
544 }
545 }
546
547 // Pass long the css_id that is usually available.
548 if (!empty($pane->css['css_id'])) {
549 $content->css_id = $pane->css['css_id'];
550 }
551
552 // Pass long the css_class that is usually available.
553 if (!empty($pane->css['css_class'])) {
554 $content->css_class = $pane->css['css_class'];
555 }
556
557 return $content;
558 }
559
560 /**
561 * Render all prepared regions, placing already-rendered panes into their
562 * appropriate positions therein.
563 *
564 * @return array
565 * An array of rendered panel regions, keyed on the region name.
566 */
567 function render_regions() {
568 $this->rendered['regions'] = array();
569
570 // Loop through all panel regions, put all panes that belong to the current
571 // region in an array, then render the region. Primarily this ensures that
572 // the panes are arranged in the proper order.
573 $content = array();
574 foreach ($this->prepared['regions'] as $region_id => $conf) {
575 $region_panes = array();
576 foreach ($conf['pids'] as $pid) {
577 // Only include panes for region rendering if they had some output.
578 if (!empty($this->rendered['panes'][$pid])) {
579 $region_panes[$pid] = $this->rendered['panes'][$pid];
580 }
581 }
582 $this->rendered['regions'][$region_id] = $this->render_region($region_id, $region_panes);
583 }
584
585 return $this->rendered['regions'];
586 }
587
588 /**
589 * Render a single panel region.
590 *
591 * Primarily just a passthrough to the panel region rendering callback
592 * specified by the style plugin that is attached to the current panel region.
593 *
594 * @param $region_id
595 * The ID of the panel region being rendered
596 * @param $panes
597 * An array of panes that are assigned to the panel that's being rendered.
598 *
599 * @return string
600 * The rendered, HTML string output of the passed-in panel region.
601 */
602 function render_region($region_id, $panes) {
603 $style = $this->prepared['regions'][$region_id]['style'];
604 $style_settings = $this->prepared['regions'][$region_id]['style settings'];
605
606 // Retrieve the pid (can be a panel page id, a mini panel id, etc.), this
607 // might be used (or even necessary) for some panel display styles.
608 // TODO: Got to fix this to use panel page name instead of pid, since pid is
609 // no longer guaranteed. This needs an API to be able to set the final id.
610 $owner_id = 0;
611 if (isset($this->display->owner) && is_object($this->display->owner) && isset($this->display->owner->id)) {
612 $owner_id = $this->display->owner->id;
613 }
614
615 return theme($style['render region'], $this->display, $owner_id, $panes, $style_settings, $region_id, $style);
616 }
617 }