ViewExecutable.php 58.2 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\ViewExecutable.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
9
namespace Drupal\views;

10
use Symfony\Component\HttpFoundation\Response;
11
use Drupal\views\Plugin\Core\Entity\View;
12

merlinofchaos's avatar
merlinofchaos committed
13
14
15
16
17
18
19
20
21
22
23
/**
 * @defgroup views_objects Objects that represent a View or part of a view
 * @{
 * These objects are the core of Views do the bulk of the direction and
 * storing of data. All database activity is in these objects.
 */

/**
 * An object to contain all of the data to generate a view, plus the member
 * functions to build the view query, execute the query and render the output.
 */
24
class ViewExecutable {
25

merlinofchaos's avatar
merlinofchaos committed
26
  /**
27
   * The config entity in which the view is stored.
merlinofchaos's avatar
merlinofchaos committed
28
   *
29
   * @var Drupal\views\Plugin\Core\Entity\View
merlinofchaos's avatar
merlinofchaos committed
30
   */
31
  public $storage;
merlinofchaos's avatar
merlinofchaos committed
32

33
34
35
  /**
   * Whether or not the view has been built.
   *
36
37
   * @todo Group with other static properties.
   *
38
39
40
41
42
43
44
   * @var bool
   */
  public $built = FALSE;

  /**
   * Whether the view has been executed/query has been run.
   *
45
46
   * @todo Group with other static properties.
   *
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
   * @var bool
   */
  public $executed = FALSE;

  /**
   * Any arguments that have been passed into the view.
   *
   * @var array
   */
  public $args = array();

  /**
   * An array of build info.
   *
   * @var array
   */
  public $build_info = array();

  /**
   * Whether this view uses AJAX.
   *
   * @var bool
   */
  public $use_ajax = FALSE;
merlinofchaos's avatar
merlinofchaos committed
71

72
73
74
75
76
77
78
  /**
   * Where the results of a query will go.
   *
   * The array must use a numeric index starting at 0.
   *
   * @var array
   */
79
  public $result = array();
merlinofchaos's avatar
merlinofchaos committed
80
81
82

  // May be used to override the current pager info.

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  /**
   * The current page. If the view uses pagination.
   *
   * @var int
   */
  public $current_page = NULL;

  /**
   * The number of items per page.
   *
   * @var int
   */
  public $items_per_page = NULL;

  /**
   * The pager offset.
   *
   * @var int
   */
  public $offset = NULL;

  /**
   * The total number of rows returned from the query.
   *
   * @var array
   */
  public $total_rows = NULL;

  /**
112
   * Attachments to place before the view.
113
   *
114
   * @var array()
115
   */
116
  public $attachment_before = array();
117
118

  /**
119
   * Attachments to place after the view.
120
   *
121
   * @var array
122
   */
123
  public $attachment_after = array();
merlinofchaos's avatar
merlinofchaos committed
124
125
126

  // Exposed widget input

127
128
129
130
131
132
  /**
   * All the form data from $form_state['values'].
   *
   * @var array
   */
  public $exposed_data = array();
merlinofchaos's avatar
merlinofchaos committed
133

134
135
136
137
138
139
  /**
   * An array of input values from exposed forms.
   *
   * @var array
   */
  public $exposed_input = array();
merlinofchaos's avatar
merlinofchaos committed
140

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  /**
   * Exposed widget input directly from the $form_state['values'].
   *
   * @var array
   */
  public $exposed_raw_input = array();

  /**
   * Used to store views that were previously running if we recurse.
   *
   * @var array
   */
  public $old_view = array();

  /**
   * To avoid recursion in views embedded into areas.
   *
   * @var array
   */
  public $parent_views = array();

  /**
   * Whether this view is an attachment to another view.
   *
   * @var bool
   */
  public $is_attachment = NULL;
merlinofchaos's avatar
merlinofchaos committed
168
169
170
171
172
173

  /**
   * Identifier of the current display.
   *
   * @var string
   */
174
  public $current_display;
merlinofchaos's avatar
merlinofchaos committed
175
176

  /**
177
   * Where the $query object will reside.
merlinofchaos's avatar
merlinofchaos committed
178
   *
179
   * @var Drupal\views\Plugin\query\QueryInterface
merlinofchaos's avatar
merlinofchaos committed
180
   */
181
  public $query = NULL;
merlinofchaos's avatar
merlinofchaos committed
182

183
184
185
186
187
188
189
190
  /**
   * The used pager plugin used by the current executed view.
   *
   * @var Drupal\views\Plugin\views\pager\PagerPluginBase
   */
  public $pager = NULL;

  /**
merlinofchaos's avatar
merlinofchaos committed
191
192
   * The current used display plugin.
   *
193
   * @var Drupal\views\Plugin\views\display\DisplayPluginBase
merlinofchaos's avatar
merlinofchaos committed
194
   */
195
  public $display_handler;
merlinofchaos's avatar
merlinofchaos committed
196

197
198
199
200
201
202
203
204
205
206
  /**
   * The list of used displays of the view.
   *
   * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase
   * objects.
   *
   * @var array
   */
  public $displayHandlers;

merlinofchaos's avatar
merlinofchaos committed
207
208
209
  /**
   * The current used style plugin.
   *
210
   * @var Drupal\views\Plugin\views\style\StylePluginBase
merlinofchaos's avatar
merlinofchaos committed
211
   */
212
  public $style_plugin;
merlinofchaos's avatar
merlinofchaos committed
213
214
215
216
217
218

  /**
   * Stores the current active row while rendering.
   *
   * @var int
   */
219
  public $row_index;
merlinofchaos's avatar
merlinofchaos committed
220
221
222
223
224
225

   /**
   * Allow to override the url of the current view.
   *
   * @var string
   */
226
  public $override_url = NULL;
merlinofchaos's avatar
merlinofchaos committed
227
228
229
230
231
232

  /**
   * Allow to override the path used for generated urls.
   *
   * @var string
   */
233
  public $override_path = NULL;
merlinofchaos's avatar
merlinofchaos committed
234
235
236

