1

I simply want to get the value of a field and have been told I should use field_view_value.

To this affect I have created:

$field_text_items = field_get_items('node', $node_context->data, 'field_text');

$field_text_items_value = field_view_value('node', $node_context->data, 'field_text', $field_text_items[0]['value']);

But running dpm($field_text_items_value); just returns an array with:

#markup (String, 1 characters ) h
#access (Boolean) TRUE

I'm assuming, because my field's value begins with a 'h', the #markup is only returning the first character for some reason.

Running dpm($field_fcc_text_items[0]['value']); returns the correct result so I know thing field is fine.

I've looked at the documentation linked above, but to be honest a lot of it doesnt make sense.

Would anyone be able to point me in the right direction here?

Thanks

As a side question, why is field_view_value() preferable to just using something like $field_text_items[0]['value'] anyway?

1 Answer 1

2

You may have the field_view_value parameters wrong. Instead of the entity_type-> delta try loading the field first using field_get_items. Use the return value as the second parameter in field_get_items.

$node = node_load($nid);
$field = field_get_items('node', $node, 'field_name');
$output = field_view_value('node', $node, 'field_name', $field[$delta]);

I took that answer from the blog: https://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.