I have an entity that has an image field attached to it. This field can hold multiple images. I would like to be able to display a single/first image with one field formatter from this field at one place and all images with another formatter from this field in another place.

~~So I need to somehow clone this field, most likely via custom configuration to store some basic data and attaching it via hook_entity_base_field_info().

What I am not sure about is how to use the proper field type for this "computed" field so I can use proper field formatters, how to set proper source of data(the original field) and how to properly use isEmpty() method on that field so this field won't show up if the original field is empty without using my own field class to do that(since I would lose the compatiblity with proprr formatters).~~

Sorry, that was a bit misleading. I only need to be able to display the field with different formatter which means I only need another instance of that field in the entity_view_display.

hook_entity_extra_field_info() seems like the best solution, but it does not allow formatter settings.

  • Is that entity is custom entity defined in code or you will use cck? – Mohammed Gomma May 27 '15 at 10:04

One way of doing this is in the theme layer. In this approach you don't need to have two fields. You could have a single Images field, and in a node template preprocess function you can break it up into two separate variables that are provided to the *.tpl.php file. So you could take the rendered output of the first image, put that in one place, and the rendered output of the rest of them could go somewhere else.


You can try to use the view() method directly, with two different view modes or custom settings.

Getting rid of all field items except the first is a bit complicated, but it's probably easier than trying to create a new field item list object from scratch.

Something like this:

$first_field = clone $entity->field_name;
// The list enforces a strict numeric list, so keep unsetting index 1 until it no longer exists.
while (isset($first_field[1])) {

$first_image = $first_field->view($view_mode_or_settings);

$other_items = clone $entity->field_name;
$remaining_images = $other_items->view($view_mode_or_settings);

Alternatively, depending on what you want to achieve, you can also try to alter the render array structure. Like almost all field formatters, you get a render array back, keyed by the numeric field data (use Element::children() to loop over them). Inspect it and for example switch out the used image style.

  • The image gallery was just an example. Basically I want to give the users the option to 'clone' any of the existing fields, only in the entity display, so they can configure it with a different formatter. – user21641 May 27 '15 at 19:38
  • Just cloning will still require similar code from. You can store your additional settings in third party settings of the entity display. But your gallery example is far more advanced, with special logic for handling parts of the field items only. Also have a look at Display Suite, it might provide quite a bit of what you're after already and should be more extensible/flexible than plain extra fields. – Berdir May 28 '15 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy