1.20.x 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

modules/views/includes/view.inc, line 1871
Provides the view object type and associated methods.

Class

view
An object to contain all of the data to generate a view, plus the member functions to build the view query, execute the query and render the output.

Code

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_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;
}