9

I'm reading a field value and its format from the database. How would I render that value with the format?

34

While you could spend a lot of time dissecting how the Field API works, you really should be using the Field API directly to render field content instead of querying the database yourself. There's a lot of encapsulation and abstraction added to fields that will awaken the Old Gods if bypassed.

Using the Field API, if you want the whole formatted field, complete with label and all values, you want to use field_view_field():

$nid = 1;
$node = node_load($nid);
$output = field_view_field('node', $node, 'field_foo');

// $output is a render array, so it needs to be rendered first
print render($output);

If you just want to display the formatted value of one item in a field, you need to use field_view_value(), which is a little more involved:

// Must load field content for entity before using field_view_value()
$fields = field_get_items('node', $node, 'field_foo');

// $index corresponds to the value you want to render. First value = 0.
$index = 0;
$output = field_view_value('node', $node, 'field_foo', $fields[$index]);

print render($output);

If you want to use a formatter other than the default specified for the field instance, pass it using the $display parameter in either field_view_field() or field_view_value():

$display = array('type' => 'my_formatter');
$output = field_view_field('node', $node, 'field_foo', $display);
  • The $index (third parameter of the field_view_value()) is $delta. On a single value field this would be 0. So, You can use $output = field_view_value('node', $node, 'field_foo', $fields[0]); or $output = field_view_value('node', $node, 'field_foo', $fields[$delta]); – johirpro Apr 24 at 9:33
0

by checking api, i think you are look for this. ( I haven't tested the code anyway )

$the_field = field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL);

// render the field
drupal_render($the_field);

update: duplicate answer, may just delete me. other answer posted while I am writing this answer.

0

If you use module Custom Formatters don't forget to add prefix 'custom_formatters_' before your formatter's mashine name like this:

$display = array('type' => 'custom_formatters_my_formatter');
0

Looks like you can also edit the template file to adjust how this is output semantically:

1) Set up a custom date fomat and give a machine name like "short_date_only" or whatever. 2) Configure the display options for a content type to use that format 3) Override the field template to something like field--field_date_custom-content_type.tpl

protected by kiamlaluno Sep 3 '16 at 8:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.