Skip to content

Instantly share code, notes, and snippets.

@kristianb21
Created August 1, 2014 20:12
Show Gist options
  • Save kristianb21/c28839b869dde18c27e1 to your computer and use it in GitHub Desktop.
Save kristianb21/c28839b869dde18c27e1 to your computer and use it in GitHub Desktop.
D7-PHP-field rendering
Output field as is in drupal ui:
$output = field_view_field('node', $node, 'field_name');
Just want a single value, and none of the field markup:
$node = node_load($nid);
$field = field_get_items('node', $node, 'field_name');
$output = field_view_value('node', $node, 'field_name', $field[$delta]);
Display the field based on drupals specific display settings:
$field = field_get_items('node', $node, 'field_name');
$instance = field_read_instance('node', 'field_name', 'content_type_machine_name');
$field = field_view_value('node', $node, 'field_name', $field[$delta], $instance['display']['default']);
Passing in some formatter options:
This would work the same for both field_view_value() and field_view_field().
<?php
$node = node_load($nid);
$image = field_get_items('node', $node, 'field_image');
$output = field_view_value('node', $node, 'field_image', $image[0], array(
'type' => 'image',
'settings' => array(
'image_style' => 'thumbnail',
'image_link' => 'content',
),
));
?>
Output Of The Field with access checks:
//field permissions from any module
<?php if (!empty($image) && field_access('view', field_info_field('field_image'), 'node', $node)) : ?>
<div class="field-output">
<?php print render($output); ?>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment