function view::clone_view

Same name in other branches
  1. 7.x-3.x 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 1794

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 = version_compare(phpversion(), '5.0') < 0 ? $this : 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] = drupal_clone($display);
        if (isset($displays[$id]->handler)) {
            unset($displays[$id]->handler);
        }
    }
    $clone->display = $displays;
    return $clone;
}