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

/**
 * @file
5
 * Contains Drupal\views\Plugin\views\display\DisplayPluginBase.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\display;
9

10
use Drupal\views\ViewExecutable;
11
use \Drupal\views\Plugin\views\PluginBase;
12

merlinofchaos's avatar
merlinofchaos committed
13 14 15 16 17 18 19
/**
 * @defgroup views_display_plugins Views display plugins
 * @{
 * Display plugins control how Views interact with the rest of Drupal.
 *
 * They can handle creating Views from a Drupal page hook; they can
 * handle creating Views from a Drupal block hook. They can also
20
 * handle creating Views from an external module source.
merlinofchaos's avatar
merlinofchaos committed
21 22 23 24 25 26
 */

/**
 * The default display plugin handler. Display plugins handle options and
 * basic mechanisms for different output methods.
 */
27
abstract class DisplayPluginBase extends PluginBase {
28

merlinofchaos's avatar
merlinofchaos committed
29 30 31
  /**
   * The top object of a view.
   *
32
   * @var Drupal\views\ViewExecutable
merlinofchaos's avatar
merlinofchaos committed
33 34 35 36 37
   */
  var $view = NULL;

  var $handlers = array();

38 39 40 41 42 43 44
  /**
   * An array of instantiated plugins used in this display.
   *
   * @var array
   */
  protected $plugins = array();

merlinofchaos's avatar
merlinofchaos committed
45 46 47 48 49
  /**
   * Stores all available display extenders.
   */
  var $extender = array();

50 51 52
  /**
   * Overrides Drupal\views\Plugin\Plugin::$usesOptions.
   */
53
  protected $usesOptions = TRUE;
54

55 56 57
  /**
   * Stores the rendered output of the display.
   *
58
   * @see View::render
59 60 61 62
   * @var string
   */
  public $output = NULL;

63 64 65 66 67 68 69
  /**
   * Whether the display allows the use of AJAX or not.
   *
   * @var bool
   */
  protected $usesAJAX = TRUE;

70 71 72 73 74 75 76 77 78 79 80 81 82 83
  /**
   * Whether the display allows the use of a pager or not.
   *
   * @var bool
   */
  protected $usesPager = TRUE;

  /**
   * Whether the display allows the use of a 'more' link or not.
   *
   * @var bool
   */
  protected $usesMore = TRUE;

84 85 86 87 88 89 90 91
  /**
   * Whether the display allows attachments.
   *
   * @var bool
   *   TRUE if the display can use attachments, or FALSE otherwise.
   */
  protected $usesAttachments = FALSE;

92 93 94 95 96 97 98
  /**
   * Whether the display allows area plugins.
   *
   * @var bool
   */
  protected $usesAreas = TRUE;

99
  public function init(ViewExecutable $view, &$display, $options = NULL) {
100
    $this->setOptionDefaults($this->options, $this->defineOptions());
merlinofchaos's avatar
merlinofchaos committed
101 102 103 104 105 106
    $this->view = &$view;
    $this->display = &$display;

    // Load extenders as soon as possible.
    $this->extender = array();
    $extenders = views_get_enabled_display_extenders();
107
    if (!empty($extenders)) {
108
      $manager = drupal_container()->get('plugin.manager.views.display_extender');
merlinofchaos's avatar
merlinofchaos committed
109
      foreach ($extenders as $extender) {
110
        $plugin = $manager->createInstance($extender);
merlinofchaos's avatar
merlinofchaos committed
111 112 113 114 115 116 117 118 119 120 121
        if ($plugin) {
          $plugin->init($this->view, $this);
          $this->extender[$extender] = $plugin;
        }
      }
    }

    // Track changes that the user should know about.
    $changed = FALSE;

    // Make some modifications:
122 123
    if (!isset($options) && isset($display['display_options'])) {
      $options = $display['display_options'];
merlinofchaos's avatar
merlinofchaos committed
124 125
    }

126
    if ($this->isDefaultDisplay() && isset($options['defaults'])) {
merlinofchaos's avatar
merlinofchaos committed
127 128 129
      unset($options['defaults']);
    }

130
    views_include('cache');
131
    // Cache for unpackOptions, but not if we are in the ui.
merlinofchaos's avatar
merlinofchaos committed
132 133
    static $unpack_options = array();
    if (empty($view->editing)) {
134
      $cid = 'unpackOptions:' . md5(serialize(array($this->options, $options)));
merlinofchaos's avatar
merlinofchaos committed
135 136 137 138 139 140
      if (empty($unpack_options[$cid])) {
        $cache = views_cache_get($cid, TRUE);
        if (!empty($cache->data)) {
          $this->options = $cache->data;
        }
        else {
141
          $this->unpackOptions($this->options, $options);
merlinofchaos's avatar
merlinofchaos committed
142 143 144 145 146 147 148 149 150
          views_cache_set($cid, $this->options, TRUE);
        }
        $unpack_options[$cid] = $this->options;
      }
      else {
        $this->options = $unpack_options[$cid];
      }
    }
    else {
151
      $this->unpackOptions($this->options, $options);
merlinofchaos's avatar
merlinofchaos committed
152 153
    }

154
    // Convert the field_language and field_language_add_to_query settings.
155 156
    $field_language = $this->getOption('field_language');
    $field_language_add_to_query = $this->getOption('field_language_add_to_query');
157
    if (isset($field_langcode)) {
158 159
      $this->setOption('field_langcode', $field_language);
      $this->setOption('field_langcode_add_to_query', $field_language_add_to_query);
160 161 162
      $changed = TRUE;
    }

merlinofchaos's avatar
merlinofchaos committed
163 164 165 166 167 168
    // Mark the view as changed so the user has a chance to save it.
    if ($changed) {
      $this->view->changed = TRUE;
    }
  }

169
  public function destroy() {
merlinofchaos's avatar
merlinofchaos committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    parent::destroy();

    foreach ($this->handlers as $type => $handlers) {
      foreach ($handlers as $id => $handler) {
        if (is_object($handler)) {
          $this->handlers[$type][$id]->destroy();
        }
      }
    }

    if (isset($this->default_display)) {
      unset($this->default_display);
    }

    foreach ($this->extender as $extender) {
      $extender->destroy();
    }
  }

  /**
   * Determine if this display is the 'default' display which contains
   * fallback settings
   */
193
  public function isDefaultDisplay() { return FALSE; }
merlinofchaos's avatar
merlinofchaos committed
194 195 196 197 198

  /**
   * Determine if this display uses exposed filters, so the view
   * will know whether or not to build them.
   */
199
  public function usesExposed() {
merlinofchaos's avatar
merlinofchaos committed
200 201 202
    if (!isset($this->has_exposed)) {
      foreach ($this->handlers as $type => $value) {
        foreach ($this->view->$type as $id => $handler) {
203
          if ($handler->canExpose() && $handler->isExposed()) {
merlinofchaos's avatar
merlinofchaos committed
204 205 206 207 208 209
            // one is all we need; if we find it, return true.
            $this->has_exposed = TRUE;
            return TRUE;
          }
        }
      }
210
      $pager = $this->getPlugin('pager');
merlinofchaos's avatar
merlinofchaos committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
      if (isset($pager) && $pager->uses_exposed()) {
        $this->has_exposed = TRUE;
        return TRUE;
      }
      $this->has_exposed = FALSE;
    }

    return $this->has_exposed;
  }

  /**
   * Determine if this display should display the exposed
   * filters widgets, so the view will know whether or not
   * to render them.
   *
   * Regardless of what this function
   * returns, exposed filters will not be used nor
228
   * displayed unless usesExposed() returns TRUE.
merlinofchaos's avatar
merlinofchaos committed
229
   */
230
  public function displaysExposed() {
merlinofchaos's avatar
merlinofchaos committed
231 232 233 234
    return TRUE;
  }

  /**
235 236 237 238
   * Whether the display allows the use of AJAX or not.
   *
   * @return bool
   */
239
  public function usesAJAX() {
240 241 242 243 244 245 246
    return $this->usesAJAX;
  }

  /**
   * Whether the display is actually using AJAX or not.
   *
   * @return bool
merlinofchaos's avatar
merlinofchaos committed
247
   */
248
  public function isAJAXEnabled() {
249
    if ($this->usesAJAX()) {
250
      return $this->getOption('use_ajax');
merlinofchaos's avatar
merlinofchaos committed
251 252 253 254
    }
    return FALSE;
  }

255 256 257 258 259 260 261 262 263 264
  /**
   * Whether the display is enabled.
   *
   * @return bool
   *   Returns TRUE if the display is marked as enabled, else FALSE.
   */
  public function isEnabled() {
    return (bool) $this->getOption('enabled');
  }

merlinofchaos's avatar
merlinofchaos committed
265
  /**
266 267 268 269 270
   * Whether the display allows the use of a pager or not.
   *
   * @return bool
   */

271
  public function usesPager() {
272 273 274 275 276 277 278
    return $this->usesPager;
  }

  /**
   * Whether the display is using a pager or not.
   *
   * @return bool
merlinofchaos's avatar
merlinofchaos committed
279
   */
280
  public function isPagerEnabled() {
281
    if ($this->usesPager()) {
282
      $pager = $this->getPlugin('pager');
283 284 285
      if ($pager) {
        return $pager->use_pager();
      }
merlinofchaos's avatar
merlinofchaos committed
286
    }
287
    return FALSE;
merlinofchaos's avatar
merlinofchaos committed
288 289 290
  }

  /**
291 292 293 294
   * Whether the display allows the use of a 'more' link or not.
   *
   * @return bool
   */
295
  public function usesMore() {
296 297 298 299 300 301 302
    return $this->usesMore;
  }

  /**
   * Whether the display is using the 'more' link or not.
   *
   * @return bool
merlinofchaos's avatar
merlinofchaos committed
303
   */
304
  public function isMoreEnabled() {
305
    if ($this->usesMore()) {
306
      return $this->getOption('use_more');
merlinofchaos's avatar
merlinofchaos committed
307 308 309 310 311 312 313
    }
    return FALSE;
  }

  /**
   * Does the display have groupby enabled?
   */
314 315
  public function useGroupBy() {
    return $this->getOption('group_by');
merlinofchaos's avatar
merlinofchaos committed
316 317 318 319 320
  }

  /**
   * Should the enabled display more link be shown when no more items?
   */
321
  public function useMoreAlways() {
322
    if ($this->usesMore()) {
323
      return $this->getOption('useMoreAlways');
merlinofchaos's avatar
merlinofchaos committed
324 325 326 327 328 329 330
    }
    return FALSE;
  }

  /**
   * Does the display have custom link text?
   */
331
  public function useMoreText() {
332
    if ($this->usesMore()) {
333
      return $this->getOption('useMoreText');
merlinofchaos's avatar
merlinofchaos committed
334 335 336 337 338
    }
    return FALSE;
  }

