In a module, I have defined a custom field. Using hook_field_formatter_info() and hook_field_formatter_view(), I can manage the display of every item (value) within that field. I want to allow the use of multiple values of this field.

How can I add extra HTML to this 'collection'? Programmatically, from within my module.

For example, let's say my module adds a 'fruit' field, that allows me to enter its name and its color. Now, I'd like to display this as such:

Name: Strawberry. Color: Red
Name: Orange. Color: Orange
Name: Lime. Color: Green
<some>Extra Html</some>

How to add that extra last line?

I can find references to hooks that should do it, but I simply don't understand how to tie all these pieces of information together. I suspect I have to work with:

hook_field_prepare_view https://api.drupal.org/api/drupal/modules%21field%21field.api.php/function/hook_field_prepare_view/7

I also found this resource suggesting these hooks: http://www.failover.co/blog/drupal-7-custom-field-formatter-hooks-overview

and that I should manipulate the render array.

And I tried just adding an extra element to the array, an extra delta:

$element[] = array(
    '#type' => 'markup',
    '#markup' => "Test HTML"

But is seems to get ignored by Drupal

  • Sorry if I'm misunderstanding, but why do you not just add it in your implementation of hook_field_formatter_view()? You have access to the full render array containing all the field deltas, so it would seem like the most logical place to do so
    – Clive
    Commented Jun 11, 2015 at 8:32
  • Thanks! I tried adding an extra delta, as if there was an extra field value, but it get's ignored by Drupal. (edited my question to reflect this comment)
    – Ideogram
    Commented Jun 11, 2015 at 8:38
  • That's strange - how about adding a string based array key? e.g. $element['footer']['#markup'] = 'foo';
    – Clive
    Commented Jun 11, 2015 at 8:41
  • Thanks, again,... but I don't understand your comment. Do you mean altering the $page render array? Or adding 'footer' as a key instead of [] ?
    – Ideogram
    Commented Jun 11, 2015 at 8:50
  • 1
    Yes I think you must be, I didn't know field render arrays were treated differently to normal render arrays. Seems like that's a bug, they shouldn't be IMO, but maybe they had a good reason to do it at the time. Did you try the hook_field_prepare_view() method mentioned over there? That looks promising
    – Clive
    Commented Jun 11, 2015 at 9:12

2 Answers 2


It turns out all I needed to do was to use hook_field_prepare_view() to add an extra, empty elements to the $items array

 * Implements hook_field_prepare_view().
function threejs_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items)
    $items[1]['footer'] = array( );

Same as @Ideogram but here is my full code. For example I have 4 images. My formatter makes the first image the main image and the rest including the 1st image thumbnails. I need to add the extra $item to the beginning.

 * Implements hook_field_formatter_prepare_view().
function yourmodule_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  foreach ($displays as $key => $display) {
    if ($display['type'] == 'my_formatter') {
      // We need to add an extra item for the main image.
      if (isset($items[$key][0])) {
        array_unshift($items[$key], $items[$key][0]);

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.