1

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 1

1

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.

2
  • Not a clean solution, but I guess this helps for now. Which method should I use for $article? Should it be $article->save()?
    – Logi
    May 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()
    – Vagner
    May 11, 2016 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.