  /**
339 340 341
   * Determines whether this display can use attachments.
   *
   * @return bool
merlinofchaos's avatar
merlinofchaos committed
342
   */
343
  public function acceptAttachments() {
344 345 346
    // To be able to accept attachments this display have to be able to use
    // attachments but at the same time, you cannot attach a display to itself.
    if (!$this->usesAttachments() || ($this->definition['id'] == $this->view->current_display)) {
merlinofchaos's avatar
merlinofchaos committed
347 348
      return FALSE;
    }
349

350
    if (!empty($this->view->argument) && $this->getOption('hide_attachment_summary')) {
merlinofchaos's avatar
merlinofchaos committed
351
      foreach ($this->view->argument as $argument_id => $argument) {
352
        if ($argument->needsStylePlugin() && empty($argument->argument_validated)) {
merlinofchaos's avatar
merlinofchaos committed
353 354 355 356
          return FALSE;
        }
      }
    }
357

merlinofchaos's avatar
merlinofchaos committed
358 359 360
    return TRUE;
  }

361 362 363 364 365
  /**
   * Returns whether the display can use attachments.
   *
   * @return bool
   */
366
  public function usesAttachments() {
367 368 369
    return $this->usesAttachments;
  }

370 371 372 373 374 375 376 377 378 379
  /**
   * Returns whether the display can use areas.
   *
   * @return bool
   *   TRUE if the display can use areas, or FALSE otherwise.
   */
  public function usesAreas() {
    return $this->usesAreas;
  }

merlinofchaos's avatar
merlinofchaos committed
380 381 382
  /**
   * Allow displays to attach to other views.
   */
383
  public function attachTo($display_id) { }
merlinofchaos's avatar
merlinofchaos committed
384 385 386 387 388

  /**
   * Static member function to list which sections are defaultable
   * and what items each section contains.
   */
389
  public function defaultableSections($section = NULL) {
merlinofchaos's avatar
merlinofchaos committed
390
    $sections = array(
391 392
      'access' => array('access'),
      'cache' => array('cache'),
merlinofchaos's avatar
merlinofchaos committed
393 394 395 396
      'title' => array('title'),
      'css_class' => array('css_class'),
      'use_ajax' => array('use_ajax'),
      'hide_attachment_summary' => array('hide_attachment_summary'),
397
      'hide_admin_links' => array('hide_admin_links'),
merlinofchaos's avatar
merlinofchaos committed
398 399 400
      'group_by' => array('group_by'),
      'query' => array('query'),
      'use_more' => array('use_more', 'use_more_always', 'use_more_text'),
401 402
      'use_more_always' => array('use_more', 'use_more_always', 'use_more_text'),
      'use_more_text' => array('use_more', 'use_more_always', 'use_more_text'),
merlinofchaos's avatar
merlinofchaos committed
403 404 405
      'link_display' => array('link_display', 'link_url'),

      // Force these to cascade properly.
406 407
      'style' => array('style', 'row'),
      'row' => array('style', 'row'),
merlinofchaos's avatar
merlinofchaos committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

      'pager' => array('pager', 'pager_options'),
      'pager_options' => array('pager', 'pager_options'),

      'exposed_form' => array('exposed_form', 'exposed_form_options'),
      'exposed_form_options' => array('exposed_form', 'exposed_form_options'),

      // These guys are special
      'header' => array('header'),
      'footer' => array('footer'),
      'empty' => array('empty'),
      'relationships' => array('relationships'),
      'fields' => array('fields'),
      'sorts' => array('sorts'),
      'arguments' => array('arguments'),
      'filters' => array('filters', 'filter_groups'),
      'filter_groups' => array('filters', 'filter_groups'),
    );

    // If the display cannot use a pager, then we cannot default it.
428
    if (!$this->usesPager()) {
merlinofchaos's avatar
merlinofchaos committed
429 430 431 432 433
      unset($sections['pager']);
      unset($sections['items_per_page']);
    }

    foreach ($this->extender as $extender) {
434
      $extender->defaultableSections($sections, $section);
merlinofchaos's avatar
merlinofchaos committed
435 436 437 438 439 440 441 442 443 444 445 446
    }

    if ($section) {
      if (!empty($sections[$section])) {
        return $sections[$section];
      }
    }
    else {
      return $sections;
    }
  }

447
  protected function defineOptions() {
merlinofchaos's avatar
merlinofchaos committed
448 449 450 451 452 453 454 455 456 457 458 459
    $options = array(
      'defaults' => array(
        'default' => array(
          'access' => TRUE,
          'cache' => TRUE,
          'query' => TRUE,
          'title' => TRUE,
          'css_class' => TRUE,

          'display_description' => FALSE,
          'use_ajax' => TRUE,
          'hide_attachment_summary' => TRUE,
460
          'hide_admin_links' => FALSE,
merlinofchaos's avatar
merlinofchaos committed
461 462 463 464 465 466 467 468 469 470
          'pager' => TRUE,
          'use_more' => TRUE,
          'use_more_always' => TRUE,
          'use_more_text' => TRUE,
          'exposed_form' => TRUE,

          'link_display' => TRUE,
          'link_url' => '',
          'group_by' => TRUE,

471 472
          'style' => TRUE,
          'row' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

          'header' => TRUE,
          'footer' => TRUE,
          'empty' => TRUE,

          'relationships' => TRUE,
          'fields' => TRUE,
          'sorts' => TRUE,
          'arguments' => TRUE,
          'filters' => TRUE,
          'filter_groups' => TRUE,
        ),
      ),

      'title' => array(
        'default' => '',
        'translatable' => TRUE,
      ),
      'enabled' => array(
        'default' => TRUE,
        'translatable' => FALSE,
        'bool' => TRUE,
      ),
      'display_comment' => array(
        'default' => '',
      ),
      'css_class' => array(
        'default' => '',
        'translatable' => FALSE,
      ),
      'display_description' => array(
        'default' => '',
        'translatable' => TRUE,
      ),
      'use_ajax' => array(
        'default' => FALSE,
        'bool' => TRUE,
      ),
      'hide_attachment_summary' => array(
        'default' => FALSE,
        'bool' => TRUE,
      ),
515 516 517 518
      'hide_admin_links' => array(
        'default' => FALSE,
        'bool' => TRUE,
      ),
merlinofchaos's avatar
merlinofchaos committed
519 520 521 522 523 524
      'use_more' => array(
        'default' => FALSE,
        'bool' => TRUE,
      ),
      'use_more_always' => array(
        'default' => FALSE,
525
        'bool' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
      ),
      'use_more_text' => array(
        'default' => 'more',
        'translatable' => TRUE,
      ),
      'link_display' => array(
        'default' => '',
      ),
      'link_url' => array(
        'default' => '',
      ),
      'group_by' => array(
        'default' => FALSE,
        'bool' => TRUE,
      ),
541
      'field_langcode' => array(
merlinofchaos's avatar
merlinofchaos committed
542 543
        'default' => '***CURRENT_LANGUAGE***',
      ),
544 545 546
      'field_langcode_add_to_query' => array(
        'default' => TRUE,
        'bool' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
547 548 549 550 551 552
      ),

      // These types are all plugins that can have individual settings
      // and therefore need special handling.
      'access' => array(
        'contains' => array(
553
          'type' => array('default' => 'none'),
554 555
          'options' => array('default' => array()),
        ),
merlinofchaos's avatar
merlinofchaos committed
556 557 558
      ),
      'cache' => array(
        'contains' => array(
559
          'type' => array('default' => 'none'),
560 561
          'options' => array('default' => array()),
        ),
merlinofchaos's avatar
merlinofchaos committed
562 563 564
      ),
      'query' => array(
        'contains' => array(
565 566
          'type' => array('default' => 'views_query'),
          'options' => array('default' => array()),
merlinofchaos's avatar
merlinofchaos committed
567 568 569 570
         ),
      ),
      'exposed_form' => array(
        'contains' => array(
571 572
          'type' => array('default' => 'basic'),
          'options' => array('default' => array()),
merlinofchaos's avatar
merlinofchaos committed
573 574 575 576
         ),
      ),
      'pager' => array(
        'contains' => array(
577 578
          'type' => array('default' => 'full'),
          'options' => array('default' => array()),
merlinofchaos's avatar
merlinofchaos committed
579 580
         ),
      ),
581 582 583 584 585
      'style' => array(
        'contains' => array(
          'type' => array('default' => 'default'),
          'options' => array('default' => array()),
        ),
merlinofchaos's avatar
merlinofchaos committed
586
      ),
587 588 589 590 591
      'row' => array(
        'contains' => array(
          'type' => array('default' => 'fields'),
          'options' => array('default' => array()),
        ),
merlinofchaos's avatar
merlinofchaos committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
      ),

      'exposed_block' => array(
        'default' => FALSE,
      ),

      'header' => array(
        'default' => array(),
      ),
      'footer' => array(
        'default' => array(),
      ),
      'empty' => array(
        'default' => array(),
      ),

      // We want these to export last.
      // These are the 5 handler types.
      'relationships' => array(
        'default' => array(),
      ),
      'fields' => array(
        'default' => array(),
      ),
      'sorts' => array(
        'default' => array(),
      ),
      'arguments' => array(
        'default' => array(),
      ),
      'filter_groups' => array(
        'contains' => array(
          'operator' => array('default' => 'AND'),
          'groups' => array('default' => array(1 => 'AND')),
        ),
      ),
      'filters' => array(
        'default' => array(),
      ),
    );

633
    if (!$this->usesPager()) {
merlinofchaos's avatar
merlinofchaos committed
634 635 636 637 638 639 640
      $options['defaults']['default']['use_pager'] = FALSE;
      $options['defaults']['default']['items_per_page'] = FALSE;
      $options['defaults']['default']['offset'] = FALSE;
      $options['defaults']['default']['pager'] = FALSE;
      $options['pager']['contains']['type']['default'] = 'some';
    }

641
    if ($this->isDefaultDisplay()) {
merlinofchaos's avatar
merlinofchaos committed
642 643 644 645
      unset($options['defaults']);
    }

    foreach ($this->extender as $extender) {
646
      $extender->defineOptionsAlter($options);
merlinofchaos's avatar
merlinofchaos committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660
    }

    return $options;
  }

  /**
   * Check to see if the display has a 'path' field.
   *
   * This is a pure function and not just a setting on the definition
   * because some displays (such as a panel pane) may have a path based
   * upon configuration.
   *
   * By default, displays do not have a path.
   */
661
  public function hasPath() { return FALSE; }
merlinofchaos's avatar
merlinofchaos committed
662 663 664 665 666 667 668 669

  /**
   * Check to see if the display has some need to link to another display.
   *
   * For the most part, displays without a path will use a link display. However,
   * sometimes displays that have a path might also need to link to another display.
   * This is true for feeds.
   */
670
  public function usesLinkDisplay() { return !$this->hasPath(); }
merlinofchaos's avatar
merlinofchaos committed
671 672 673 674 675 676 677 678 679

  /**
   * Check to see if the display can put the exposed formin a block.
   *
   * By default, displays that do not have a path cannot disconnect
   * the exposed form and put it in a block, because the form has no
   * place to go and Views really wants the forms to go to a specific
   * page.
   */
680
  public function usesExposedFormInBlock() { return $this->hasPath(); }
merlinofchaos's avatar
merlinofchaos committed
681 682 683 684 685

