Created
August 1, 2014 20:12
-
-
Save kristianb21/c28839b869dde18c27e1 to your computer and use it in GitHub Desktop.
D7-PHP-field rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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