35

I added a new (text) field to a Drupal 7 content type that already has many nodes.

How can I fill the field with a default value for all these nodes?

37

You can use EntityFieldQuery to retrieve a list of nodes, then update the node's fields with node_save():

$lang = LANGUAGE_NONE; // Replace with ISO639-2 code if localizing
$node_type = 'page'; // Machine name of the content type

$query = new EntityFieldQuery;
$result = $query
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('type', $node_type)
  ->execute();

if (!empty($result['node'])) {
  $nodes = entity_load('node', array_keys($result['node']));

  foreach($nodes as $node) {
    // Replace field_foo with the machine name of the field to update.
    // - 0 refers to specific value within the field array, for when the field contains
    //    multiple values. If the field only has one value, it should be 0.
    $node->field_foo[$lang][0]['value'] = 'New Value';
    node_save($node);
  }
}

If this is a one-off operation, you can use the Devel module's Execute PHP function to run the above: otherwise, you can create a simple custom module.

  • 2
    Besides using devel or vbo, you can also use "drush scr myscript.php" to execute the above code in a fully bootstrapped environment. – fietserwin Mar 10 '15 at 17:04
  • How does this code change to accommodate performing the same operation to a term reference field? – Screenack Sep 8 '15 at 20:39
17

I would use Views Bulk Operations and use the "Execute Arbitrary PHP Script" to do essentially the items above, but you don't have to do all the extra code, just the little snippet that does what you want (like $object->field_foo['und'][0]['value'] = 'some_value')

9

If you want only to update field with some value, then the more performant alternative to accepted answer is this:

$lang = LANGUAGE_NONE; // Replace with ISO639-2 code if localizing
$node_type = 'page'; // Machine name of the content type

$query = new EntityFieldQuery;
$result = $query
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('type', $node_type)
  ->execute();

if (!empty($result['node'])) {
  $nodes = entity_load('node', array_keys($result['node']));

  foreach($nodes as $node) {
    // Replace field_foo with the machine name of the field to update.
    // - 0 refers to specific within the field array, for when the field contains
    //    multiple values. If the field only has one value, it should be 0.
    $node->field_foo[$lang][0]['value'] = 'New Value';
    field_attach_presave('node', $node);
    field_attach_update('node', $node);
  }
}

The difference is in using directly field_attach_presave and field_attach_update functions, which update correctly only node field and skip the rest of node saving process. This has an impact that no node presave/save hooks will be called, "changed" date will not be updated to current date etc. According your use case it may be better as using whole node_save() process.

4

Indeed VBO (Views Bulk Operations) is a good solution. Furthermore, with the latest versions, you will find an option "Modify entity values" that provides a very easy way to update nodes language in one operation.

2

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.

In your case its title. In the image it is tags.

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.

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.