I have a custom module which creates a new field with a computed value, which I need to update.
How do I bulk update my nodes for a specific content type?
1 Answer
This is just a suggestion, its not well designed solution.
Considering that you already added your field to a Node type and you need update its value...
You can create a function that loads all nodes with specific type:
$articles = \Drupal::entityManager()->getStorage('node')->loadByProperties(array('type' => 'article'));;
And use a foreach to update your field content:
foreach ($articles as $nid => $article) {
//Change what you need
//Save your changes
}
Now, just run this once, on a new module install function or whatever you want.
-
Not a clean solution, but I guess this helps for now. Which method should I use for $article? Should it be $article->save()?– LogiMay 11, 2016 at 18:23
-
There is no "clean solution" for this, only if you update one by one your nodes. In Drupal there is no bulk operations to do this (by default, maybe through modules). With
$article
, which method you will use depends in what will change, (see docs for more info api.drupal.org/api/drupal/…), then use$article->save()
– VagnerMay 11, 2016 at 19:40