Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / src / DisplayPluginCollection.php
1 <?php
2
3 namespace Drupal\views;
4
5 use Drupal\Component\Plugin\Exception\PluginException;
6 use Drupal\Component\Plugin\PluginManagerInterface;
7 use Drupal\Core\Plugin\DefaultLazyPluginCollection;
8
9 /**
10  * A class which wraps the displays of a view so you can lazy-initialize them.
11  */
12 class DisplayPluginCollection extends DefaultLazyPluginCollection {
13
14   /**
15    * Stores a reference to the view which has this displays attached.
16    *
17    * @var \Drupal\views\ViewExecutable
18    */
19   protected $view;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected $pluginKey = 'display_plugin';
25
26   /**
27    * Constructs a DisplayPluginCollection object.
28    *
29    * @param \Drupal\views\ViewExecutable $view
30    *   The view which has this displays attached.
31    * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
32    *   The manager to be used for instantiating plugins.
33    */
34   public function __construct(ViewExecutable $view, PluginManagerInterface $manager) {
35     parent::__construct($manager, $view->storage->get('display'));
36
37     $this->view = $view;
38     $this->initializePlugin('default');
39   }
40
41   /**
42    * Destructs a DisplayPluginCollection object.
43    */
44   public function __destruct() {
45     $this->clear();
46   }
47
48   /**
49    * {@inheritdoc}
50    *
51    * @return \Drupal\views\Plugin\views\display\DisplayPluginBase
52    */
53   public function &get($instance_id) {
54     return parent::get($instance_id);
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function clear() {
61     foreach (array_filter($this->pluginInstances) as $display) {
62       $display->destroy();
63     }
64
65     parent::clear();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function initializePlugin($display_id) {
72     // Retrieve and initialize the new display handler with data.
73     $display = &$this->view->storage->getDisplay($display_id);
74
75     try {
76       $this->configurations[$display_id] = $display;
77       parent::initializePlugin($display_id);
78     }
79     // Catch any plugin exceptions that are thrown. So we can fail nicely if a
80     // display plugin isn't found.
81     catch (PluginException $e) {
82       $message = $e->getMessage();
83       \Drupal::messenger()->addWarning(t('@message', ['@message' => $message]));
84     }
85
86     // If no plugin instance has been created, return NULL.
87     if (empty($this->pluginInstances[$display_id])) {
88       return NULL;
89     }
90
91     $this->pluginInstances[$display_id]->initDisplay($this->view, $display);
92     // If this is not the default display handler, let it know which is since
93     // it may well use some data from the default.
94     if ($display_id != 'default') {
95       $this->pluginInstances[$display_id]->default_display = $this->pluginInstances['default'];
96     }
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function remove($instance_id) {
103     $this->get($instance_id)->remove();
104
105     parent::remove($instance_id);
106   }
107
108 }