How to clone a node programmatically?

Public

How to clone a node programmatically?

Get raw version
php
  1. use Drupal\node\Entity\Node;
  2. $node = Node::load(84);
  3. $duplicateNode = $node->createDuplicate();
  4. $duplicateNode->set('title', 'Duplicate! Believe it!!');
  5. $duplicateNode->set('changed', strtotime("now"));
  6. $duplicateNode->save();