  /**
   * Allow to override the used database which is used for this query.
237
238
   *
   * @var bool
merlinofchaos's avatar
merlinofchaos committed
239
   */
240
  public $base_database = NULL;
merlinofchaos's avatar
merlinofchaos committed
241

242
  // Handlers which are active on this view.
merlinofchaos's avatar
merlinofchaos committed
243
244
245

  /**
   * Stores the field handlers which are initialized on this view.
246
247
248
249
250
   *
   * An array containing Drupal\views\Plugin\views\field\FieldPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
251
   */
252
  public $field;
merlinofchaos's avatar
merlinofchaos committed
253
254
255

  /**
   * Stores the argument handlers which are initialized on this view.
256
257
258
259
260
   *
   * An array containing Drupal\views\Plugin\views\argument\ArgumentPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
261
   */
262
  public $argument;
merlinofchaos's avatar
merlinofchaos committed
263
264
265

  /**
   * Stores the sort handlers which are initialized on this view.
266
267
268
269
   *
   * An array containing Drupal\views\Plugin\views\sort\SortPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
270
   */
271
  public $sort;
merlinofchaos's avatar
merlinofchaos committed
272
273
274

  /**
   * Stores the filter handlers which are initialized on this view.
275
276
277
278
279
   *
   * An array containing Drupal\views\Plugin\views\filter\FilterPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
280
   */
281
  public $filter;
merlinofchaos's avatar
merlinofchaos committed
282
283
284

  /**
   * Stores the relationship handlers which are initialized on this view.
285
286
287
288
289
   *
   * An array containing Drupal\views\Plugin\views\relationship\RelationshipPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
290
   */
291
  public $relationship;
merlinofchaos's avatar
merlinofchaos committed
292
293
294

  /**
   * Stores the area handlers for the header which are initialized on this view.
295
296
297
298
   *
   * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
299
   */
300
  public $header;
merlinofchaos's avatar
merlinofchaos committed
301
302
303

  /**
   * Stores the area handlers for the footer which are initialized on this view.
304
305
306
307
   *
   * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
308
   */
309
  public $footer;
merlinofchaos's avatar
merlinofchaos committed
310
311
312

  /**
   * Stores the area handlers for the empty text which are initialized on this view.
313
314
315
316
   *
   * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
317
   */
318
  public $empty;
merlinofchaos's avatar
merlinofchaos committed
319

320
321
322
323
324
325
326
  /**
   * Stores the current response object.
   *
   * @var Symfony\Component\HttpFoundation\Response
   */
  protected $response = NULL;

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  /**
   * Does this view already have loaded it's handlers.
   *
   * @todo Group with other static properties.
   *
   * @var bool
   */
  public $inited;

  /**
   * The name of the active style plugin of the view.
   *
   * @todo remove this and just use $this->style_plugin
   *
   * @var string
   */
  public $plugin_name;

  /**
   * The options used by the style plugin of this running view.
   *
   * @todo To be able to remove it, Drupal\views\Plugin\views\argument\ArgumentPluginBase::default_summary()
   *   should instantiate the style plugin.
   * @var array
   */
  public $style_options;

  /**
   * The rendered output of the exposed form.
   *
   * @var string
   */
  public $exposed_widgets;

  /**
   * If this view has been previewed.
   *
   * @var bool
   */
  public $preview;

  /**
   * Force the query to calculate the total number of results.
   *
   * @todo Move to the query.
   *
   * @var bool
   */
  public $get_total_rows;

  /**
   * Indicates if the sorts have been built.
   *
   * @todo Group with other static properties.
   *
   * @var bool
   */
  public $build_sort;

  /**
   * Stores the many-to-one tables for performance.
   *
   * @var array
   */
  public $many_to_one_tables;

  /**
   * A unique identifier which allows to update multiple views output via js.
   *
   * @var string
   */
  public $dom_id;

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  /**
   * A render array container to store render related information.
   *
   * For example you can alter the array and attach some css/js via the
   * #attached key. This is the required way to add custom css/js.
   *
   * @var array
   *
   * @see drupal_process_attached
   */
  public $element = array(
    '#attached' => array(
      'css' => array(),
      'js' => array(),
      'library' => array(),
    ),
  );

418
419
420
  /**
   * Constructs a new ViewExecutable object.
   *
421
   * @param Drupal\views\Plugin\Core\Entity\View $storage
422
423
   *   The view config entity the actual information is stored on.
   */
424
  public function __construct(View $storage) {
425
426
    // Reference the storage and the executable to each other.
    $this->storage = $storage;
427
    $this->storage->set('executable', $this);
428
429
430

    // Add the default css for a view.
    $this->element['#attached']['css'][] = drupal_get_path('module', 'views') . '/css/views.base.css';
431
432
433
  }

  /**
434
   * @todo.
435
   */
436
437
  public function save() {
    $this->storage->save();
merlinofchaos's avatar
merlinofchaos committed
438
439
440
441
442
443
  }

  /**
   * Set the arguments that come to this view. Usually from the URL
   * but possibly from elsewhere.
   */
444
  public function setArguments($args) {
merlinofchaos's avatar
merlinofchaos committed
445
446
447
448
449
450
    $this->args = $args;
  }

  /**
   * Change/Set the current page for the pager.
   */
451
  public function setCurrentPage($page) {
merlinofchaos's avatar
merlinofchaos committed
452
    $this->current_page = $page;
453
454

    // If the pager is already initialized, pass it through to the pager.
455
456
    if (!empty($this->pager)) {
      return $this->pager->set_current_page($page);
457
    }
merlinofchaos's avatar
merlinofchaos committed
458
459
460
461
462
  }

  /**
   * Get the current page from the pager.
   */
463
  public function getCurrentPage() {
464
    // If the pager is already initialized, pass it through to the pager.
465
466
    if (!empty($this->pager)) {
      return $this->pager->get_current_page();
merlinofchaos's avatar
merlinofchaos committed
467
    }
468
469
470
471

    if (isset($this->current_page)) {
      return $this->current_page;
    }
merlinofchaos's avatar
merlinofchaos committed
472
473
474
475
476
  }

