You are here

public function view::clone_view in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 includes/view.inc \view::clone_view()
  2. 6.2 includes/view.inc \view::clone_view()

Safely clone a view.

Because views are complicated objects within objects, and PHP loves to do references to everything, if a View is not properly and safely cloned it will still have references to the original view, and can actually cause the original view to point to objects in the cloned view. This gets ugly fast.

This will completely wipe a view clean so it can be considered fresh.

Return value

view The cloned view.

File

includes/view.inc, line 2017
views_objects Objects that represent a View or part of a view

Class

view
An object to contain all of the data to generate a view.

Code

public function clone_view() {
  $clone = clone $this;
  $keys = array(
    'current_display',
    'display_handler',
    'build_info',
    'built',
    'executed',
    'attachment_before',
    'attachment_after',
    'field',
    'argument',
    'filter',
    'sort',
    'relationship',
    'header',
    'footer',
    'empty',
    'query',
    'inited',
    'style_plugin',
    'plugin_name',
    'exposed_data',
    'exposed_input',
    'exposed_widgets',
    'many_to_one_aliases',
    'many_to_one_tables',
    'feed_icon',
  );
  foreach ($keys as $key) {
    if (isset($clone->{$key})) {
      unset($clone->{$key});
    }
  }
  $clone->built = $clone->executed = FALSE;
  $clone->build_info = array();
  $clone->attachment_before = '';
  $clone->attachment_after = '';
  $clone->result = array();

  // Shallow cloning means that all the display objects *were not cloned*. We
  // must clone them ourselves.
  $displays = array();
  foreach ($clone->display as $id => $display) {
    $displays[$id] = clone $display;
    if (isset($displays[$id]->handler)) {
      unset($displays[$id]->handler);
    }
  }
  $clone->display = $displays;
  return $clone;
}