Wednesday, February 6, 2013

How to get Drupal 7 fields out in code

Demo:

<?php

if (arg(0) == 'node' && is_numeric(arg(1)))
 {
   $nid = arg(1);
  }

$node = node_load($nid);
$node_wrapper = entity_metadata_wrapper('node', $node);

$format =  $node_wrapper->field_format->value();


$result = field_view_field('node', $node, 'field_feedcategory', array('default'));

$category = $result['#object']->field_feedcategory['und'][0]['taxonomy_term']->name;

?>

Drupal 7 is more complex than Drupal 6 in its entity structure ( just combined user and node) and its language integration ..  It adds many complexity when we try to get the content type fields out when it comes to taxonomy or image/files etc
entity_metadata_wrapper turns out to be a good tool and also you can use function like field_view_field

Notice ->field_feedcategory['und'][0]['taxonomy_term']->name;  ['und'] means language。


No comments:

Post a Comment