  /**
   * Get the items per page from the pager.
   */
477
  public function getItemsPerPage() {
478
    // If the pager is already initialized, pass it through to the pager.
479
480
    if (!empty($this->pager)) {
      return $this->pager->get_items_per_page();
merlinofchaos's avatar
merlinofchaos committed
481
    }
482
483
484
485

    if (isset($this->items_per_page)) {
      return $this->items_per_page;
    }
merlinofchaos's avatar
merlinofchaos committed
486
487
488
489
490
  }

  /**
   * Set the items per page on the pager.
   */
491
  public function setItemsPerPage($items_per_page) {
merlinofchaos's avatar
merlinofchaos committed
492
493
494
    $this->items_per_page = $items_per_page;

    // If the pager is already initialized, pass it through to the pager.
495
496
    if (!empty($this->pager)) {
      $this->pager->set_items_per_page($items_per_page);
merlinofchaos's avatar
merlinofchaos committed
497
498
499
500
501
502
    }
  }

  /**
   * Get the pager offset from the pager.
   */
503
  public function getOffset() {
504
    // If the pager is already initialized, pass it through to the pager.
505
506
    if (!empty($this->pager)) {
      return $this->pager->get_offset();
merlinofchaos's avatar
merlinofchaos committed
507
    }
508
509
510
511

    if (isset($this->offset)) {
      return $this->offset;
    }
merlinofchaos's avatar
merlinofchaos committed
512
513
514
515
516
  }

  /**
   * Set the offset on the pager.
   */
517
  public function setOffset($offset) {
merlinofchaos's avatar
merlinofchaos committed
518
519
520
    $this->offset = $offset;

    // If the pager is already initialized, pass it through to the pager.
521
522
    if (!empty($this->pager)) {
      $this->pager->set_offset($offset);
merlinofchaos's avatar
merlinofchaos committed
523
524
525
526
527
528
    }
  }

  /**
   * Determine if the pager actually uses a pager.
   */
529
  public function usePager() {
530
531
    if (!empty($this->pager)) {
      return $this->pager->use_pager();
merlinofchaos's avatar
merlinofchaos committed
532
533
534
535
536
537
538
539
    }
  }

  /**
   * Whether or not AJAX should be used. If AJAX is used, paging,
   * tablesorting and exposed filters will be fetched via an AJAX call
   * rather than a page refresh.
   */
540
  public function setUseAJAX($use_ajax) {
merlinofchaos's avatar
merlinofchaos committed
541
542
543
544
545
546
547
    $this->use_ajax = $use_ajax;
  }

  /**
   * Set the exposed filters input to an array. If unset they will be taken
   * from $_GET when the time comes.
   */
548
  public function setExposedInput($filters) {
merlinofchaos's avatar
merlinofchaos committed
549
550
551
552
553
554
    $this->exposed_input = $filters;
  }

  /**
   * Figure out what the exposed input for this view is.
   */
555
  public function getExposedInput() {
merlinofchaos's avatar
merlinofchaos committed
556
557
558
    // Fill our input either from $_GET or from something previously set on the
    // view.
    if (empty($this->exposed_input)) {
559
      $this->exposed_input = drupal_container()->get('request')->query->all();
merlinofchaos's avatar
merlinofchaos committed
560
561
562
563
564
565
566
567
568
569
570
571
572
      // unset items that are definitely not our input:
      foreach (array('page', 'q') as $key) {
        if (isset($this->exposed_input[$key])) {
          unset($this->exposed_input[$key]);
        }
      }

      // If we have no input at all, check for remembered input via session.

      // If filters are not overridden, store the 'remember' settings on the
      // default display. If they are, store them on this display. This way,
      // multiple displays in the same view can share the same filters and
      // remember settings.
573
      $display_id = ($this->display_handler->isDefaulted('filters')) ? 'default' : $this->current_display;
merlinofchaos's avatar
merlinofchaos committed
574

575
576
      if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->storage->get('name')][$display_id])) {
        $this->exposed_input = $_SESSION['views'][$this->storage->get('name')][$display_id];
merlinofchaos's avatar
merlinofchaos committed
577
578
579
580
581
582
583
584
585
      }
    }

    return $this->exposed_input;
  }

  /**
   * Set the display for this view and initialize the display handler.
   */
586
  public function initDisplay() {
merlinofchaos's avatar
merlinofchaos committed
587
588
589
590
    if (isset($this->current_display)) {
      return TRUE;
    }

591
    // Initialize the display cache array.
592
    $this->displayHandlers = new DisplayBag($this, drupal_container()->get('plugin.manager.views.display'));
merlinofchaos's avatar
merlinofchaos committed
593
594

    $this->current_display = 'default';
595
    $this->display_handler = $this->displayHandlers['default'];
merlinofchaos's avatar
merlinofchaos committed
596
597
598
599
600
601
602

    return TRUE;
  }

  /**
   * Get the first display that is accessible to the user.
   *
603
   * @param array|string $displays
merlinofchaos's avatar
merlinofchaos committed
604
   *   Either a single display id or an array of display ids.
605
606
607
   *
   * @return string
   *   The first accessible display id, at least default.
merlinofchaos's avatar
merlinofchaos committed
608
   */
609
  public function chooseDisplay($displays) {
merlinofchaos's avatar
merlinofchaos committed
610
611
612
613
    if (!is_array($displays)) {
      return $displays;
    }

614
    $this->initDisplay();
merlinofchaos's avatar
merlinofchaos committed
615
616

    foreach ($displays as $display_id) {
617
      if ($this->displayHandlers[$display_id]->access()) {
merlinofchaos's avatar
merlinofchaos committed
618
619
620
621
622
623
624
625
626
627
628
629
630
        return $display_id;
      }
    }

    return 'default';
  }

  /**
   * Set the display as current.
   *
   * @param $display_id
   *   The id of the display to mark as current.
   */
