Chris Muench
Chris Muench

Reputation: 18328

Drupal 6 --> Drupal 7 Migration field und's

In drupal 6 to get a node field's value you would do:

$node->field_ajax_override[0]['value']

Now it is:

$node->field_ajax_override['und'][0]['value']

Is this just going to be a pain to migrate or is there a better way?

Upvotes: 0

Views: 1659

Answers (1)

Ben Swinburne
Ben Swinburne

Reputation: 26477

You can use echo render($content['field_ajax_override']); instead in Drupal 7 (amongst other ways)

The article Rendering Drupal 7 Fields the right way discusses why accessing variables through ['und'] is bad. See the below excerpt.

Firstly, the ['und'] element is part of the field localisation in Drupal 7 (see this article from Gábor Hojtsy for more on that); directly accessing that value will cause issues in any kind of multi-lingual environment. Boo.

By accessing the field value directly you miss out on any theming that might come courtesy of the normal field markup.

The [0][safe_value] explicitly accesses the first value of the field - if you wanted every value from a multi-value field you'd need to do some sort of loop.

Some fields (such as node references) won't have a safe_value element, only a value - which can easily be printed without thought for sanitisation. This is dangerous, not because node reference fields contain dangerous data (they're just a nid), but because it's not a helpful habit to get into, especially for new developers. Other fields types 'value' may well be highly dangerous.

The article then goes on to advocate the use of field_view_field('node', $node, 'field_name'); but in most cases render($content['field']); will be sufficient, particularly if you already have access to the node you're rendering, for example in the node.tpl.php file or one of it's variations.

Upvotes: 1

Related Questions