  /**
   * Check to see which display to use when creating links within
   * a view using this display.
   */
686 687
  public function getLinkDisplay() {
    $display_id = $this->getOption('link_display');
merlinofchaos's avatar
merlinofchaos committed
688
    // If unknown, pick the first one.
689 690 691
    if (empty($display_id) || empty($this->view->displayHandlers[$display_id])) {
      foreach ($this->view->displayHandlers as $display_id => $display) {
        if (!empty($display) && $display->hasPath()) {
merlinofchaos's avatar
merlinofchaos committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
          return $display_id;
        }
      }
    }
    else {
      return $display_id;
    }
    // fall-through returns NULL
  }

  /**
   * Return the base path to use for this display.
   *
   * This can be overridden for displays that do strange things
   * with the path.
   */
708 709 710
  public function getPath() {
    if ($this->hasPath()) {
      return $this->getOption('path');
merlinofchaos's avatar
merlinofchaos committed
711 712
    }

713
    $display_id = $this->getLinkDisplay();
714 715
    if ($display_id && !empty($this->view->displayHandlers[$display_id]) && is_object($this->view->displayHandlers[$display_id])) {
      return $this->view->displayHandlers[$display_id]->getPath();
merlinofchaos's avatar
merlinofchaos committed
716 717 718
    }
  }

719
  public function getUrl() {
720
    return $this->view->getUrl();
merlinofchaos's avatar
merlinofchaos committed
721 722 723 724 725 726 727
  }

  /**
   * Check to see if the display needs a breadcrumb
   *
   * By default, displays do not need breadcrumbs
   */
728
  public function usesBreadcrumb() { return FALSE; }
merlinofchaos's avatar
merlinofchaos committed
729 730 731 732 733 734 735 736

  /**
   * Determine if a given option is set to use the default display or the
   * current display
   *
   * @return
   *   TRUE for the default display
   */
737 738
  public function isDefaulted($option) {
    return !$this->isDefaultDisplay() && !empty($this->default_display) && !empty($this->options['defaults'][$option]);
merlinofchaos's avatar
merlinofchaos committed
739 740 741 742 743 744
  }

  /**
   * Intelligently get an option either from this display or from the
   * default display, if directed to do so.
   */
745 746 747
  public function getOption($option) {
    if ($this->isDefaulted($option)) {
      return $this->default_display->getOption($option);
merlinofchaos's avatar
merlinofchaos committed
748 749 750 751 752 753 754 755 756
    }

    if (array_key_exists($option, $this->options)) {
      return $this->options[$option];
    }
  }

  /**
   * Determine if the display's style uses fields.
757 758
   *
   * @return bool
merlinofchaos's avatar
merlinofchaos committed
759
   */
760
  public function usesFields() {
761
    return $this->getPlugin('style')->usesFields();
merlinofchaos's avatar
merlinofchaos committed
762 763 764 765 766 767 768 769
  }

  /**
   * Get the instance of a plugin, for example style or row.
   *
   * @param string $type
   *   The type of the plugin.
   *
770
   * @return \Drupal\views\Plugin\views\PluginBase
771 772
   */
  public function getPlugin($type) {
773
    // Look up the plugin name to use for this instance.
774 775
    $options = $this->getOption($type);
    $name = $options['type'];
merlinofchaos's avatar
merlinofchaos committed
776

777 778
    // Query plugins allow specifying a specific query class per base table.
    if ($type == 'query') {
779
      $views_data = views_fetch_data($this->view->storage->get('base_table'));
780
      $name = isset($views_data['table']['base']['query_id']) ? $views_data['table']['base']['query_id'] : 'views_query';
781
    }
782

783 784
    // Plugin instances are stored on the display for re-use.
    if (!isset($this->plugins[$type][$name])) {
785
      $plugin = drupal_container()->get("plugin.manager.views.$type")->createInstance($name);
merlinofchaos's avatar
merlinofchaos committed
786

787 788
      // Initialize the plugin.
      $plugin->init($this->view, $this, $options['options']);
789

790
      $this->plugins[$type][$name] = $plugin;
merlinofchaos's avatar
merlinofchaos committed
791 792
    }

793
    return $this->plugins[$type][$name];
merlinofchaos's avatar
merlinofchaos committed
794 795 796 797 798
  }

  /**
   * Get the handler object for a single handler.
   */
799
  public function &getHandler($type, $id) {
merlinofchaos's avatar
merlinofchaos committed
800
    if (!isset($this->handlers[$type])) {
801
      $this->getHandlers($type);
merlinofchaos's avatar
merlinofchaos committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815
    }

    if (isset($this->handlers[$type][$id])) {
      return $this->handlers[$type][$id];
    }

    // So we can return a reference.
    $null = NULL;
    return $null;
  }

  /**
   * Get a full array of handlers for $type. This caches them.
   */
816
  public function getHandlers($type) {
merlinofchaos's avatar
merlinofchaos committed
817 818
    if (!isset($this->handlers[$type])) {
      $this->handlers[$type] = array();
819
      $types = ViewExecutable::viewsHandlerTypes();
merlinofchaos's avatar
merlinofchaos committed
820 821
      $plural = $types[$type]['plural'];

822
      foreach ($this->getOption($plural) as $id => $info) {
merlinofchaos's avatar
merlinofchaos committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
        // If this is during form submission and there are temporary options
        // which can only appear if the view is in the edit cache, use those
        // options instead. This is used for AJAX multi-step stuff.
        if (isset($_POST['form_id']) && isset($this->view->temporary_options[$type][$id])) {
          $info = $this->view->temporary_options[$type][$id];
        }

        if ($info['id'] != $id) {
          $info['id'] = $id;
        }

        // If aggregation is on, the group type might override the actual
        // handler that is in use. This piece of code checks that and,
        // if necessary, sets the override handler.
        $override = NULL;
838
        if ($this->useGroupBy() && !empty($info['group_type'])) {
merlinofchaos's avatar
merlinofchaos committed
839
          if (empty($this->view->query)) {
840
            $this->view->initQuery();
merlinofchaos's avatar
merlinofchaos committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
          }
          $aggregate = $this->view->query->get_aggregation_info();
          if (!empty($aggregate[$info['group_type']]['handler'][$type])) {
            $override = $aggregate[$info['group_type']]['handler'][$type];
          }
        }

        if (!empty($types[$type]['type'])) {
          $handler_type = $types[$type]['type'];
        }
        else {
          $handler_type = $type;
        }

        $handler = views_get_handler($info['table'], $info['field'], $handler_type, $override);
        if ($handler) {
          // Special override for area types so they know where they come from.
          if ($handler_type == 'area') {
            $handler->handler_type = $type;
          }

          $handler->init($this->view, $info);
          $this->handlers[$type][$id] = &$handler;
        }

        // Prevent reference problems.
        unset($handler);
      }
    }

    return $this->handlers[$type];
  }

  /**
   * Retrieve a list of fields for the current display with the
876 877 878 879 880
   * relationship associated if it exists.
   *
   * @param $groupable_only
   *  Return only an array of field labels from handler that return TRUE
   *  from use_string_group_by method.
merlinofchaos's avatar
merlinofchaos committed
881
   */
882
  public function getFieldLabels() {
883 884 885
    // Use func_get_arg so the function signature isn't amended
    // but we can still pass TRUE into the function to filter
    // by groupable handlers.
886 887
    $args = func_get_args();
    $groupable_only = isset($args[0]) ? $args[0] : FALSE;
888

merlinofchaos's avatar
merlinofchaos committed
889
    $options = array();
890
    foreach ($this->getHandlers('relationship') as $relationship => $handler) {
merlinofchaos's avatar
merlinofchaos committed
891 892 893 894
      if ($label = $handler->label()) {
        $relationships[$relationship] = $label;
      }
      else {
895
        $relationships[$relationship] = $handler->adminLabel();
merlinofchaos's avatar
merlinofchaos committed
896 897 898
      }
    }

899
    foreach ($this->getHandlers('field') as $id => $handler) {
900 901 902 903
      if ($groupable_only && !$handler->use_string_group_by()) {
        // Continue to next handler if it's not groupable.
        continue;
      }
merlinofchaos's avatar
merlinofchaos committed
904 905 906 907
      if ($label = $handler->label()) {
        $options[$id] = $label;
      }
      else {
908
        $options[$id] = $handler->adminLabel();
merlinofchaos's avatar
merlinofchaos committed
909 910 911 912 913 914 915 916 917 918 919 920
      }
      if (!empty($handler->options['relationship']) && !empty($relationships[$handler->options['relationship']])) {
        $options[$id] = '(' . $relationships[$handler->options['relationship']] . ') ' . $options[$id];
      }
    }
    return $options;
  }

  /**
   * Intelligently set an option either from this display or from the
   * default display, if directed to do so.
   */
921 922 923
  public function setOption($option, $value) {
    if ($this->isDefaulted($option)) {
      return $this->default_display->setOption($option, $value);
merlinofchaos's avatar
merlinofchaos committed
924 925 926 927 928
    }

    // Set this in two places: On the handler where we'll notice it
    // but also on the display object so it gets saved. This should
    // only be a temporary fix.
929
    $this->display['display_options'][$option] = $value;
merlinofchaos's avatar
merlinofchaos committed
930 931 932 933 934 935
    return $this->options[$option] = $value;
  }

  /**
   * Set an option and force it to be an override.
   */
936 937 938
  public function overrideOption($option, $value) {
    $this->setOverride($option, FALSE);
    $this->setOption($option, $value);
merlinofchaos's avatar
merlinofchaos committed
939 940 941 942 943 944
  }

  /**
   * Because forms may be split up into sections, this provides
   * an easy URL to exactly the right section. Don't override this.
   */
945
  public function optionLink($text, $section, $class = '', $title = '') {
merlinofchaos's avatar
merlinofchaos committed
946 947 948 949 950 951 952 953 954 955 956 957 958
    views_add_js('ajax');
    if (!empty($class)) {
      $text = '<span>' . $text . '</span>';
    }

    if (!trim($text)) {
      $text = t('Broken field');
    }

    if (empty($title)) {
      $title = $text;
    }

959
    return l($text, 'admin/structure/views/nojs/display/' . $this->view->storage->get('name') . '/' . $this->display['id'] . '/' . $section, array('attributes' => array('class' => 'views-ajax-link ' . $class, 'title' => $title, 'id' => drupal_html_id('views-' . $this->display['id'] . '-' . $section)), 'html' => TRUE));
merlinofchaos's avatar
merlinofchaos committed
960 961 962 963 964 965 966 967
  }

  /**
   * Returns to tokens for arguments.
   *
   * This function is similar to views_handler_field::get_render_tokens()
   * but without fields tokens.
   */
968
  public function getArgumentsTokens() {
merlinofchaos's avatar
merlinofchaos committed
969 970 971 972 973
    $tokens = array();
    if (!empty($this->view->build_info['substitutions'])) {
      $tokens = $this->view->build_info['substitutions'];
    }
    $count = 0;
974
    foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) {
merlinofchaos's avatar
merlinofchaos committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
      $token = '%' . ++$count;
      if (!isset($tokens[$token])) {
        $tokens[$token] = '';
      }

      // Use strip tags as there should never be HTML in the path.
      // However, we need to preserve special characters like " that
      // were removed by check_plain().
      $tokens['!' . $count] = isset($this->view->args[$count - 1]) ? strip_tags(decode_entities($this->view->args[$count - 1])) : '';
    }

