3

I have a field name 'field_new' in a content type 'article'. I want to insert the value of node title on this field (all the nodes) programatically.

What I have done to achieve, I am using hook_node_update for this, but it is not updating the field values.

function modulename_node_update($node) {
  $new_value = $node->title;
  $node->field_new[LANGUAGE_NONE][0]['value'] = $new_value;
  field_attach_update('node', $node);
}

It would be great if there are another alternatives to do this.

1
  • Updated my question.
    – fanatic
    Commented Aug 20, 2016 at 10:02

2 Answers 2

5

hook_node_update - Respond to updates to a node. This is useful when you wants to update a field during node save. This will not helpful in this case because you wants to update all your exiting nodes.

1.Hope this code will helps you. You will get an idea but this is not the exact code for copy and paste.

<?php
    // Get all nodes of 'article' type, populate the field with a value of '1'.
    $articles = db_query("SELECT nid, vid FROM {node} WHERE type = 'article'")->fetchAllAssoc('nid');
    foreach ($articles as $nid => $values) {
      $fields = array(
        'entity_type' => 'node',
        'bundle' => 'article',
        'deleted' => 0,
        'entity_id' => $values->nid,
        'revision_id' => $values->vid,
        'language' => LANGUAGE_NONE,
        'delta' => 0,
        'field_data_field_new' => 'title need to fetch',
      );
      db_insert('field_data_field_new') //data table name
        ->fields($fields)
        ->execute();
      db_insert('field_revision_field_data_field_new') //revision table name
        ->fields($fields)
        ->execute();
    }
    ?>
  1. Another suggestion is Views Bulk Operations (VBO). You need investigate on it. Bulk field update?

  2. Updating a single field value for a particular content type:

 $article_nodes = node_load_multiple(array(), array('type' => 'article'));
    foreach ($article_nodes as $article_node) {
          $article_node->field_new[LANGUAGE_NONE][0]['value'] = 'title value';
          field_attach_update('node', $article_node);
        }
4

As per the "It would be great if there are another alternatives to do this" in your question: yes there are alternatives, using Rules and Views Bulk Operations and without writing custom code, as further detailed below ...

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_some_field" : {
    "LABEL" : "Set an initial value for some field",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "node" : { "label" : "Node", "type" : "node" } },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "article" : "article" } } } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_new" } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-free-format-text" ], "value" : "[node:title]" } }
    ]
  }
}

Some more details to further explain the above above example:

  • This Rules Component will be used in "Step 2" below.
  • It assumes a field with machine name field_new and content type is article.
  • It sets the value of that field_new field equal to the node title.

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.

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. After doing so you can simply import this Rules Component in your own environment (if you have Rules UI enabed).

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 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 lots (thousands?) of 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

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.