15

I have an existing content type with a number of nodes. I have just added a new boolean field with a default value of "off."

However, until I re-save each node the default value is not set, so a view that should shown only those nodes where the new field is using the default value is currently empty.

How can I update the existing nodes, created before the field has been added, to set that field to its default value?

9

Sadly, there's no very simple way to do this (besides VBO/rules), but here's the code I use in update functions in my custom module install files when I need to prepopulate field values for a certain node type after adding a new field (in this case, 'page' nodes):

<?php
// Get all nodes of 'page' type, populate the field with a value of '1'.
$pages = db_query("SELECT nid, vid FROM {node} WHERE type = 'page'")->fetchAllAssoc('nid');
foreach ($pages as $nid => $values) {
  $fields = array(
    'entity_type' => 'node',
    'bundle' => 'page',
    'deleted' => 0,
    'entity_id' => $values->nid,
    'revision_id' => $values->vid,
    'language' => LANGUAGE_NONE,
    'delta' => 0,
    'field_page_new_field_value' => 1,
  );
  db_insert('field_data_field_page_new_field')
    ->fields($fields)
    ->execute();
  db_insert('field_revision_field_page_new_field')
    ->fields($fields)
    ->execute();
}
?>

Another manual method mentioned in this answer uses EntityFieldQuery and loads/saves each node. More Drupal-y, but way less performant... (requires a full node load and save operation for each node!).

  • That's very sad – AlxVallejo Jun 6 '14 at 22:29
  • Have you seen Field Defaults module? dgo.to/field_defaults – forest Dec 27 '15 at 2:06
  • It is a bug to run code like this without also flushing all the affected nodes from the entity load cache: entity_get_controller('node')->resetCache($nids); -- otherwise a subsequent node_load() may load stale data from cache, and any node_save() of that object would write that stale data back to the database. – phils Sep 13 '18 at 0:13
2

Best bet is directly in the MySQl. The tables would look like:

field_data_field_newbooleanfieldname
field_revision_field_newbooleanfieldname

If you look at them it's pretty straightforward - do you need me to get you started on what the query would look like or are you ok from here?

  • Thanks Mike, no it's not a problem I can handle the SQL really. I just feel like there should be a 're-save' option or something provided as a bulk operation, I think I might look at VBO and Rules integration for this in fact. – DanH Aug 3 '11 at 2:41
  • Understood. I'm in a similar position, I'm migrating about 30,000 articles from another CMS into Drupal. I didn't see the point to adding another module when the SQL was quick and dirty. Maybe you can also write a quick function that would call node_load() for all of your nodes, that may work too. – Mike Aug 3 '11 at 13:30
0

Just try the bellow logic. its much faster and its by pass all the hook implementations also. more details. you can writes a simple direct db_select query to get the all the nid's and loop it with this sample code.

$node = new stdClass();
$node->nid = $val;
$node->type = 'NODETYPE';
$node-field_whatever[LANGUAGE_NONE][0]['value'] = 'VALUE';
field_attach_presave('node', $node);
field_attach_update('node', $node);
0

using sql , considering that nodes with the field value not set are

SELECT nid,vid
 from node where type='procedure' and nid not in (select entity_id from field_data_field_pr_choix_du_document)

field_data_field_pr_choix_du_document being the table corresponding to my field and containing one entry par value set with entity id referinng the node

i did using SQL insert...select syntax

https://dev.mysql.com/doc/refman/5.5/en/insert-select.html

INSERT INTO `field_data_field_pr_choix_du_document` (`entity_type`, `bundle`, `deleted`, `entity_id`, `revision_id`, `language`, `delta`, `field_pr_choix_du_document_value`)
SELECT 'node','procedure',0,nid,vid,'und',0,'Importer un document'
from node where type='procedure' and nid not in (select entity_id from field_data_field_pr_choix_du_document)
0

Install and enable the Views Bulk Operations module and create a view with a page display.

Add => Bulk operations: Content (Content) field in view.

Refer

enter image description here

Select the Fields you want to set the default value.

Save the view, and go to the page it created. If you have more than one page of results, you can choose to select all items on the current page, all items on all pages, or you can manually check boxes corresponding to individual nodes. At least one checkbox must be checked to proceed.

Now you set the default value and save it.

0

I found an easy way to update field(s) in content type with this module : Field Defaults

See screen shot. You can update existing content with the default value(s) or keep existing values.

I’ve tested it on D7 and it works.

enter image description here

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.