    return $tokens;
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
994
  public function optionsSummary(&$categories, &$options) {
merlinofchaos's avatar
merlinofchaos committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    $categories = array(
      'title' => array(
        'title' => t('Title'),
        'column' => 'first',
      ),
      'format' => array(
        'title' => t('Format'),
        'column' => 'first',
      ),
      'filters' => array(
        'title' => t('Filters'),
        'column' => 'first',
      ),
      'fields' => array(
        'title' => t('Fields'),
        'column' => 'first',
      ),
      'pager' => array(
        'title' => t('Pager'),
        'column' => 'second',
      ),
      'exposed' => array(
        'title' => t('Exposed form'),
        'column' => 'third',
        'build' => array(
          '#weight' => 1,
        ),
      ),
      'access' => array(
        'title' => '',
        'column' => 'second',
        'build' => array(
          '#weight' => -5,
        ),
      ),
      'other' => array(
        'title' => t('Other'),
        'column' => 'third',
        'build' => array(
          '#weight' => 2,
        ),
      ),
    );

1039
    if ($this->display['id'] != 'default') {
merlinofchaos's avatar
merlinofchaos committed
1040 1041 1042
      $options['display_id'] = array(
        'category' => 'other',
        'title' => t('Machine Name'),
1043
        'value' => !empty($this->display['new_id']) ? check_plain($this->display['new_id']) : check_plain($this->display['id']),
merlinofchaos's avatar
merlinofchaos committed
1044 1045 1046 1047
        'desc' => t('Change the machine name of this display.'),
      );
    }

1048
    $display_comment = check_plain(drupal_substr($this->getOption('display_comment'), 0, 10));
merlinofchaos's avatar
merlinofchaos committed
1049 1050 1051 1052 1053 1054 1055
    $options['display_comment'] = array(
      'category' => 'other',
      'title' => t('Comment'),
      'value' => !empty($display_comment) ? $display_comment : t('No comment'),
      'desc' => t('Comment or document this display.'),
    );

1056
    $title = strip_tags($this->getOption('title'));
merlinofchaos's avatar
merlinofchaos committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
    if (!$title) {
      $title = t('None');
    }

    $options['title'] = array(
      'category' => 'title',
      'title' => t('Title'),
      'value' => $title,
      'desc' => t('Change the title that this display will use.'),
    );

1068
    $style_plugin_instance = $this->getPlugin('style');
1069 1070
    $style_summary = empty($style_plugin_instance->definition['title']) ? t('Missing style plugin') : $style_plugin_instance->summaryTitle();
    $style_title = empty($style_plugin_instance->definition['title']) ? t('Missing style plugin') : $style_plugin_instance->pluginTitle();
merlinofchaos's avatar
merlinofchaos committed
1071 1072 1073

    $style = '';

1074
    $options['style'] = array(
merlinofchaos's avatar
merlinofchaos committed
1075 1076 1077 1078 1079 1080 1081 1082
      'category' => 'format',
      'title' => t('Format'),
      'value' => $style_title,
      'setting' => $style_summary,
      'desc' => t('Change the way content is formatted.'),
    );

    // This adds a 'Settings' link to the style_options setting if the style has options.
1083
    if ($style_plugin_instance->usesOptions()) {
1084
      $options['style']['links']['style_options'] = t('Change settings for this format');
merlinofchaos's avatar
merlinofchaos committed
1085 1086
    }

1087
    if ($style_plugin_instance->usesRowPlugin()) {
1088 1089 1090
      $row_plugin_instance = $this->getPlugin('row');
      $row_summary = empty($row_plugin_instance->definition['title']) ? t('Missing style plugin') : $row_plugin_instance->summaryTitle();
      $row_title = empty($row_plugin_instance->definition['title']) ? t('Missing style plugin') : $row_plugin_instance->pluginTitle();
merlinofchaos's avatar
merlinofchaos committed
1091

1092
      $options['row'] = array(
merlinofchaos's avatar
merlinofchaos committed
1093 1094 1095 1096 1097 1098 1099
        'category' => 'format',
        'title' => t('Show'),
        'value' => $row_title,
        'setting' => $row_summary,
        'desc' => t('Change the way each row in the view is styled.'),
      );
      // This adds a 'Settings' link to the row_options setting if the row style has options.
1100
      if ($row_plugin_instance->usesOptions()) {
1101
        $options['row']['links']['row_options'] = t('Change settings for this style');
merlinofchaos's avatar
merlinofchaos committed
1102 1103
      }
    }
1104
    if ($this->usesAJAX()) {
merlinofchaos's avatar
merlinofchaos committed
1105 1106 1107
      $options['use_ajax'] = array(
        'category' => 'other',
        'title' => t('Use AJAX'),
1108
        'value' => $this->getOption('use_ajax') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
1109 1110 1111
        'desc' => t('Change whether or not this display will use AJAX.'),
      );
    }
1112
    if ($this->usesAttachments()) {
merlinofchaos's avatar
merlinofchaos committed
1113 1114 1115
      $options['hide_attachment_summary'] = array(
        'category' => 'other',
        'title' => t('Hide attachments in summary'),
1116
        'value' => $this->getOption('hide_attachment_summary') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
1117 1118 1119
        'desc' => t('Change whether or not to display attachments when displaying a contextual filter summary.'),
      );
    }
1120 1121 1122 1123
    if (!isset($this->definition['contextual links locations']) || !empty($this->definition['contextual links locations'])) {
      $options['hide_admin_links'] = array(
        'category' => 'other',
        'title' => t('Hide contextual links'),
1124
        'value' => $this->getOption('hide_admin_links') ? t('Yes') : t('No'),
1125 1126 1127
        'desc' => t('Change whether or not to display contextual links for this view.'),
      );
    }
merlinofchaos's avatar
merlinofchaos committed
1128

1129
    $pager_plugin = $this->getPlugin('pager');
merlinofchaos's avatar
merlinofchaos committed
1130 1131 1132 1133 1134
    if (!$pager_plugin) {
      // default to the no pager plugin.
      $pager_plugin = views_get_plugin('pager', 'none');
    }

1135
    $pager_str = $pager_plugin->summaryTitle();
merlinofchaos's avatar
merlinofchaos committed
1136 1137 1138 1139

    $options['pager'] = array(
      'category' => 'pager',
      'title' => t('Use pager'),
1140
      'value' => $pager_plugin->pluginTitle(),
merlinofchaos's avatar
merlinofchaos committed
1141 1142 1143 1144 1145
      'setting' => $pager_str,
      'desc' => t("Change this display's pager setting."),
    );

    // If pagers aren't allowed, change the text of the item:
1146
    if (!$this->usesPager()) {
merlinofchaos's avatar
merlinofchaos committed
1147 1148 1149
      $options['pager']['title'] = t('Items to display');
    }

1150
    if ($pager_plugin->usesOptions()) {
merlinofchaos's avatar
merlinofchaos committed
1151 1152 1153
      $options['pager']['links']['pager_options'] = t('Change settings for this pager type.');
    }

1154
    if ($this->usesMore()) {
merlinofchaos's avatar
merlinofchaos committed
1155 1156 1157
      $options['use_more'] = array(
        'category' => 'pager',
        'title' => t('More link'),
1158
        'value' => $this->getOption('use_more') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
1159 1160 1161 1162
        'desc' => t('Specify whether this display will provide a "more" link.'),
      );
    }

1163
    $this->view->initQuery();
merlinofchaos's avatar
merlinofchaos committed
1164 1165 1166 1167
    if ($this->view->query->get_aggregation_info()) {
      $options['group_by'] = array(
        'category' => 'other',
        'title' => t('Use aggregation'),
1168
        'value' => $this->getOption('group_by') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
        'desc' => t('Allow grouping and aggregation (calculation) of fields.'),
      );
    }

    $options['query'] = array(
      'category' => 'other',
      'title' => t('Query settings'),
      'value' => t('Settings'),
      'desc' => t('Allow to set some advanced settings for the query plugin'),
    );

    $languages = array(
        '***CURRENT_LANGUAGE***' => t("Current user's language"),
        '***DEFAULT_LANGUAGE***' => t("Default site language"),
1183
        LANGUAGE_NOT_SPECIFIED => t('Language neutral'),
merlinofchaos's avatar
merlinofchaos committed
1184
    );
1185 1186
    if (module_exists('language')) {
      $languages = array_merge($languages, language_list());
merlinofchaos's avatar
merlinofchaos committed
1187
    }
1188
    $options['field_langcode'] = array(
merlinofchaos's avatar
merlinofchaos committed
1189 1190
      'category' => 'other',
      'title' => t('Field Language'),
1191
      'value' => $languages[$this->getOption('field_langcode')],
merlinofchaos's avatar
merlinofchaos committed
1192 1193 1194
      'desc' => t('All fields which support translations will be displayed in the selected language.'),
    );

1195
    $access_plugin = $this->getPlugin('access');
merlinofchaos's avatar
merlinofchaos committed
1196 1197 1198 1199 1200
    if (!$access_plugin) {
      // default to the no access control plugin.
      $access_plugin = views_get_plugin('access', 'none');
    }

1201
    $access_str = $access_plugin->summaryTitle();
merlinofchaos's avatar
merlinofchaos committed
1202 1203 1204 1205

    $options['access'] = array(
      'category' => 'access',
      'title' => t('Access'),
1206
      'value' => $access_plugin->pluginTitle(),
merlinofchaos's avatar
merlinofchaos committed
1207 1208 1209 1210
      'setting' => $access_str,
      'desc' => t('Specify access control type for this display.'),
    );

1211
    if ($access_plugin->usesOptions()) {
merlinofchaos's avatar
merlinofchaos committed
1212 1213 1214
      $options['access']['links']['access_options'] = t('Change settings for this access type.');
    }

1215
    $cache_plugin = $this->getPlugin('cache');
merlinofchaos's avatar
merlinofchaos committed
1216 1217 1218 1219 1220
    if (!$cache_plugin) {
      // default to the no cache control plugin.
      $cache_plugin = views_get_plugin('cache', 'none');
    }

1221
    $cache_str = $cache_plugin->summaryTitle();
merlinofchaos's avatar
merlinofchaos committed
1222 1223 1224 1225

    $options['cache'] = array(
      'category' => 'other',
      'title' => t('Caching'),
1226
      'value' => $cache_plugin->pluginTitle(),
merlinofchaos's avatar
merlinofchaos committed
1227 1228 1229 1230
      'setting' => $cache_str,
      'desc' => t('Specify caching type for this display.'),
    );

1231
    if ($cache_plugin->usesOptions()) {
merlinofchaos's avatar
merlinofchaos committed
1232 1233 1234
      $options['cache']['links']['cache_options'] = t('Change settings for this caching type.');
    }