How to change value of a field in node view

We can alter field value to show it in node view without changing the actual value in database by implementing template_preprocess_field(&$variables, $hook)

Here is an example to check the node type then change the value of a field in that node before rendering the node

 

function THEMENAME_preprocess_field(&$variables, $hook) {
  if ($node = menu_get_object()) {
    if ($node->type == 'car') {
      if($variables['element']['#field_name'] == 'field_price') {
        $variables['items']['0']['#markup'] = $new_value; //new value;
      }
    }
  }
}

Leave a Comment.