class DisplayBag

A class which wraps the displays of a view so you can lazy-initialize them.

Hierarchy

  • class \Drupal\Component\Plugin\PluginBag implements \Drupal\Component\Plugin\Iterator, \Drupal\Component\Plugin\Countable

Expanded class hierarchy of DisplayBag

1 file declares its use of DisplayBag
ViewExecutableTest.php in drupal/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php
Contains \Drupal\views\Tests\ViewExecutableTest.

File

drupal/core/modules/views/lib/Drupal/views/DisplayBag.php, line 17
Contains \Drupal\views\DisplayBag.

Namespace

Drupal\views
View source
class DisplayBag extends PluginBag {

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

  /**
   * The manager used to instantiate the plugins.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $manager;

  /**
   * Constructs a DisplayBag object.
   *
   * @param \Drupal\views\ViewExecutable
   *   The view which has this displays attached.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
   *   The manager to be used for instantiating plugins.
   */
  public function __construct(ViewExecutable $view, PluginManagerInterface $manager) {
    $this->view = $view;
    $this->manager = $manager;
    $this
      ->initializePlugin('default');

    // Store all display IDs to access them easy and fast.
    $display = $this->view->storage
      ->get('display');
    $this->instanceIDs = drupal_map_assoc(array_keys($display));
  }

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

  /**
   * Overrides \Drupal\Component\Plugin\PluginBag::clear().
   */
  public function clear() {
    foreach (array_filter($this->pluginInstances) as $display_id => $display) {
      $display
        ->destroy();
    }
    parent::clear();
  }

  /**
   * Overrides \Drupal\Component\Plugin\PluginBag::initializePlugin().
   */
  protected function initializePlugin($display_id) {

    // If the display was initialized before, just return.
    if (isset($this->pluginInstances[$display_id])) {
      return;
    }

    // Retrieve and initialize the new display handler with data.
    $display =& $this->view->storage
      ->getDisplay($display_id);
    try {
      $this->pluginInstances[$display_id] = $this->manager
        ->createInstance($display['display_plugin']);
    } catch (PluginException $e) {
      $message = $e
        ->getMessage();
      drupal_set_message(t('!message', array(
        '!message' => $message,
      )), 'warning');
    }

    // If no plugin instance has been created, return NULL.
    if (empty($this->pluginInstances[$display_id])) {
      return NULL;
    }
    $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'];
    }
  }

  /**
   * Overrides \Drupal\Component\Plugin\PluginBag::remove().
   */
  public function remove($instance_id) {
    $this
      ->get($instance_id)
      ->remove();
    parent::remove($instance_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DisplayBag::$manager protected property The manager used to instantiate the plugins.
DisplayBag::$view protected property Stores a reference to the view which has this displays attached.
DisplayBag::clear public function Overrides \Drupal\Component\Plugin\PluginBag::clear(). Overrides PluginBag::clear
DisplayBag::initializePlugin protected function Overrides \Drupal\Component\Plugin\PluginBag::initializePlugin(). Overrides PluginBag::initializePlugin
DisplayBag::remove public function Overrides \Drupal\Component\Plugin\PluginBag::remove(). Overrides PluginBag::remove
DisplayBag::__construct public function Constructs a DisplayBag object.
DisplayBag::__destruct public function Destructs a DisplayBag object.
PluginBag::$instanceIDs protected property Stores the IDs of all potential plugin instances.
PluginBag::$pluginInstances protected property Stores all instantiated plugins.
PluginBag::addInstanceID public function Adds an instance ID to the array of available instance IDs.
PluginBag::count public function Implements \Countable::count().
PluginBag::current public function Implements \Iterator::current().
PluginBag::get public function Retrieves a plugin instance, initializing it if necessary.
PluginBag::getInstanceIDs public function Returns all instance IDs.
PluginBag::has public function Determines if a plugin instance exists.
PluginBag::key public function Implements \Iterator::key().
PluginBag::next public function Implements \Iterator::next().
PluginBag::rewind public function Implements \Iterator::rewind().
PluginBag::set public function Stores an initialized plugin.
PluginBag::setInstanceIDs public function Sets the instance IDs property.
PluginBag::valid public function Implements \Iterator::valid().