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