Skip to content

Instantly share code, notes, and snippets.

@alexd73
Last active September 23, 2017 15:46
Show Gist options
  • Save alexd73/189c2d9b67283160b8a7d570a5c07572 to your computer and use it in GitHub Desktop.
Save alexd73/189c2d9b67283160b8a7d570a5c07572 to your computer and use it in GitHub Desktop.
drupal 8: Node and fields manipulation
// clone node programmatically
$node = \Drupal\node\Entity\Node::load($nid);
if ($node) {
$cloned_node = $node->createDuplicate();
$cloned_node->save();
}
$nids = \Drupal::entityQuery('node')->condition('type','menu_item')->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
foreach ($nodes as $node) {
$node->set("field_menu_category", [
['target_id' => $tid]
]);
$node->save();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment