How to change a field value

Last updated on
22 September 2016

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Purpose: Change a field value in an existing node.

Version: Tested and worked with Drupal 7.26

Note: If you already know the basics of creating a module, skip to Step 4 and Step 5.

==============================

Is there a Contributed Module already available that will do what I need? Always ask yourself that question before writing a custom module. The Rules.module will easily change the field value; however, I needed a query to get the field's value. I had read a warning somewhere that said any PHP code used in my Rule would be stored in the database and it was better to write your own module.

Creating modules - a tutorial: Drupal 7.x: If you have never written a module before, start here. This step by step tutorial guides you to create a basic module. I will refer to this tutorial versus repeating previously documented information.

I am a beginner at writing modules and PHP code, please feel free to Edit this Book page to improve the explanation. I think it would be terrific if the more experienced Users would Add a Child Page with an alternative way to reach the same goal.

==============================

Example content-type: news
Example module's name: update_news

Step 1 Create the update_news folder and start the php file, update_news.module (Creating Modules tutorial -Getting Started chapter.)
..
..

Step 2 Create the update_news.info file. (Creating Modules tutorial -Telling Drupal about your module chapter.)
..
..

Step 3 -Add the basic Help function to your update_news.module. (Creating Modules tutorial -Writing comments and implementing your first hook chapter.)
..

/* ***************************
 * Admin help -describe what the module does
 **************************** */
function update_news_help($path, $arg) {
  switch ($path) {
    case "admin/help#update_news":
      return '<p>' . t("Updates fields in News posts") . '</p>';
      break;
  }
}

..
..

Step 4 -Add the menu function, if needed. I did not have a common action, such as node published, or a scheduled event to trigger the module. I used this menu to define a URL path to trigger the action.
..

/* ***************************
* hook_menu displays menu links
* When I go to this page, trigger the function to update_news_now
* and display the results message
 **************************** */
function update_news_menu() {
  $items = array();
  $items['path-to/update-news-results'] = array(
    // Page title
    'title' => 'News update completed',
    // What this link does
    'description' => 'Display the update News results',
    'type' => MENU_CALLBACK,
    // Name of function to execute
    'page callback' => 'update_news_now',
    // Who can access this menu item? Select a permission already in your lists.
    'access arguments' => array('use the administration toolbar '),
  );
  return $items;
}

..
..

Step 5 -Add the function to update the field value. See the query examples for Drupal 7 posted on www.mousewheel.net.

/* ******************************
 * Update the node field
 *
 *  Examples of syntax for different field types to set the values
 *  $node->title[$node->language][0]['value'] = 'the value goes here';
 *  $node->field_for_multi-values[$node->language][ ]['value'] = "apple, banana, orange';
 *
 *  $node->field_for_node_reference[$node->language][0]['nid'] = 3730;
 *  $node->field_for_user_reference[$node->language][0]['uid'] = 5;
 *  $node->field_for_taxonomy_term[$node->language][0]['tid'] = 114;
 *
 **************************** */
function update_news_now(){ 

 // A message to display on screen when the function is finished
 $finished_message = 'The Update News module has completed updating the nodes.';
  
  // Your own query to get the node id (nid) and the field value you need
 $query = db_select('node', 'N');
       $query->Fields('N', array('nid', 'title');
       ...rest of query);

  // Execute the query
  $result = $query->execute();

  // Loop through each row item in the results
  foreach ($result as $record) {

    // Results from my query are in these variables
    // $record->nid = node to update
    // $record->ref_node = referenced node value

    // Load the node to update
    $node = node_load($record->nid);
    
    // Set the field value 
    $node->field_news_node_reference[$node->language][0]['nid'] = $record->ref_node;
    
    // Optional lines to make this change a new revision
    $node->revision = 1;
    $node->log = 'This node was programmatically updated on ' . date('c');   
    
    // Save the updated node
    node_save($node);
  }  
  
  // Display the message on the page
  return $finished_message;
}

Help improve this page

Page status: No known problems

You can: