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?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou 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.
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'
)
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.
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.
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
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.