I added a field of type computed field to existing nodes. It's required to re-save the nodes to set/update computed field values. As I do not want to update the changed timestamp I decided to not use the views bulk update to resave content. The easiest way would be a drush script, that way I can re-save content whenever it is necessary.
My first problem was that I was not able to leave the changed time unchanged on update ($node->save()). But I found that it could be re-set on a second node save.
My problem is that on each node save a new revision gets created. The node type is revision-able and beside the core module content moderation is enabled. I would appreciate any help on how to prevent the creation of revisions.
This is my current drush script in custom_helper.drush.inc:
<?php
/**
* @file
* Provides drush command to update all articles.
*/
use Drupal\node\Entity\Node;
/**
* Implements hook_drush_command().
*/
function custom_helper_drush_command() {
$items = array();
$items['custom-resave-content'] = array(
'description' => dt('Resaves all article entities.'),
'options' => [
'types' => dt('Coma separated list of content type to be resaved.'),
'nids' => dt('Coma separated list if node ids.'),
'field' => dt('Machine name of computed field to be updated.')
],
'aliases' => array('custom-rc'),
);
return $items;
}
/**
* Moves paragraphs into one paragraph reference field.
*/
function drush_custom_helper_custom_resave_content() {
$nids = _convert_csv_to_array(drush_get_option('nids', []));
$types = _convert_csv_to_array(drush_get_option('types', []));
if (!$nids || $types) {
// Get an array of node IDs.
$query = \Drupal::entityQuery('node');
if ($types) {
$query->condition('type', $types, 'IN');
}
if ($nids) {
$query->condition('nid', $nids, 'IN');
}
$nids = $query->execute();
}
// Load all the nodes.
if ($nids) {
$field_name = drush_get_option('field', '');
$nodes = Node::loadMultiple($nids);
foreach ($nodes as $node) {
$entity_type = $node->getEntityType();
$original_changed = $node->getChangedTime();
if ($entity_type->isRevisionable()) {
$node->setNewRevision(FALSE);
}
// Force update of computed field.
if ($field_name) {
/** @var \Drupal\Core\Field\FieldItemListInterface $items */
$items = $node->get($field_name);
if (empty($items[0])) {
$items->appendItem();
}
$items->preSave();
}
// Save the updated computed field value.
$node->setChangedTime($original_changed);
if ($entity_type->isRevisionable()) {
$node->setNewRevision(FALSE);
}
$node->save();
// Reset changed time to original value.
$node->setChangedTime($original_changed);
if ($entity_type->isRevisionable()) {
$node->setNewRevision(FALSE);
}
$node->save();
$changed_after = $node->getChangedTime();
drush_print('node:' . $node->id() . ':' . $original_changed . ':' . $changed_after);
}
}
drush_log(dt('Resave finished.'), 'ok');
}