631
  public function setDisplay($display_id = NULL) {
merlinofchaos's avatar
merlinofchaos committed
632
633
    // If we have not already initialized the display, do so. But be careful.
    if (empty($this->current_display)) {
634
      $this->initDisplay();
merlinofchaos's avatar
merlinofchaos committed
635
636
637
638
639
640
641
642

      // If handlers were not initialized, and no argument was sent, set up
      // to the default display.
      if (empty($display_id)) {
        $display_id = 'default';
      }
    }

643
    $display_id = $this->chooseDisplay($display_id);
merlinofchaos's avatar
merlinofchaos committed
644
645
646
647
648
649
650

    // If no display id sent in and one wasn't chosen above, we're finished.
    if (empty($display_id)) {
      return FALSE;
    }

    // Ensure the requested display exists.
651
    if (empty($this->displayHandlers[$display_id])) {
merlinofchaos's avatar
merlinofchaos committed
652
      $display_id = 'default';
653
      if (empty($this->displayHandlers[$display_id])) {
654
        debug(format_string('set_display() called with invalid display ID @display.', array('@display' => $display_id)));
merlinofchaos's avatar
merlinofchaos committed
655
656
657
658
659
660
661
662
        return FALSE;
      }
    }

    // Set the current display.
    $this->current_display = $display_id;

    // Ensure requested display has a working handler.
663
    if (empty($this->displayHandlers[$display_id])) {
merlinofchaos's avatar
merlinofchaos committed
664
665
666
667
      return FALSE;
    }

    // Set a shortcut
668
    $this->display_handler = $this->displayHandlers[$display_id];
merlinofchaos's avatar
merlinofchaos committed
669
670
671
672
673
674
675
676
677
678

    return TRUE;
  }

  /**
   * Find and initialize the style plugin.
   *
   * Note that arguments may have changed which style plugin we use, so
   * check the view object first, then ask the display handler.
   */
679
  public function initStyle() {
merlinofchaos's avatar
merlinofchaos committed
680
681
682
683
684
    if (isset($this->style_plugin)) {
      return is_object($this->style_plugin);
    }

    if (!isset($this->plugin_name)) {
685
686
687
      $style = $this->display_handler->getOption('style');
      $this->plugin_name = $style['type'];
      $this->style_options = $style['options'];
merlinofchaos's avatar
merlinofchaos committed
688
689
    }

690
    $this->style_plugin = drupal_container()->get("plugin.manager.views.style")->createInstance($this->plugin_name);
merlinofchaos's avatar
merlinofchaos committed
691
692
693
694
695
696

    if (empty($this->style_plugin)) {
      return FALSE;
    }

    // init the new style handler with data.
697
    $this->style_plugin->init($this, $this->display_handler, $this->style_options);
merlinofchaos's avatar
merlinofchaos committed
698
699
700
701
702
703
    return TRUE;
  }

  /**
   * Acquire and attach all of the handlers.
   */
704
  public function initHandlers() {
705
    $this->initDisplay();
merlinofchaos's avatar
merlinofchaos committed
706
    if (empty($this->inited)) {
707
      foreach ($this::viewsHandlerTypes() as $key => $info) {
708
        $this->_initHandler($key, $info);
merlinofchaos's avatar
merlinofchaos committed
709
710
711
712
713
714
715
716
717
718
719
      }
      $this->inited = TRUE;
    }
  }

  /**
   * Initialize the pager
   *
   * Like style initialization, pager initialization is held until late
   * to allow for overrides.
   */
720
  public function initPager() {
721
    if (!isset($this->pager)) {
722
      $this->pager = $this->display_handler->getPlugin('pager');
merlinofchaos's avatar
merlinofchaos committed
723

724
725
      if ($this->pager->use_pager()) {
        $this->pager->set_current_page($this->current_page);
merlinofchaos's avatar
merlinofchaos committed
726
727
728
729
730
      }

      // These overrides may have been set earlier via $view->set_*
      // functions.
      if (isset($this->items_per_page)) {
731
        $this->pager->set_items_per_page($this->items_per_page);
merlinofchaos's avatar
merlinofchaos committed
732
733
734
      }

      if (isset($this->offset)) {
735
        $this->pager->set_offset($this->offset);
merlinofchaos's avatar
merlinofchaos committed
736
737
738
739
      }
    }
  }

740
741
742
  /**
   * Render the pager, if necessary.
   */
743
  public function renderPager($exposed_input) {
744
745
746
747
748
749
750
    if (!empty($this->pager) && $this->pager->use_pager()) {
      return $this->pager->render($exposed_input);
    }

    return '';
  }

merlinofchaos's avatar
merlinofchaos committed
751
752
753
754
  /**
   * Create a list of base tables eligible for this view. Used primarily
   * for the UI. Display must be already initialized.
   */
755
  public function getBaseTables() {
merlinofchaos's avatar
merlinofchaos committed
756
    $base_tables = array(
757
      $this->storage->get('base_table') => TRUE,
merlinofchaos's avatar
merlinofchaos committed
758
759
760
      '#global' => TRUE,
    );

761
    foreach ($this->display_handler->getHandlers('relationship') as $handler) {
merlinofchaos's avatar
merlinofchaos committed
762
763
764
765
766
767
      $base_tables[$handler->definition['base']] = TRUE;
    }
    return $base_tables;
  }

  /**
768
   * Run the preQuery() on all active handlers.
merlinofchaos's avatar
merlinofchaos committed
769
   */
770
  protected function _preQuery() {
771
    foreach ($this::viewsHandlerTypes() as $key => $info) {
merlinofchaos's avatar
merlinofchaos committed
772
773
774
775
      $handlers = &$this->$key;
      $position = 0;
      foreach ($handlers as $id => $handler) {
        $handlers[$id]->position = $position;
776
        $handlers[$id]->preQuery();
merlinofchaos's avatar
merlinofchaos committed
777
778
779
780
781
782
        $position++;
      }
    }
  }

  /**
783
   * Run the postExecute() on all active handlers.
merlinofchaos's avatar
merlinofchaos committed
784
   */
785
  protected function _postExecute() {
786
    foreach ($this::viewsHandlerTypes() as $key => $info) {
merlinofchaos's avatar
merlinofchaos committed
787
788
      $handlers = &$this->$key;
      foreach ($handlers as $id => $handler) {
789
        $handlers[$id]->postExecute($this->result);
merlinofchaos's avatar
merlinofchaos committed
790
791
792
793
794
795
796
797
798
799
      }
    }
  }

  /**
   * Attach all of the handlers for each type.
   *
   * @param $key
   *   One of 'argument', 'field', 'sort', 'filter', 'relationship'
   * @param $info
800
   *   The $info from viewsHandlerTypes for this object.
merlinofchaos's avatar
merlinofchaos committed
801
   */
802
  protected function _initHandler($key, $info) {
merlinofchaos's avatar
merlinofchaos committed
803
    // Load the requested items from the display onto the object.
804
    $this->$key = $this->display_handler->getHandlers($key);
merlinofchaos's avatar
merlinofchaos committed
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

    // This reference deals with difficult PHP indirection.
    $handlers = &$this->$key;

    // Run through and test for accessibility.
    foreach ($handlers as $id => $handler) {
      if (!$handler->access()) {
        unset($handlers[$id]);
      }
    }
  }

  /**
   * Build all the arguments.
   */
820
  protected function _buildArguments() {
merlinofchaos's avatar
merlinofchaos committed
821
822
823
824
825
826
827
828
829
830
    // Initially, we want to build sorts and fields. This can change, though,
    // if we get a summary view.
    if (empty($this->argument)) {
      return TRUE;
    }

    // build arguments.
    $position = -1;

    // Create a title for use in the breadcrumb trail.
831
    $title = $this->display_handler->getOption('title');
merlinofchaos's avatar
merlinofchaos committed
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847

    $this->build_info['breadcrumb'] = array();
    $breadcrumb_args = array();
    $substitutions = array();

    $status = TRUE;

    // Iterate through each argument and process.
    foreach ($this->argument as $id => $arg) {
      $position++;
      $argument = &$this->argument[$id];

      if ($argument->broken()) {
        continue;
      }

848
      $argument->setRelationship();
merlinofchaos's avatar
merlinofchaos committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865

      $arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
      $argument->position = $position;

      if (isset($arg) || $argument->has_default_argument()) {
        if (!isset($arg)) {
          $arg = $argument->get_default_argument();
          // make sure default args get put back.
          if (isset($arg)) {
            $this->args[$position] = $arg;
          }
          // remember that this argument was computed, not passed on the URL.
          $argument->is_default = TRUE;
        }

        // Set the argument, which will also validate that the argument can be set.
        if (!$argument->set_argument($arg)) {
866
          $status = $argument->validateFail($arg);
merlinofchaos's avatar
merlinofchaos committed
867
868
869
870
871
872
873
874
          break;
        }

        if ($argument->is_exception()) {
          $arg_title = $argument->exception_title();
        }
        else {
          $arg_title = $argument->get_title();
875
          $argument->query($this->display_handler->useGroupBy());
merlinofchaos's avatar
merlinofchaos committed
876
877
878
879
880
881
882
883
        }

        // Add this argument's substitution
        $substitutions['%' . ($position + 1)] = $arg_title;
        $substitutions['!' . ($position + 1)] = strip_tags(decode_entities($arg));

        // Since we're really generating the breadcrumb for the item above us,
        // check the default action of this argument.
884
        if ($this->display_handler->usesBreadcrumb() && $argument->uses_breadcrumb()) {
885
          $path = $this->getUrl($breadcrumb_args);
merlinofchaos's avatar
merlinofchaos committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
          if (strpos($path, '%') === FALSE) {
            if (!empty($argument->options['breadcrumb_enable']) && !empty($argument->options['breadcrumb'])) {
              $breadcrumb = $argument->options['breadcrumb'];
            }
            else {
              $breadcrumb = $title;
            }
            $this->build_info['breadcrumb'][$path] = str_replace(array_keys($substitutions), $substitutions, $breadcrumb);
          }
        }

        // Allow the argument to muck with this breadcrumb.
        $argument->set_breadcrumb($this->build_info['breadcrumb']);

        // Test to see if we should use this argument's title
        if (!empty($argument->options['title_enable']) && !empty($argument->options['title'])) {
          $title = $argument->options['title'];
        }

        $breadcrumb_args[] = $arg;
      }
      else {
        // determine default condition and handle.
        $status = $argument->default_action();
        break;
      }

      // Be safe with references and loops:
      unset($argument);
    }

    // set the title in the build info.
    if (!empty($title)) {
      $this->build_info['title'] = $title;
    }

    // Store the arguments for later use.
    $this->build_info['substitutions'] = $substitutions;

    return $status;
  }

  /**
   * Do some common building initialization.
   */
931
  public function initQuery() {
merlinofchaos's avatar
merlinofchaos committed
932
933
934
935
936
937
938
939
940
    if (!empty($this->query)) {
      $class = get_class($this->query);
      if ($class && $class != 'stdClass') {
        // return if query is already initialized.
        return TRUE;
      }
    }

    // Create and initialize the query object.
941
    $views_data = drupal_container()->get('views.views_data')->get($this->storage->get('base_table'));
942
    $this->storage->set('base_field', !empty($views_data['table']['base']['field']) ? $views_data['table']['base']['field'] : '');
merlinofchaos's avatar
merlinofchaos committed
943
944
945
946
    if (!empty($views_data['table']['base']['database'])) {
      $this->base_database = $views_data['table']['base']['database'];
    }

947
    $this->query = $this->display_handler->getPlugin('query');
merlinofchaos's avatar
merlinofchaos committed
948
949
950
951
952
953
    return TRUE;
  }

  /**
   * Build the query for the view.
   */
