| 7 field.module | field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL) |
| 8 field.module | field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL) |
Returns a renderable array for a single field value.
Parameters
$entity_type: The type of $entity; e.g., 'node' or 'user'.
$entity: The entity containing the field to display. Must at least contain the id key and the field data to display.
$field_name: The name of the field to display.
$item: The field value to display, as found in $entity->field_name[$langcode][$delta].
$display: Can be either the name of a view mode, or an array of display settings. See field_view_field() for more information.
$langcode: (Optional) The language of the value in $item. If not provided, the current language will be assumed.
Return value
A renderable array for the field value.
Related topics
1 call to field_view_value()
File
- modules/
field/ field.module, line 814 - Attach custom data fields to Drupal entities.
Code
function field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL) {
$output = array();
if ($field = field_info_field($field_name)) {
// Determine the langcode that will be used by language fallback.
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
// Push the item as the single value for the field, and defer to
// field_view_field() to build the render array for the whole field.
$clone = clone $entity;
$clone->{$field_name}[$langcode] = array($item);
$elements = field_view_field($entity_type, $clone, $field_name, $display, $langcode);
// Extract the part of the render array we need.
$output = isset($elements[0]) ? $elements[0] : array();
if (isset($elements['#access'])) {
$output['#access'] = $elements['#access'];
}
}
return $output;
}
Login or register to post comments
Comments
This caught me out...
I struggled with this for a while, presuming (even having read this documentation) that
$itemwould be the delta of the field value you wanted to render... it's not. It's the actual field item itself as found in$entity->field_name[$langcode][$delta]. You should access this viafield_get_items()for proper localisation. For example:$node = node_load($nid);$image = field_get_items('node', $node, 'field_image');
$output = field_view_value('node', $node, 'field_image', $image[0]);
Where
$image[0]refers to the value you want.The reason behind that is
The reason behind that is field_get_items returns the array of items found, even if its just one.
The 0 refers the item's delta, or array position in the list.
So, when you run field_view_value, you should be passing a specific item[delta] to it.
Print custom fields on view, from a user profile
Can anyone tell me how do i print a user profile custom field value within a content view??
i'm not sure on how to get the entity of the current user, and how to apply some functions like field_get_item or field_view_value
thanks for your time!
This is a way to loop through multiple fields (of an image)
<?php$node = node_load($nid);
$image = field_get_items('node', $node, 'field_NAME');
foreach ($image as $key=>$value) {
$output = field_view_value('node', $node, 'field_NAME', $image[$key], array(
'type' => 'image',
'settings' => array(
'image_style' => 'thumbnail', //place your image style here
'image_link' => 'content',
),
));
print render($output);
}
?>
Hope it helps,
Guus
Correction
It looks like your
foreach()loop will overwrite the$outputarray on each iteration. If anyone is running into problems with this example, try replacing the first$outputwith$output[], or work in anarray_push(), if that suits you.guus, thanks for the example. it came in handy for my current project.
Not finding sub path
So when I try to use this approach I have my images in a sub directory. The image is coming up broken and if I look at the rendered html it goes down through styles/thumbnail/public/. After public there should be my custom directory for the image but it's not there. Am I missing something? Thanks.
Thanks
Thanks for the help, I am now using a wrapper method like this for a better api in some preprocess functions for a theme. You can easily add a loop if you need multiple values.
<?php
function mymodule_render_field ( $entity_type_id , $entity_object , $field_id , $display_opts = null ) {
if ( $display_opts == null ) $display_opts = array();
$field = field_get_items ( $entity_type_id , $entity_object , $field_id );
$output = field_view_value ( $entity_type_id , $entity_object , $field_id , $field[ 0 ], $display_opts );
return drupal_render ( $output );
}
//example for imagefield with custom imagestyle
function mymodule_render_imagefield_with_style ( $entity_type_id , $entity_object , $field_id , $image_style_id ) {
$opts = array (
'type' => 'image' ,
'settings' => array (
'image_style' => $image_style_id ,
'image_link' => 'content' ,
) ,
);
return mymodule_render_field ( $entity_type_id , $entity_object , $field_id, $opts );
}
?>
User array field not printing
Hi
I added an array field with three possible values to the user. It is called field_user_school_category.
I want to extract the field value for the current user and use it in an page in an if statement (in php text format). I just can't get the extract code to work. I can't even get the value to print - see code below. What am I doing wrong?!
The field is currently set up to accept multiple values. (I know I need to probably loop through to extract any other values later on). I have also tried using $school['und'][0]['safe_value'] instead of $school[0] amongst a myriad other things.
Any ideas or help would be very gratefully received.
Thanks Sheila
<?php
global $user;
$account = user_load($user>uid);
$school = field_get_items('user', $account, 'field_user_school_category');
$schoolcat = field_view_value('user', $account, 'field_user_school_category', $school[0]);
print $schoolcat;
?>
Sheila. I believe $schoolcat
Sheila. I believe $schoolcat is an array. Have you tried $schoolcat['#markup']?
Hi Andrew - I did try that
Hi Andrew - I did try that thanks - no joy :(. You were right about it being an array. I am still trying to resolve it - I got a bit further with a bit of help and this code but can't get the final school_cat to print.
---php
global $user;
$account = user_load($user->uid);
$school_cats = field_get_items('user', $account, 'field_user_school');
foreach ($school_cats as $school_cat) {
print $school_cat['safe_value'];
}
---php end
Any ideas?! Thanks Sheila
Yeah
---php
global $user;
$account = user_load($user->uid);
$school_cats = field_get_items('user', $account, 'field_user_school');
$schoolcats = field_view_value('user', $account, 'field_user_school', $school_cats[0]);
foreach ($schoolcats as $schoolcats) {
print $schoolcats;
}
---php end
I think that should work, I did something like this on my site. Although I think $account should be $user, wasn't sure if that was something you just did differently or not.