Score:0

How can I update the node date for all nodes

cn flag

I have imported a number of nodes to a fresh installed Drupal 9. Later, the original content creation date (from the previous website) became important.

I have the information according to the new node ids. I don't know where to update?

I have tried to update the created and changed columns in node_field_data table, but in the content list I still see the import date... Where should i look? (of course I'm open to suggestions with modules, tricks in administration interface etc, I thought updating sql tables would be easier).

Score:0
in flag

First off, I would avoid as much as possible making direct DB updates for entities. Drupal 8+'s entity model is tightly bound to its abstraction layers, implemented as hooks. It is considered best practice to always modify entities via the entity API. This will allow Drupal to keep all things in sync.

Now, the short answer to your question is that clearing your site's cache will likely result in the new values being displayed.

A better answer would be to script some PHP in order to iterate through all nodes and update their content creation date via entity API. I often use Drush in order to perform these types of operations. Below is an example that should suit your needs, based on the following presumption:

  • You have loaded your node:date values into an array called $new_node_dates, where the keys are NIDs and the values are Unix timestamps representing the new creation dates. See here for an example of reading a CSV file into an array. If the dates are not in a Unix timestamp format, you can use strtotime() to convert them.

The Drush statement would then be:

drush eval '$node_storage = \Drupal::entityTypeManager()->getStorage("node"); foreach($new_node_dates as $nid => $creation_date) { $node = $node_storage->load($nid); $node->created = $creation_date; $node->save(); }'

Best of luck!

loker avatar
cn flag
Thank you for your nicely detailed and explained answer. Worked like a charm, and also I have a new approach...
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.