DisplayPluginCollection.php 2.7 KB

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