Printing entity field values with field_get_items & field_view_value functions

This code demonstrates how to print out field values from an associated entity within a node. The two functions used are:

  • field_get_items - to get the entities field values
  • field_view_value - to render out the values

For the purpose of demonstration lets say you have a 'Shop' and 'Chain' content type. Within the 'Chain' content type you have general values, eg. chain name & logo. Within the 'Shop' content type you have details specific to one individual shop, eg. telephone number & address.

When creating a 'Shop' content type you can choose what 'Chain' to link it to with an entity reference field. When rendering out your 'Shop' node you then want to include some of the fields that were set within your 'Chain' node.

Here's examples of the code you would place in your 'Shop' content template (node--shop.tpl.php), in order to pull in those entity field value arrays.

Loading the entire entity & outputting formatted fields as well as raw field values

The below code demonstrates loading all the fields from an associated entity and then rending the fields and values using a combination of field_view_field output (which adheres to your display settings) & specific values.

// load fields from associated entity 'Chain' content type
$node = node_load($node->field_chain['und'][0]['target_id']);

// chain logo
$chainLogo = field_view_field('node', $node, 'field_chain_logo', '');		

// chain website
$chainSiteUrl = field_view_field('node', $node, 'field_site_url', '');		

// specific values pulled from those fields
$chainLogoFilename = $node->field_chain_logo['und'][0]['filename'];
$chainSiteUrlRaw = $node->field_site_url['und'][0]['value'];

Getting a single value from the entity array

In the below code the $delta is set by the number in the $field_body_chain[0]. That would be bringing back the first item in the array, where $field_body_chain[2] would be bringing back the 3rd item.

$field_body_chain = field_get_items('node', $node->field_chain_franchise['und'][0]['entity'], 'body');
$render_body_chain = field_view_value('node', $node, 'body', $field_body_chain[0]);
print render ($render_body_chain);

Getting the entity field array and printing each item

This is useful if you are trying to print out an array of values, eg. defined via something like a list of checkboxes.

$accepted_payments = field_get_items('node', $node->field_chain_franchise['und'][0]['entity'], 'field_accepted_payments');
foreach ($accepted_payments as $key=>$value) {
    $output = field_view_value('node', $node, 'field_accepted_payments', $accepted_payments[$key], array());
    print render($output);
}

Removing html tags from the entity values

If you're rendering a list of taxonomy terms they appear as a list of links. In order to remove those links you can use the PHP strip_tags function.
PS. I'm sure there is a better way to do this so please let me know if you know what that is!

print strip_tags(render($output));
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +9 years.

Comments

Submitted by Jyo on

Hi,

I am using paragraphs module for a content type. I have created two paragraph module bundles, each contains 3-4 field types. (HTML editor, text fields and file browser).

I am planning to create a custom template for this particular content type. I used hook_preprocess_page() to to get the saved data and apply logic for tpl file.

But I found that the array I've received has 8 level nested and it's very hard to read the data. I tried the below code to read a particular field data.
$field_data = $vars['page']['content']['system_main']['nodes']['2']['field_product_footer'][0]['entity']['paragraphs_item'][3]['field_specification']

But I myself found that it's an awkward method and I believe it's not the proper way to read the data.

Can we get the value through field_view_value hook .

Please help me to get better way to access the data than this lengthy procedure . Also I have to iterate the array to get all the field values. Help me to get simpler solution to read these nested data.

Thanks

Jyo

Submitted by Justin on

The easiest way I can think of to get the field data for your paragraph bundles would be to install the devel module, and then add the below to your tpl template.

<?php
dpm($content);
?>

This will return the values with Krumo which will let you easily drill down and simply copy the right code for the fields you want. Unfortunately the code is still quite long (see example below), but at least it's a lot easier to retrieve this way.

$content['field_paragraph'][1]['entity']['paragraphs_item'][2]['field_text_field']['#items'][0]['value'];

Hopefully this helps?

Submitted by Joel Davis on

I struggled with this too. With perseverance, hacking, and a bit of luck, I was able to get things working. I'd welcome any reply about whether this is the "best" way or any performance counter arguments.

Examples:

Getting the values of a field with multiple values:

$styles = field_get_items('paragraphs_item', $variables['paragraphs_item'], 'field_layout_style');
if (is_array($styles)) {
foreach($styles as $delta => $style) {
$variables['content_attributes_array']['class'][] = drupal_html_class($style['value']);
}
unset($variables['content']['field_layout_style']);
}

Rendering field with only one value:

$heading_text = field_get_items('paragraphs_item', $variables['paragraphs_item'], 'field_heading');
$heading_text = field_view_value('paragraphs_item', $variables['paragraphs_item'], 'field_heading', $heading_text[0]);
$heading_text = render($heading_text);

Getting just the value of the field, not the rendered output:

$heading_level = field_get_items('paragraphs_item', $variables['paragraphs_item'], 'field_heading_level');
$heading_level = $heading_level[0]['value'];

Submitted by Helder Luis on

Hi, any idea how to print the raw data of a computed field (php code) in a template page. I was using this "<?php print render(field_view_field('node', $node, 'field_typeface_product_list', array('label' => 'hidden')));?>" but it prints the html on the page as text.

Submitted by April Rosequist on

I am trying to basically do what you describe except I have Events (event_calendar) and Event Sponsors (partners). I'm using entity reference on the Events node to choose the sponsor via field "field_sponsor". I am trying to display the sponsor logo on the event listing. However, what I'm getting is... nothing.

I tried:

// load fields from associated entity 'Sponsor' content type
$node = node_load($node->field_sponsor['und'][0]['target_id']);

// sponsor logo
$sponsorLogo = field_view_field('node', $node, 'field_image_partners', '');

// sponsor website
$sponsorSiteUrl = field_view_field('node', $node, 'field_sponsor_url', '');
// print render($prodSiteUrl);

// specific values pulled from those fields
$sponsorLogoFilename = $node->field_image_partners['und'][0]['filename'];
$sponsorSiteUrlRaw = $node->field_sponsor_url['und'][0]['value'];

Can you help?

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.