DisplayBag.php 2.82 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\views\DisplayBag.
 */

namespace Drupal\views;

10
use Drupal\Component\Plugin\Exception\PluginException;
11
use Drupal\Component\Plugin\PluginManagerInterface;
12
use Drupal\Core\Plugin\DefaultPluginBag;
13 14 15 16

/**
 * A class which wraps the displays of a view so you can lazy-initialize them.
 */
17
class DisplayBag extends DefaultPluginBag {
18 19 20 21 22 23 24 25

  /**
   * Stores a reference to the view which has this displays attached.
   *
   * @var \Drupal\views\ViewExecutable
   */
  protected $view;

26
  /**
27
   * {@inheritdoc}
28
   */
29
  protected $pluginKey = 'display_plugin';
30

31 32 33 34 35
  /**
   * Constructs a DisplayBag object.
   *
   * @param \Drupal\views\ViewExecutable
   *   The view which has this displays attached.
36 37
   * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
   *   The manager to be used for instantiating plugins.
38
   */
39
  public function __construct(ViewExecutable $view, PluginManagerInterface $manager) {
40
    parent::__construct($manager, $view->storage->get('display'));
41

42
    $this->view = $view;
43 44 45 46 47 48 49 50 51 52
    $this->initializePlugin('default');
  }

  /**
   * Destructs a DisplayBag object.
   */
  public function __destruct() {
    $this->clear();
  }

53 54 55 56 57 58 59 60 61
  /**
   * {@inheritdoc}
   *
   * @return \Drupal\views\Plugin\views\display\DisplayPluginBase
   */
  public function &get($instance_id) {
    return parent::get($instance_id);
  }

62 63 64 65
  /**
   * Overrides \Drupal\Component\Plugin\PluginBag::clear().
   */
  public function clear() {
66
    foreach (array_filter($this->pluginInstances) as $display) {
67 68 69 70 71 72 73
      $display->destroy();
    }

    parent::clear();
  }

  /**
74
   * {@inheritdoc}
75 76 77 78
   */
  protected function initializePlugin($display_id) {
    // Retrieve and initialize the new display handler with data.
    $display = &$this->view->storage->getDisplay($display_id);
79 80

    try {
81 82
      $this->configurations[$display_id] = $display;
      parent::initializePlugin($display_id);
83 84 85 86 87 88 89 90 91
    }
    // Catch any plugin exceptions that are thrown. So we can fail nicely if a
    // display plugin isn't found.
    catch (PluginException $e) {
      $message = $e->getMessage();
      drupal_set_message(t('!message', array('!message' => $message)), 'warning');
    }

    // If no plugin instance has been created, return NULL.
92
    if (empty($this->pluginInstances[$display_id])) {
93
      return NULL;
94 95 96 97 98 99 100 101 102 103
    }

    $this->pluginInstances[$display_id]->initDisplay($this->view, $display);
    // If this is not the default display handler, let it know which is since
    // it may well utilize some data from the default.
    if ($display_id != 'default') {
      $this->pluginInstances[$display_id]->default_display = $this->pluginInstances['default'];
    }
  }

104 105 106 107 108 109 110 111 112 113
  /**
   * Overrides \Drupal\Component\Plugin\PluginBag::remove().
   */
  public function remove($instance_id) {
    $this->get($instance_id)->remove();

    parent::remove($instance_id);
  }


114
}