Replacement for clone_view() in Drupal 8

Posted on January 29th, 2016 in Drupal

In Drupal 8, view::clone_view() (Drupal 7) and its inital replacement ViewExecutable::cloneView() was removed in #1858054: Remove ViewExecutable::cloneView(). Occasionally, a module may have the need to copy a view to perform additional query modifications without affecting the original view (think attachments and aggregation).

Luckily, there are a couple of ways to perform a similar cloning.


    $cloned = $view->createDuplicate();

where $view is a ViewExecutable



use Drupal\views\Views;

$cloned = Views::executableFactory()->get($view->storage);

where $view is a ViewExecutable

Both produce new objects, verified with spl_object_hash():

SPL Hash of cloned and original objects

And both unset identifiers and built values, retaining the storage info and Drupal\views\ViewsData we can use:

Cloned view with clened properties


Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.