You are here

5 February 2015

The idea would be that you don't take any values directly from node_load result. You can either use entity wrapper(is not a part of this tutorial) or field get and view functions.

Source code viewer
  1. // Those are wrong ways to do this.
  2. $body = $node->body['und'][0]['value'];
  3. $body = $node->body['en'][0]['value'];
  4. $body = $node->body[LANGUAGE_NONE][0]['value'];
  5. $body = $node->body[LANGUAGE_NONE][0]['safe_value'];
  6.  
  7. // THE RIGHT WAY.
  8. // You start the regular way, with loading the entity you want to use.
  9. $node = node_load($nid);
  10.  
  11. // Then we have 2 functions that we use for different purposes. field_view_field for renderable array and field_get_items for raw values.
  12. $output = field_get_items('node', $node, 'field_name');
  13. $output = field_view_field('node', $node, 'field_name');
  14.  
  15. // Raw value of a field, using "value" as an example. If the field is a term reference the value key is "tid" for an example.
  16. $field = field_get_items('node', $node, 'field_name');
  17. $field_first_value = $field[0]['safe_value'];
  18. $field_first_value = $field[0]['tid'];
  19.  
  20. // Render field with default settings.
  21. echo render(field_view_field('node', $node, 'field_name'));
  22.  
  23. // Render field without label.
  24. echo render(field_view_field('node', $node, 'field_name', array('label' => 'hidden')));
  25.  
  26. // IN PROGRESS...
Programming Language: PHP