11

I want to display a teaser on the top of each article under the title, as in following “full content” layout:

  1. Title
  2. Teaser (article summary)
  3. Image
  4. Body

Moreover the teaser should have its own formatting (for example italic and a bigger font)

I did not find any way to do that without editing tpl files or core files.

A solution would be to add a new field "article_teaser" to my content type, then manage display in order to get my layout in “Full content” view. This works, but I do not like to have two teaser-fields in my input content form, because I should type two times the same text.

It seems that I cannot split the field body in its “text” and “summary” parts, but I can only display one or the other, but not both!

Is there any way (a module?) to do that?

(My environment: Drupal 7 with Zen sub theme.)

10

You can do this with Views. Create a view that overrides the paths for your content type. Set the display to Fields and then add the body field twice; set the first instance to display the teaser and the second instance to display the body. You can add the images in between. Views will also allow you to customize the CSS of the teaser/body fields easily (by default, Views provides a number of classes, and you can add additional classes if necessary).

For reference: Node One series of video screencasts on using Views

  • Thank you very much. The link is very useful, and I am learning a lot about views: I got a view that did the stuff. But views show list of nodes, while my content type (a book page) is a single page: how can I override a single book page (a node) with a list of nodes (the view I created)? I am probably missing something about views, maybe how to override the default book page with its own. – gingo Feb 13 '13 at 7:53
  • @gingo To override all book pages, you need to configure the view path in a special way. For an example, look at the Taxonomy term view, which is provided by Views by default (disabled). The path is set to /taxonomy/term/%; if you edit that view, you can see how the arguments (contextual filters) are pulled in to allow you to edit the display for taxonomy terms. You can set something similar up for book pages on your site. – Patrick Kenny Feb 13 '13 at 8:19
  • well, I have to look deeper in contextual filters but I think I am on the right way. thank you. – gingo Feb 13 '13 at 10:32
3

Views can definitely do the job. But I think it is a little bit overkill for this requirement.

Another way to achieve that is implement hook_field_extra_fields() and hook_node_view() in your custom module.

/**
 * Implements hook_field_extra_fields().
 */
function mymodule_field_extra_fields() {
  // Put the content type you want to display summary field here.
  $content_type = 'page';

  $extra['node'][$content_type]['display']['body_summary'] = array(
    'label' => t('Body summary'),
    'description' => t('Display body summary.'),
    'weight' => 0,
  );

  return $extra;
}

/**
 * Implements hook_node_view().
 */
function mymodule_node_view($node, $view_mode, $langcode) {
  // Put the content type you want to display summary field here.
  $content_type = 'page';

  if ($node->type == $content_type) {
    $summary = field_view_field('node', $node, 'body', array(
      'type' => 'text_summary_or_trimmed',
    ));

    $node->content['body_summary'] = array(
      '#markup' => $summary,
      '#weight' => 0,  
    );
  }
}

Clear the cache and you can go to the content type "Manage display" settings to drag and drop to place the "body summary" field. For example admin/structure/types/manage/page/display.

  • Hm I think this is close, but the field content doesn't display, just Array. – arjan Oct 8 '14 at 16:48
  • Hey @arjan, have you checked Array data and copy it here, please? – eric.chenchao Oct 14 '14 at 23:00
  • What's the point creating a variable, 1 line above the only place it is used, just put the string in the if statement – Kiee Mar 23 '17 at 16:08
  • Hi @Kiee, you are right. But this snippet is used as example with comment ;) – eric.chenchao Mar 26 '17 at 23:01
3

on drupal 7:

In the field Body you can use 'rewrite result' and use in the 'replacement patterns' this:

[body-summary]

and this this will display the full summary.

  • You are a genius Javier, all the other overkill explanations, yours is the one that works the best by far and sooooooooooooo simple! – Bruno Vincent Dec 27 '17 at 16:15
0

For content type redefinition you need to look at Panels because one of the function of Panels is exactly redefining the basic look of certain Content Types (or information based on other selection criteria) Csaba

0

As Arjan noticed Cityreader's snippet produces the result but it does not render properly.

This variation worked for me:

 /**
 * Implements hook_field_extra_fields().
 */
function mymodule_field_extra_fields() {
  // Put the content type you want to display summary field here.
  $content_type = 'page';

  $extra['node'][$content_type]['display']['body_summary'] = array(
    'label' => t('Body summary'),
    'description' => t('Display body summary.'),
    'weight' => 0,
  );

  return $extra;
}

/**
 * Implements hook_node_view().
 */
function mymodule_node_view($node, $view_mode, $langcode) {
  // Put the content type you want to display summary field here.
  $content_type = 'page';

  if ($node->type == $content_type) {
    $summary = field_view_field('node', $node, 'body', array(
      'type' => 'text_summary_or_trimmed',
    ));

    $node->content['body_summary'] = array(
      '#markup' => $summary['#items'][0]['safe_summary'],
      '#weight' => 0,  
    );
  }
}
0

If you are interested in no-code solution using Views and Display Suite module you can:

  1. Create Views block with summary field on it. Under Contextual filter option in advanced settings you can set "Provide default value" option with type Content ID from URL.

  2. Under Content Types -> [your content type] -> Manage display select Custom fields option (menu at the bottom) and create Custom block field from View you created in step 1.

  3. On save Custom field will appear in the node type field list where you can choose in which region will it appear.

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.