954
  public function build($display_id = NULL) {
merlinofchaos's avatar
merlinofchaos committed
955
956
957
958
959
    if (!empty($this->built)) {
      return;
    }

    if (empty($this->current_display) || $display_id) {
960
      if (!$this->setDisplay($display_id)) {
merlinofchaos's avatar
merlinofchaos committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
        return FALSE;
      }
    }

    // Let modules modify the view just prior to building it.
    foreach (module_implements('views_pre_build') as $module) {
      $function = $module . '_views_pre_build';
      $function($this);
    }

    // Attempt to load from cache.
    // @todo Load a build_info from cache.

    $start = microtime(TRUE);
    // If that fails, let's build!
    $this->build_info = array(
      'query' => '',
      'count_query' => '',
      'query_args' => array(),
    );

982
    $this->initQuery();
merlinofchaos's avatar
merlinofchaos committed
983
984
985
986
987
988
989

    // Call a module hook and see if it wants to present us with a
    // pre-built query or instruct us not to build the query for
    // some reason.
    // @todo: Implement this. Use the same mechanism Panels uses.

    // Run through our handlers and ensure they have necessary information.
990
    $this->initHandlers();
merlinofchaos's avatar
merlinofchaos committed
991
992

    // Let the handlers interact with each other if they really want.
993
    $this->_preQuery();
merlinofchaos's avatar
merlinofchaos committed
994

995
996
    if ($this->display_handler->usesExposed()) {
      $exposed_form = $this->display_handler->getPlugin('exposed_form');
merlinofchaos's avatar
merlinofchaos committed
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
      $this->exposed_widgets = $exposed_form->render_exposed_form();
      if (form_set_error() || !empty($this->build_info['abort'])) {
        $this->built = TRUE;
        // Don't execute the query, but rendering will still be executed to display the empty text.
        $this->executed = TRUE;
        return empty($this->build_info['fail']);
      }
    }

    // Build all the relationships first thing.
    $this->_build('relationship');

    // Set the filtering groups.
    if (!empty($this->filter)) {
1011
      $filter_groups = $this->display_handler->getOption('filter_groups');
merlinofchaos's avatar
merlinofchaos committed
1012
1013
      if ($filter_groups) {
        $this->query->set_group_operator($filter_groups['operator']);
1014
        foreach ($filter_groups['groups'] as $id => $operator) {
merlinofchaos's avatar
merlinofchaos committed
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
          $this->query->set_where_group($operator, $id);
        }
      }
    }

    // Build all the filters.
    $this->_build('filter');

    $this->build_sort = TRUE;

    // Arguments can, in fact, cause this whole thing to abort.
1026
    if (!$this->_buildArguments()) {
merlinofchaos's avatar
merlinofchaos committed
1027
      $this->build_time = microtime(TRUE) - $start;
1028
      $this->attachDisplays();
merlinofchaos's avatar
merlinofchaos committed
1029
1030
1031
1032
1033
1034
      return $this->built;
    }

    // Initialize the style; arguments may have changed which style we use,
    // so waiting as long as possible is important. But we need to know
    // about the style when we go to build fields.
1035
    if (!$this->initStyle()) {
merlinofchaos's avatar
merlinofchaos committed
1036
1037
1038
1039
      $this->build_info['fail'] = TRUE;
      return FALSE;
    }

1040
    if ($this->style_plugin->usesFields()) {
merlinofchaos's avatar
merlinofchaos committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
      $this->_build('field');
    }

    // Build our sort criteria if we were instructed to do so.
    if (!empty($this->build_sort)) {
      // Allow the style handler to deal with sorting.
      if ($this->style_plugin->build_sort()) {
        $this->_build('sort');
      }
      // allow the plugin to build second sorts as well.
      $this->style_plugin->build_sort_post();
    }

    // Allow area handlers to affect the query.
    $this->_build('header');
    $this->_build('footer');
    $this->_build('empty');

    // Allow display handler to affect the query:
1060
    $this->display_handler->query($this->display_handler->useGroupBy());
merlinofchaos's avatar
merlinofchaos committed
1061
1062

    // Allow style handler to affect the query:
1063
    $this->style_plugin->query($this->display_handler->useGroupBy());
merlinofchaos's avatar
merlinofchaos committed
1064
1065
1066
1067
1068
1069

    // Allow exposed form to affect the query:
    if (isset($exposed_form)) {
      $exposed_form->query();
    }

1070
    if (config('views.settings')->get('sql_signature')) {
merlinofchaos's avatar
merlinofchaos committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
      $this->query->add_signature($this);
    }

    // Let modules modify the query just prior to finalizing it.
    $this->query->alter($this);

    // Only build the query if we weren't interrupted.
    if (empty($this->built)) {
      // Build the necessary info to execute the query.
      $this->query->build($this);
    }

    $this->built = TRUE;
    $this->build_time = microtime(TRUE) - $start;

    // Attach displays
1087
    $this->attachDisplays();
merlinofchaos's avatar
merlinofchaos committed
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099

    // Let modules modify the view just after building it.
    foreach (module_implements('views_post_build') as $module) {
      $function = $module . '_views_post_build';
      $function($this);
    }

    return TRUE;
  }

  /**
   * Internal method to build an individual set of handlers.
1100
   *
1101
1102
   * @todo Some filter needs this function, even it is internal.
   *
1103
1104
1105
   * @param string $key
   *    The type of handlers (filter etc.) which should be iterated over to
   *    build the relationship and query information.
merlinofchaos's avatar
merlinofchaos committed
1106
   */
