diff --git a/lib/Drupal/views/DisplayArray.php b/lib/Drupal/views/DisplayArray.php new file mode 100644 index 0000000..521d3ab --- /dev/null +++ b/lib/Drupal/views/DisplayArray.php @@ -0,0 +1,159 @@ +view = $view; + + // @todo Figure out whether it might be helpful to initialize at least the + // default display automatically. + } + + /** + * Destructs a DisplayArray object. + */ + public function __destruct() { + foreach ($this->display as $display_id => $display) { + $display->destroy(); + unset($this->display[$display_id]); + } + } + + /** + * Initializes a single display and stores the result in self::display. + * + * @param string $display_id + * The name of the display to initialize. + */ + protected function initializeDisplay($display_id) { + // If the display was initialize before just return it. + if (isset($this->display[$display_id])) { + return $this->display[$display_id]; + } + $this->display[$display_id] = views_get_plugin('display', $this->view->storage->display[$display_id]['display_plugin']); + if (!empty($this->display[$display_id])) { + // Initialize the new display handler with data. + $this->display[$display_id]->init($this->view, $this->view->storage->display[$display_id]); + // If this is NOT the default display handler, let it know which is + // since it may well utilize some data from the default. + // This assumes that the 'default' handler is always first. It always + // is. Make sure of it. + if ($display_id != 'default') { + $this->display[$display_id]->default_display = $this->display['default']; + } + } + } + + /** + * Initialized all displays of the view. + */ + protected function initializeAllDisplays() { + foreach (array_keys($this->view->storage->display) as $display_id) { + $this->initializeDisplay($display_id); + } + } + + /** + * Implements \ArrayAccess::offsetExists(). + */ + public function offsetExists($offset) { + return isset($this->display[$offset]) || $this->view->storage->display[$offset]; + } + + /** + * Implements \ArrayAccess::offsetGet(). + */ + public function offsetGet($offset) { + if (isset($this->display[$offset])) { + return $this->display[$offset]; + } + else { + $this->initializeDisplay($offset); + return $this->display[$offset]; + } + } + + /** + * Implements \ArrayAccess::offsetSet(). + */ + public function offsetSet($offset, $value) { + $this->display[$offset] = $value; + } + + /** + * Implements \ArrayAccess::offsetUnset(). + */ + public function offsetUnset($offset) { + unset($this->display[$offset]); + } + + /** + * Implements \Iterator::current(). + */ + public function current() { + return current($this->display); + } + + /** + * Implements \Iterator::current(). + */ + public function next() { + // If we do an actual iteration over the elements, let's initialize all + // displays. + $this->initializeAllDisplays(); + return next($this->display); + } + + /** + * Implements \Iterator::key(). + */ + public function key() { + return key($this->display); + } + + /** + * Implements \Iterator::valid(). + */ + public function valid() { + $display_id = key($this->display); + return isset($this->display[$display_id]); + } + + /** + * Implements \Iterator::rewind(). + */ + public function rewind() { + reset($this->display); + } + +} diff --git a/lib/Drupal/views/ViewExecutable.php b/lib/Drupal/views/ViewExecutable.php index 36aaee8..7f47dd1 100644 --- a/lib/Drupal/views/ViewExecutable.php +++ b/lib/Drupal/views/ViewExecutable.php @@ -574,21 +574,8 @@ class ViewExecutable { return TRUE; } - // Instantiate all displays - foreach (array_keys($this->storage->display) as $id) { - $this->displayHandlers[$id] = views_get_plugin('display', $this->storage->display[$id]['display_plugin']); - if (!empty($this->displayHandlers[$id])) { - // Initialize the new display handler with data. - $this->displayHandlers[$id]->init($this, $this->storage->display[$id]); - // If this is NOT the default display handler, let it know which is - // since it may well utilize some data from the default. - // This assumes that the 'default' handler is always first. It always - // is. Make sure of it. - if ($id != 'default') { - $this->displayHandlers[$id]->default_display =& $this->displayHandlers['default']; - } - } - } + // Initialize the display cache array. + $this->displayHandlers = new DisplayArray($this); $this->current_display = 'default'; $this->display_handler = $this->displayHandlers['default']; @@ -1827,12 +1814,7 @@ class ViewExecutable { * collected. */ public function destroy() { - foreach (array_keys($this->displayHandlers) as $display_id) { - if (isset($this->displayHandlers[$display_id])) { - $this->displayHandlers[$display_id]->destroy(); - unset($this->displayHandlers[$display_id]); - } - } + unset($this->displayHandlers); foreach ($this::viewsHandlerTypes() as $type => $info) { if (isset($this->$type)) {