14

I have a Content Type with an Image Field. This field will typically be populated with 0 to 4 values. By default the teaser for this Content Type outputs all 0 to 4 images. How can I adjust the Teaser to only display the first image?

12

Create a custom module, replace MODULENAME with the name of the module and field_image with the name of your field:

/**
 * Implements hook_entity_view().
 */
function MODULENAME_entity_view($entity, $type, $view_mode, $langcode) {
  if ($view_mode == 'teaser') {
    $entity->content['field_image']['#items'] = array_slice($entity->content['field_image']['#items'], 0, 1);
  }
}

See the hook_entity_view() documentation for more details.

  • This looks promising, I'll give it a try. It may be a couple days before I can implement it and report back. I'm relatively new to writing modules, so I have a couple questions: (1) will your sample code work (not crash) when there are zero values in the field and (2) will it work (not crash) when there are other content types without the Image field? Thanks! – Greg Mar 15 '11 at 15:08
  • 1) Yes, it should. 2) You could change the if statement to be if ($view_mode == 'teaser' && $entity->type == 'CONTENT_TYPE'). – tim.plunkett Mar 15 '11 at 15:19
  • 4
    Or simply && isset($entity->content['field_image']), then you don't need to update the code when adding the field to another content type or renaming that one, or.. :) – Berdir Mar 15 '11 at 17:43
  • Hmmm. This might work, but it's an example of putting into code what should be in a theme. – artfulrobot Jul 11 '14 at 9:44
  • It will work, but if you solve thinks like this by custom code the site will be less maintainable in the future. Use some module from drupal.org mentioned in other answers instead. – Bobík Jul 8 '15 at 14:32
12

No need to write custom code.

Try using Field multiple limit module.

It allows you to go into the display settings for your content type and configure the teaser settings to display only a certain amount of values. You can even make it skip vales, for example if you want it to display values 2 to 5

.

4

If using a teaser template (ie: node--CONTENT-TYPE--teaser.tpl.php), this works:

  <?php print render($content['IMAGE-FIELD'][0]); ?>
1

I used hook_field_extra_fields() and hook_node_view() to create a new field to display the first image so that I wasn't tampering with the original images field.

hook_field_extra_fields() initialises the new field which will appear on the manage display tab of your node type.

function HOOK_field_extra_fields() {
  $extra['node']['NODE_TYPE'] = array(
    'display' => array(
      'primary_image' => array(
        'label' => t('Primary Image'),
        'weight' => 0,
      )
    ),
  );
  return $extra;
}

Then hook_node_view() is used to add the first image to be the content of your new field.

function HOOK_node_view($node, $view_mode, $langcode) {
  if($node->type == 'NODE_TYPE' && $view_mode == 'teaser') {

    //make sure there are images
    if(!empty($node->field_images)) {
      $node->content['primary_image'] = $node->content['field_images'][0];

      //hide the original field with multiple images
      hide($node->content['field_images']);
    }
  }
}

NOTE: If you have display suite enabled you can do this in the manage display tab.

Your Answer

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

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