1107
  public function _build($key) {
merlinofchaos's avatar
merlinofchaos committed
1108
1109
    $handlers = &$this->$key;
    foreach ($handlers as $id => $data) {
1110

merlinofchaos's avatar
merlinofchaos committed
1111
      if (!empty($handlers[$id]) && is_object($handlers[$id])) {
1112
        $multiple_exposed_input = array(0 => NULL);
1113
        if ($handlers[$id]->multipleExposedInput()) {
1114
1115
1116
1117
1118
1119
          $multiple_exposed_input = $handlers[$id]->group_multiple_exposed_input($this->exposed_data);
        }
        foreach ($multiple_exposed_input as $group_id) {
          // Give this handler access to the exposed filter input.
          if (!empty($this->exposed_data)) {
            $converted = FALSE;
1120
            if ($handlers[$id]->isAGroup()) {
1121
1122
1123
1124
1125
1126
              $converted = $handlers[$id]->convert_exposed_input($this->exposed_data, $group_id);
              $handlers[$id]->store_group_input($this->exposed_data, $converted);
              if (!$converted) {
                continue;
              }
            }
1127
1128
            $rc = $handlers[$id]->acceptExposedInput($this->exposed_data);
            $handlers[$id]->storeExposedInput($this->exposed_data, $rc);
1129
1130
1131
            if (!$rc) {
              continue;
            }
merlinofchaos's avatar
merlinofchaos committed
1132
          }
1133
          $handlers[$id]->setRelationship();
1134
          $handlers[$id]->query($this->display_handler->useGroupBy());
merlinofchaos's avatar
merlinofchaos committed
1135
1136
1137
1138
1139
1140
1141
        }
      }
    }
  }

  /**
   * Execute the view's query.
1142
1143
1144
1145
1146
1147
1148
   *
   * @param string $display_id
   *   The machine name of the display, which should be executed.
   *
   * @return bool
   *   Return whether the executing was successful, for example an argument
   *   could stop the process.
merlinofchaos's avatar
merlinofchaos committed
1149
   */
1150
  public function execute($display_id = NULL) {
merlinofchaos's avatar
merlinofchaos committed
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
    if (empty($this->built)) {
      if (!$this->build($display_id)) {
        return FALSE;
      }
    }

    if (!empty($this->executed)) {
      return TRUE;
    }

    // Don't allow to use deactivated displays, but display them on the live preview.
1162
    if (!$this->display_handler->isEnabled() && empty($this->live_preview)) {
merlinofchaos's avatar
merlinofchaos committed
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
      $this->build_info['fail'] = TRUE;
      return FALSE;
    }

    // Let modules modify the view just prior to executing it.
    foreach (module_implements('views_pre_execute') as $module) {
      $function = $module . '_views_pre_execute';
      $function($this);
    }

    // Check for already-cached results.
    if (!empty($this->live_preview)) {
1175
      $cache = $this->display_handler->getPlugin('cache', 'none');
merlinofchaos's avatar
merlinofchaos committed
1176
1177
    }
    else {
1178
      $cache = $this->display_handler->getPlugin('cache');
merlinofchaos's avatar
merlinofchaos committed
1179
    }
1180
    if ($cache->cache_get('results')) {
1181
1182
1183
      if ($this->pager->use_pager()) {
        $this->pager->total_items = $this->total_rows;
        $this->pager->update_page_info();
merlinofchaos's avatar
merlinofchaos committed
1184
1185
1186
1187
      }
    }
    else {
      $this->query->execute($this);
1188
1189
1190
      // Enforce the array key rule as documented in
      // views_plugin_query::execute().
      $this->result = array_values($this->result);
1191
      $this->_postExecute();
1192
      $cache->cache_set('results');
merlinofchaos's avatar
merlinofchaos committed
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
    }

    // Let modules modify the view just after executing it.
    foreach (module_implements('views_post_execute') as $module) {
      $function = $module . '_views_post_execute';
      $function($this);
    }

    $this->executed = TRUE;
  }

  /**
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
   * Render this view for a certain display.
   *
   * Note: You should better use just the preview function if you want to
   * render a view.
   *
   * @param string $display_id
   *   The machine name of the display, which should be rendered.
   *
   * @return (string|NULL)
   *   Return the output of the rendered view or NULL if something failed in the process.
merlinofchaos's avatar
merlinofchaos committed
1215
   */
1216
  public function render($display_id = NULL) {
merlinofchaos's avatar
merlinofchaos committed
1217
1218
1219
1220
1221
1222
    $this->execute($display_id);

    // Check to see if the build failed.
    if (!empty($this->build_info['fail'])) {
      return;
    }
1223
    if (!empty($this->build_info['denied'])) {
merlinofchaos's avatar
merlinofchaos committed
1224
1225
1226
1227
      return;
    }

    drupal_theme_initialize();
1228
    $config = config('views.settings');
merlinofchaos's avatar
merlinofchaos committed
1229

1230
1231
1232
    // Set the response so other parts can alter it.
    $this->response = new Response('', 200);

merlinofchaos's avatar
merlinofchaos committed
1233
    $start = microtime(TRUE);
1234
    if (!empty($this->live_preview) && $config->get('ui.show.additional_queries')) {
1235
      $this->startQueryCapture();
merlinofchaos's avatar
merlinofchaos committed
1236
1237
    }

1238
    $exposed_form = $this->display_handler->getPlugin('exposed_form');
merlinofchaos's avatar
merlinofchaos committed
1239
1240
1241
1242
1243
1244
1245
    $exposed_form->pre_render($this->result);

    // Check for already-cached output.
    if (!empty($this->live_preview)) {
      $cache = FALSE;
    }
    else {
1246
      $cache = $this->display_handler->getPlugin('cache');
merlinofchaos's avatar
merlinofchaos committed
1247
1248
1249
1250
1251
1252
1253
1254
1255
    }
    if ($cache && $cache->cache_get('output')) {
    }
    else {
      if ($cache) {
        $cache->cache_start();
      }

      // Run pre_render for the pager as it might change the result.
1256
1257
      if (!empty($this->pager)) {
        $this->pager->pre_render($this->result);
merlinofchaos's avatar
merlinofchaos committed
1258
1259
1260
      }

      // Initialize the style plugin.
1261
      $this->initStyle();
merlinofchaos's avatar
merlinofchaos committed
1262
1263
1264

      // Give field handlers the opportunity to perform additional queries
      // using the entire resultset prior to rendering.
1265
      if ($this->style_plugin->usesFields()) {
merlinofchaos's avatar
merlinofchaos committed
1266
1267
1268
1269
1270
1271
1272
1273
1274
        foreach ($this->field as $id => $handler) {
          if (!empty($this->field[$id])) {
            $this->field[$id]->pre_render($this->result);
          }
        }
      }

      $this->style_plugin->pre_render($this->result);

1275
1276
1277
1278
1279
1280
1281
      // Let each area handler have access to the result set.
      foreach (array('header', 'footer', 'empty') as $area) {
        foreach ($this->{$area} as $handler) {
          $handler->preRender($this->result);
        }
      }

merlinofchaos's avatar
merlinofchaos committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
      // Let modules modify the view just prior to rendering it.
      foreach (module_implements('views_pre_render') as $module) {
        $function = $module . '_views_pre_render';
        $function($this);
      }

      // Let the themes play too, because pre render is a very themey thing.
      foreach ($GLOBALS['base_theme_info'] as $base) {
        $function = $base->name . '_views_pre_render';
        if (function_exists($function)) {
          $function($this);
        }
      }
      $function = $GLOBALS['theme'] . '_views_pre_render';
      if (function_exists($function)) {
        $function($this);
      }

      $this->display_handler->output = $this->display_handler->render();
      if ($cache) {
        $cache->cache_set('output');
      }
    }

    $exposed_form->post_render($this->display_handler->output);

    if ($cache) {
      $cache->post_render($this->display_handler->output);
    }

    // Let modules modify the view output after it is rendered.
    foreach (module_implements('views_post_render') as $module) {
      $function = $module . '_views_post_render';
      $function($this, $this->display_handler->output, $cache);
    }

    // Let the themes play too, because post render is a very themey thing.
    foreach ($GLOBALS['base_theme_info'] as $base) {
      $function = $base->name . '_views_post_render';
      if (function_exists($function)) {
        $function($this);
      }
    }
    $function = $GLOBALS['theme'] . '_views_post_render';
    if (function_exists($function)) {
      $function($this, $this->display_handler->output, $cache);
    }

1330
    if (!empty($this->live_preview) && $config->get('ui.show.additional_queries')) {
1331
      $this->endQueryCapture();
merlinofchaos's avatar
merlinofchaos committed
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
    }
    $this->render_time = microtime(TRUE) - $start;

    return $this->display_handler->output;
  }

  /**
   * Execute the given display, with the given arguments.
   * To be called externally by whatever mechanism invokes the view,
   * such as a page callback, hook_block, etc.
   *
   * This function should NOT be used by anything external as this
   * returns data in the format specified by the display. It can also
   * have other side effects that are only intended for the 'proper'
   * use of the display, such as setting page titles and breadcrumbs.
   *
1348
   * If you simply want to view the display, use View::preview() instead.
merlinofchaos's avatar
merlinofchaos committed
1349
   */
