3

I have a content type and it has a select list field. There are two options Yes or No and this field is not a required field.

The website has about 100,000 nodes and I want to bulk update all nodes in order to select the "Yes" for each node. Is there any way to do this?

7
  • Is this a one time thing? Have you got access to the database?
    – Kartagis
    Commented Feb 23, 2016 at 20:28
  • Yes, it will be one time. I have access to the database but I don't want to change it via MySQL etc. because it can cause some problems (drupal.stackexchange.com/questions/189450/…). Thanks.
    – herci
    Commented Feb 23, 2016 at 20:29
  • Erm, you already have an answer there?
    – Kartagis
    Commented Feb 23, 2016 at 20:32
  • @Kartagis, I think the structure is different and I can't change the values as mentioned in the answer of other question, can I?
    – herci
    Commented Feb 23, 2016 at 20:34
  • 1
    Sure, I'm waiting for your answer, thanks and are you familiar with the BATCH API? If you're it would be perfect for me to get your help on that question.
    – herci
    Commented Feb 24, 2016 at 16:39

5 Answers 5

2

You have multiple options:

  • You can use code that iterates over all nodes and changes the value of the field (you will need some module to execute the code, for example devel).

  • Other option is to use the Views Bulk Operations module, build a view which shows a list of nodes of that type, and add a views bulk operations field which has the option to edit a node, and this will allow you to select all the nodes in a view and modify a field with the same value.

2

Question refinement

This answer ALSO takes into account the answer to my extra comment like "What should the updated value become for a node that currently has value No? Should it become Yes or remain No?". And to which you answered like "Good question :) For this case, every node should have the Yes value. But for other cases we should check the current value first and then change if needed, you're right".

Step 1: Create a Rules Component

Have a look at this example of a Rules Component (in Rules export format):

{ "rules_set_an_initial_value_for_a_text_field" : {
    "LABEL" : "Set an initial value for a text field",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "node" : { "label" : "Node", "type" : "node" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } },
      { "NOT data_is" : { "data" : [ "node:field-optionlist" ], "value" : "A" } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-optionlist" ], "value" : "B" } }
    ]
  }
}

Some more details to further explain the above above example:

  • This Rules Component will be used in "Step 2" below.
  • In my case I have a selection list field with machine name field_optionlist.
  • This field actually has 3 possible values, like so:

    • A (=Advanced).
    • B (=Basic).
    • C (=Custom).
  • As an example, I decided to use a Rules Condition "The selected value must NOT be Advanced" (assume that's the value I do not want to reset).
  • For any other value of that field I want the Rules Action to change the value of that field to Basic.

Note that I also added the Rules Condition Entity has field to make that field available for further processing in this Rules Component. That's also what is mentioned in the Rules UI when you're adding a "data comparison" condition: somewhere it says "The data selector helps you drill down into the data available to Rules. To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type')". For a video tutorial that illustrates the importance of this Entity has field condition, refer to Data types and data selection, especially what is shown between about 13:30 and 17:30 in that video.

Note that I've not added a Rules condition here about selected content types, that's possibly an extra condition you might want to add also. But if the selection list field is only used for the content types you're interested in, then the Entity has field condition should be sufficient already.

If you want, just grab (copy) the exported rule above, change all occurences of the machine name of that field to match the machine name of your field (and adapt the possible values like 'A' and 'B' in my example). After doing so you can simply import this Rules Component in your own environment.

After you imported this Rules Component, navigate to admin/config/workflow/rules/components in your own site. Then use the hyperlink "execute" to qa-test it in your own site. After you use that link, use the button that says Switch to direct input mode. Then you'll be able to enter some Node identifier and hit the Execute button. Depending on the node id you selected (i.e. if it has that field, and if the Rules Condition is satisfied), your node will be updated accordingly.

After you're confident your Rules Component works perfect for all variations of your nodes, move on to step 2 below.

Step 2: Let VBO to the bulk of the work

This step is pretty easy ... Just create an appropriate view of all your about 100.000 nodes to be processed, and transform it into a Views Bulk Operations view. The clue to what you want VBO to do as an operation is ... you guessed it: Execute the Rules Component from Step 1.

If you're not familiar with how to do so, have a look at the video about Using Rules components with VBO". If you want even more, then also watch the subsequent "Using Rules components with more than one parameter" video.

Of course there are multiple variations of the above approach, like processing by component type, narrowing down your Views output to only "a" specific value, etc. But that should be straight forward, and up to your own imagination.

Attention: to process 100.000 nodes at once is quite a challenge (memory requirements, etc). Therefor you might want to also watch the video about "More details about VBO settings" ... Before you are tempted to use VBO for processing them all at once ...

Video tutorials

If you're not familiar (enough/yet) with Rules, checkout the 32 (!!!) great, and free, video tutorials Learn the Rules framework.

Happy VBO-ing while Rules-ing!

0
1

In such scenario, one could be required to preserve node timestamps or avoid whole node_save() process, in this case you would do:

$key = 'value'; // Key that holds the field value.
$value = '1'; // Assuming '1' is the value for the option "Yes".
$bundle = 'article'; // Name of the node type.

// Get $bundle node ids.
$query = new EntityFieldQuery();
$result = $query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', $bundle)
  ->execute();

if (isset($result['node'])) {
  // Load matching nodes.
  $nids = array_keys($result['node']);
  $nodes = entity_load('node', $nids);

  foreach ($nodes as $nid => $node) {
    // Update the field(s). Assign whole array since some nodes may 
    // come with an empty field.
    $node->field_foobar[LANGUAGE_NONE] = array(array($key => $value));

    // Give other modules the opportunity to act before saving fields.
    field_attach_presave('node', $node);

    // Save field data (modules can also hook here).
    field_attach_update('node', $node);
  }

  // Reset cache for the updated nodes.
  entity_get_controller('node')->resetCache($nids);
}
1

You need something along the lines of

<?php
$nodes = node_load_multiple(array(), array('type' => 'page'));
foreach($nodes as $node) {
  // Only modify nodes that have something in the body field
    $node->field_select['und'][0]['value'] = 'yes';
    node_save($node);
}

Modify as needed. Don't forget to clear the caches afterwards.

Another option is installing the Administrative Views and Views Bulk Operations modules, and pointing to Change Value, as seen in the below screenshot vbo and admin_views combined

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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