A class which wraps the displays of a view so you can lazy-initialize them.
Expanded class hierarchy of DisplayBag
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);
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DisplayBag:: |
public | function | Constructs a DisplayBag object. | |
DisplayBag:: |
public | function | Destructs a DisplayBag object. | |
DisplayBag:: |
public | function |
Overrides \Drupal\Component\Plugin\PluginBag::clear(). Overrides PluginBag:: |
|
DisplayBag:: |
protected | function |
Overrides \Drupal\Component\Plugin\PluginBag::initializePlugin(). Overrides PluginBag:: |
|
DisplayBag:: |
public | function |
Overrides \Drupal\Component\Plugin\PluginBag::remove(). Overrides PluginBag:: |
|
PluginBag:: |
public | function | Determines if a plugin instance exists. | |
PluginBag:: |
public | function | Retrieves a plugin instance, initializing it if necessary. | |
PluginBag:: |
public | function | Stores an initialized plugin. | |
PluginBag:: |
public | function | Adds an instance ID to the array of available instance IDs. | |
PluginBag:: |
public | function | Returns all instance IDs. | |
PluginBag:: |
public | function | Sets the instance IDs property. | |
PluginBag:: |
public | function | Implements \Iterator::current(). | |
PluginBag:: |
public | function | Implements \Iterator::next(). | |
PluginBag:: |
public | function | Implements \Iterator::key(). | |
PluginBag:: |
public | function | Implements \Iterator::valid(). | |
PluginBag:: |
public | function | Implements \Iterator::rewind(). | |
PluginBag:: |
public | function | Implements \Countable::count(). | |
DisplayBag:: |
protected | property | Stores a reference to the view which has this displays attached. | |
DisplayBag:: |
protected | property | The manager used to instantiate the plugins. | |
PluginBag:: |
protected | property | Stores all instantiated plugins. | |
PluginBag:: |
protected | property | Stores the IDs of all potential plugin instances. |