I'm reading a field value and its format from the database. How would I render that value with the format?
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
$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
$display = array('type' => 'my_formatter'); $output = field_view_field('node', $node, 'field_foo', $display);
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.
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');
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?