1350
1351
1352
  public function executeDisplay($display_id = NULL, $args = array()) {
    if (empty($this->current_display) || $this->current_display != $this->chooseDisplay($display_id)) {
      if (!$this->setDisplay($display_id)) {
merlinofchaos's avatar
merlinofchaos committed
1353
1354
1355
1356
        return FALSE;
      }
    }

1357
    $this->preExecute($args);
merlinofchaos's avatar
merlinofchaos committed
1358
1359
1360
1361

    // Execute the view
    $output = $this->display_handler->execute();

1362
    $this->postExecute();
merlinofchaos's avatar
merlinofchaos committed
1363
1364
1365
1366
1367
1368
1369
1370
1371
    return $output;
  }

  /**
   * Preview the given display, with the given arguments.
   *
   * To be called externally, probably by an AJAX handler of some flavor.
   * Can also be called when views are embedded, as this guarantees
   * normalized output.
1372
1373
1374
1375
1376
   *
   * This function does not do any access checks on the view. It is the
   * responsibility of the caller to check $view->access() or implement other
   * access logic. To render the view normally with access checks, use
   * views_embed_view() instead.
merlinofchaos's avatar
merlinofchaos committed
1377
   */
1378
  public function preview($display_id = NULL, $args = array()) {
merlinofchaos's avatar
merlinofchaos committed
1379
    if (empty($this->current_display) || ((!empty($display_id)) && $this->current_display != $display_id)) {
1380
      if (!$this->setDisplay($display_id)) {
merlinofchaos's avatar
merlinofchaos committed
1381
1382
1383
1384
1385
        return FALSE;
      }
    }

    $this->preview = TRUE;
1386
    $this->preExecute($args);
merlinofchaos's avatar
merlinofchaos committed
1387
1388
1389
    // Preview the view.
    $output = $this->display_handler->preview();

1390
    $this->postExecute();
merlinofchaos's avatar
merlinofchaos committed
1391
1392
1393
1394
1395
1396
1397
    return $output;
  }

  /**
   * Run attachments and let the display do what it needs to do prior
   * to running.
   */
1398
  public function preExecute($args = array()) {
merlinofchaos's avatar
merlinofchaos committed
1399
1400
1401
1402
1403
1404
1405
    $this->old_view[] = views_get_current_view();
    views_set_current_view($this);
    $display_id = $this->current_display;

    // Prepare the view with the information we have, but only if we were
    // passed arguments, as they may have been set previously.
    if ($args) {
1406
      $this->setArguments($args);
merlinofchaos's avatar
merlinofchaos committed
1407
1408
1409
1410
1411
1412
1413
1414
1415
    }

    // Let modules modify the view just prior to executing it.
    foreach (module_implements('views_pre_view') as $module) {
      $function = $module . '_views_pre_view';
      $function($this, $display_id, $this->args);
    }

    // Allow hook_views_pre_view() to set the dom_id, then ensure it is set.
1416
    $this->dom_id = !empty($this->dom_id) ? $this->dom_id : md5($this->storage->get('name') . REQUEST_TIME . rand());
merlinofchaos's avatar
merlinofchaos committed
1417
1418

    // Allow the display handler to set up for execution
1419
    $this->display_handler->preExecute();
merlinofchaos's avatar
merlinofchaos committed
1420
1421
1422
1423
1424
  }

  /**
   * Unset the current view, mostly.
   */
1425
  public function postExecute() {
merlinofchaos's avatar
merlinofchaos committed
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
    // unset current view so we can be properly destructed later on.
    // Return the previous value in case we're an attachment.

    if ($this->old_view) {
      $old_view = array_pop($this->old_view);
    }

    views_set_current_view(isset($old_view) ? $old_view : FALSE);
  }

  /**
   * Run attachment displays for the view.
   */
damiankloip's avatar