Create a gist now

Instantly share code, notes, and snippets.

Drupal 8 Clone Nodes
name: Clone Nodes
description: 'Creates clone of node.'
type: module
core: 8.x
package: Custom
function clone_nodes_pages_entity_operation(\Drupal\Core\Entity\EntityInterface $entity)
$operations = [];
if ($entity instanceof \Drupal\node\Entity\Node) {
$operations['clone'] = [
'title' => t('Clone'),
'url' => \Drupal\Core\Url::fromRoute('clone_nodes',['id' => $entity->id()]),
'weight' => 150,
return $operations;
path: '/cnodes/node/{id}/clone'
_controller: '\Drupal\clone_nodes\Controller\CloneController::clone'
_permission: 'access content'
namespace Drupal\clone_nodes\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\node\Entity\Node;
class CloneController extends ControllerBase
public function clone($id)
$node = Node::load($id);
if ($node === null) {
drupal_set_message(t('Node with id @id does not exist.', ['@id' => $id]), 'error');
} else {
$nodeDuplicate = $node->createDuplicate();
foreach ($nodeDuplicate->field_paragraphs as $field) {
$field->entity = $field->entity->createDuplicate();
//edit title or something so you can find cloned
t("Node has been created. <a href='/node/@id/edit' target='_blank'>Edit now</a>", [
'@id' => $nodeDuplicate->id(),
'@title' => $nodeDuplicate->getTitle()]
), 'status');
return new RedirectResponse('/admin/content');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment