How to simply mass update entities with an added or modified "Computed Field" ?

Thank you

CommentFileSizeAuthor
#7 drupal-computed_field-2988058-7.patch693 bytesattisan

Comments

steveoriol created an issue. See original summary.

kohashi’s picture

I'm running into the same issue and haven't found a good solution.

These all failed:

I've tried bulk saving from admin/content.

I've tried saving by executing PHP code to load a node and $node->save();

bgilhome’s picture

This worked for me - not sure if the appendItem() or preSave() calls are necessary.

$field_name = 'field_my_field_name';
$items = $node->get($field_name);
if (empty($items[0])) {
  $items->appendItem();
}
$items->preSave();
$node->save();

I put it in an update hook and looped over nodes returned from an entity query.

hansrossel’s picture

You can also do it on cron

function MYMODULE_cron() {
  $query = \Drupal::entityQuery('node')
		->condition('type', 'MYCONTENTTYPE')
		->execute();
  $nids = array_keys($query);

  foreach ($nids as $nid) {
    // \Drupal::logger('MYMODULE')->notice($nid);
    $node = \Drupal\node\Entity\Node::load($nid);
    $field_name = 'field_FIELDNAME';
    $items = $node->get($field_name);
    if (empty($items[0])) {
      $items->appendItem();
    }
    $items->preSave();
    $node->save();
    // \Drupal::entityManager()->getStorage('node')->resetCache(array($nid));
  }
}
mrogers’s picture

Leaving this for the less code literate (like me!), I was able to use Views Bulk Edit to bulk update computed fields for thousands of nodes in one go.

1. Create a new Page view listing the nodes you want to update.

2. Be sure to add the Views Bulk Operations field to the view with the "Modify Field Values" action.

3. Save and visit the view's page. Select the "Modify Field Values" action, check the "all results" checkbox, click "Apply", and then select the checkbox next to your computed field (you don't have to enter a value) and hit 'Apply' again.

Hope this helps someone!

webel’s picture

@bgilhome and @hansrossel

Thanks, this one helped me a lot.

@hansrossel wrote:

not sure if the appendItem() or preSave() calls are necessary.

Just $node->save(); definitely not enough (leading me to search and find this page).

attisan’s picture

Component: Documentation » Code
Status: Active » Needs review
StatusFileSize
new693 bytes

or we could populate default values when there are none - saving us the trouble of updating possible large amounts of entities

osopolar’s picture

@attisan: This may result in unexpected behavior on sorting view results on computed field.

osopolar’s picture

#3 and #4 is working well so far, but I do not want to update changed time nor crate a new revision. I was able to reset changed time by re-saving the node a second time. But that leads to two new revisions. Any help on that?

I wrote a drush script to do the update, see https://drupal.stackexchange.com/q/282416/10316

liquidcms’s picture

Not sure if this is a View Bulk Edit issue; but i have this on 2 sites to update computed fields:

VBE 2.4: works
VBE 2.5: the computed fields don't show on the list of fields (so doesn't work).

Edit: nope, i updated site 1 to 2.5.. and it still shows the computed field fields

carsonw’s picture

To update all users:

  $query = \Drupal::entityQuery('user')
		->execute();
  $uids = array_keys($query);

  foreach ($uids as $uid) {
    $user = \Drupal\user\Entity\User::load($uid);
    $field_name = 'field_my_field_name;
    $items = $user->get($field_name);
    if (empty($items[0])) {
      $items->appendItem();
    }
    $items->preSave();
    $user->save();
  }
webdrips’s picture

I just wanted to chime in here with a 2022 update: I was able to use views bulk edit, select all items for a given content type, and just run the "Save Content Item", and that updated all computed fields for the selected content type.

Of course you can still check the box for the computed field you want updated, and click Apply, and that will have the same affect.