0

I needed to create a new field for each node in my website that would have as its value a concat of two existing fields.

I firstly inserted the new rows in the newly created field table, and then updated each of the rows with he corresponding information. But when I check on the browser in the edit page of several nodes some of them have the correct value inside the field while others have that field blank. The strange part is that there is no apparent difference in the database between the nodes with a value in the field and those that have it blank.

After investigating the issue I copied all the table to its correspondent revision table, leaving revision_id as 0. This didn't solve the problem.

I know I'm not supposed to directly modify the database like this but it has half-worked and I was wondering if there's a way to execute a method or a module so all nodes will have their field filled, or if I should erase all contents of the table and use db_update() or node_save() to do the job.

thanks in advance

  • I had forgotten to fill the 'bundle' field. After it had the correct value all nodes have now the correct value in the newly created field. – Jaime Marcos del Ser Apr 5 '17 at 19:01
1

The safest way would be adding field data and saving it with node_save. If you write it to the database, you have to also duplicate the record to the revision table of that field and keep track of vids. The API will just do this for you. You do not (should not) write field data via db_insert or other functions like it. Doing so may irreparably harm your database.

To apply the data retroactively, I would advise that you create a batch op via Batch API. Query up all nodes, create batch operations, and in your batch callback, process the node and save it with node_save.

There is a docs page on getting started with the Batch API.

Here is a blog post that demonstrates a basic batch job that is close to what you'd want to start out with. You'd want to populate the $operations array with node ids, and every time the batch callback is called, the node id is passed. Then you load the node with it, set the field, then save the node.

edit: tutorial author also provides a Drupal 8 version.

  • Thanks @Kevin, i note this for future use, I got it up and running updating the bundle node of the field table which I had forgotten to do so. Right now it's working but I don't know if the revision table will give me trouble in the future as you pointed out. – Jaime Marcos del Ser Apr 5 '17 at 19:00
0

I had forgotten to fill the 'bundle' field. After it had the correct value all nodes have now the correct value in the newly created field. – Jaime Marcos del Ser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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