In Drupal 6, when you make a $node = node_load(14) you get a object $node->teaser immediately.

Drupal 7 seems it's not the case. I understood that the teaser should be here:

$node->body['und'][0]['summary']

but it's not.

drupal_render(node_view(node_load(14),'teaser'));

this will render all "accessories" of the teaser view (comments links, title, admin contextual menu, etc)

Maybe manipulating the node_view object before rendering can be a solution... but it's not very elegant.

Is there a simple way to get the HTML of the teaser ONLY?

Thanks!

Comments

lorinpda’s picture

Hi,
I am not sure that I understand your question (or the context). However, here is something to get you started.

In Drupal 7 the concept of "entities" is introduced. A "node" is a specialization of an "entity". An entity can be defined as "fieldable" (which node is). A fieldable entity in theory could contain more than one body field, thus the "delta" value (see below in my example).

Content types are a further specialization of "node". Drupal 7 comes with 2 built in "content types" upon install, (article and page). In the following example I created an "article" and explicitly set a "summary" (aka "teaser"). The system assigned the new "article" a nid value of 4.

  $nid = 4;
  $delta = 0;
  $language = 'und';

  $entity = entity_load('node', array($nid));
  $teaser = $entity[$nid]->body[$language][$delta]['summary'];
  $html_teaser = $entity[$nid]->body[$language][$delta]['safe_summary'];

The var $html_teaser contains the "teaser" with it's corresponding html markup. The var $teaser contains text only.

Note! Drupal 7 is really great however it introduces new data structures. One of the things we've noticed on this forum is that folks don't set up normal development environments. You install Drupal locally so that you have access to the system logs and can run the code through a debugger. You deploy to a remote installation, never develop on a remote installation.

The other advantage of the local installation is easier access to the Drupal source code (which is documented really well). With a debugger you can watch the code execute. Really the best way to solve your question :) Hope that helps.

corbacho’s picture

Thanks for the detailed answer lorinpda. :) It helps to grasp the new structures of Drupal 7

In your example you created explicitly a summary (teaser), this way I see it works.

But the most frequent use case is to let Drupal generate the teaser, trimming the body. This is the teaser I am interested to grab, and it's not loaded when you use node_load or entity_load. (as it was generated in D6)

corbacho’s picture

This way works:
$node = node_view(node_load(14),'teaser');

basically node_load is a wrapper for entity_load('node', ...... )

So now you can access to the teaser markup here:
$node['body'][0][#markup];

Simon Naude’s picture

 $nl = node_load($n) ;
 /* get body from loaded node, up to '</p>' ; strip all tags except 'br />' and '<br>' ; substring to last ' ' (inclusive) ; add '... '. l('Learn more', node_path_alias) ; */
      $nodetitle = '' ; // title from node
      $
      $nodebody = $nl['body']['und'][0]['value'] ;
      $newteaser = '' ; // make teaser from $nodebody ...
     // display generated teaser.
charlie-s’s picture

Glad you found the answer you needed. I wanted to point out to others that see this that what you're really doing is specifying the view mode of the node that you want to display. In this case, you're saying "give me a render array for node 14, in the view mode 'teaser'". If you were to simply run node_load([nid]) without specifying the view mode, it would be "full" (take a look at the API page for node_view(): http://api.drupal.org/api/drupal/modules%21node%21node.module/function/n...).

While Drupal 6 had 2 built-in view modes (normal and teaser) Drupal 7 provides for a limitless number of view modes, while 2 are added by Drupal core (full and teaser). It can be useful to add additional view modes depending on the context or user role or something like that, and then display the node in that particular view mode.

Ranieri Machado’s picture

$node_view = node_view(node_load($node_id),'summary');
$summary = $node_view['body'][0]['#markup'];

Jaypan’s picture

I used this:

$rendered_teaser = render(node_view(node_load($nid), 'teaser'));

This generates a themed teaser for the node represented by $nid.

naber’s picture

Used to render product view teaser

sirajs’s picture

This worked for me in Drupal 7 (inside overridden theme template: node--article.tpl.php):

$body =field_get_items('node', $node, 'body');
$teaser= $body[0]['summary'];

In place of 'summary' you can also use 'safe_summary'.

sergiomazza’s picture

Works for me!
Thanks.

jghyde’s picture

First define the page in hook_menu:

<?php
/**
 * Implements hook_menu()
 */
function MYMODULE_menu() {
  $items['mymodule/%ctools_js/%'] = array(
      'title' => 'Title of the Page Default',
      'page arguments' => array(1, 2),
      'page callback' => 'MYMODULE_modal_page',
      'access callback' => TRUE,
      'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}
?>

Then, define the modal theme and settings:

<?php
// I did this for a view, where I want the links the view generates to open a modal:
/**
 * Implements hook_views_pre_render()
 */
function MYMODULE_views_pre_render(&$views) {
  if ($views->name == 'MYMODULE_view') {  // Set to the view name created in views_ui
      // Include the CTools tools that we need.
      ctools_include('ajax');
      ctools_include('modal');

      // Add CTools' javascript to the page.
      ctools_modal_add_js();

      // Create our own javascript that will be used to theme a modal.
      $MYMODULE_modal_style = array(
        'MYMODULE-modal-style' => array(
          'modalSize' => array(
            'type' => 'fixed',
            'width' => 500,
            'height' => 300,
            'addWidth' => 20,
            'addHeight' => 15,
          ),
          'modalOptions' => array(
            'opacity' => .5,
            'background-color' => '#000000',
          ),
          'animation' => 'fadeIn',
          'modalTheme' => 'MYMODULE_Modal', // This is the settings or theme name in your module's *.js file preamble.
          // Make your own images and place into ./MYMODULE/images and use this func:
          // ctools_image_path($image, $module = 'ctools', $dir = 'images')
          'throbber' => theme('image', array('path' => ctools_image_path('ajax-loader.gif', 'MYMODULE'), 'alt' => t('Loading...'), 'title' => t('Loading'))),
          'closeImage' => theme('image', array('path' => ctools_image_path('modal-close.png', 'MYMODULE'), 'alt' => t('Close window'), 'title' => t('Close window'))),

        ),
      );

    drupal_add_js($MYMODULE_modal_style, 'setting');
    ctools_add_js('MYMODULE-modal-style', 'MYMODULE');
    ctools_add_css('MYMODULE-modal-style', 'MYMODULE');
  }
}
?>

For more about above, see: http://api.drupalize.me/api/drupal/function/ctools_add_js/7

Generate the links:

<?php
// I hook_theme'd a views template, so I preprocessed a new $vars to print on that template. You can
// add your links anywhere in the module hooks or theme funcs to render these links like this:
/*
 * Implements hook_preprocess_views_view_fields()
 */
function MYMODULE_preprocess_views_view_fields(&$vars) {
  if ($vars['theme_hook_suggestion'] == 'views_view_fields__MYMODULE') {
    $name = $vars['fields']['field_first_name']->content . ' ' . $vars['fields']['field_last_name']->content;
    $href = 'MYMODULE/nojs/' . $vars['fields']['nid']->content;
    // Anytime you use a ctools func, you have to declare the ctools operations first, or WSOD!
    ctools_include('modal');
    ctools_include('ajax');
    /* ctools_modal_text_button($text, $dest, $alt, $class = '')  */
    $vars['ctools_link'] = ctools_modal_text_button($name, $href, t('View contact info for @name', array('@name' => $name)) 'ctools-modal-MYMODULE-modal-style');  // Make sure to add the style name as class
  }
}
?>

For more of above, see: http://api.drupalize.me/api/drupal/function/ctools_modal_text_button/7

Define the page callback set forth in the first step above:

<?php
/**
 * A modal static page callback.
 */
function MYMODULE_modal_page($js = NULL, $nid = NULL) {
  if ($nid == NULL) {
    return t('Nothing to show because you did not provide a $nid.)';
    // or insert a drupal_goto the page itself instead.
  }
  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
  }
  $node = node_load($nid);
  $contents = render(node_view($node, 'teaser', NULL));
  return ctools_modal_render($node->title, $contents) ;
}
?>

I hope this helps!

Joe
(edited to offer customizations of images, throbber, etc 9/16/2012)

Local News Platform Built on Drupal
http://sanangelolive.com/

SergFromSD’s picture

Does anyone know how to force the the render function to use full URIs. For example, when I render I teaser the href link to the original article is href="/blog/...". Fine for instances when the teaser is displayed on the website. The problem is that some modules like Forward use this function to render an email with the teaser and at that point the partial path does not work. I would like to force the teaser to create a full uri like: href="http://mysite.com/blog/...".

Thanks for the input.

jghyde’s picture

Look at theming the field with the URL on the theme layer with template.php.

Something like this in template.php inside your active theme folder, with the caveat that your field is labeld 'URL' and your theme name is THEMENAME:

<?php
function YOURTHEME_field($variables) {
  // Install the devel module to inspect the variables array:
  dpm($variables);
  if ($variables['label'] == 'URL') {
    $output = '';
    // Do stuff to fill up the $output
    return $output;
  }
}
?>

References: Field API https://api.drupal.org/api/drupal/modules%21field%21field.module/functio...
Devel Module: http://drupal.org/project/devel

Joe

Local News Platform Built on Drupal
http://sanangelolive.com/

peezy’s picture

Putting this code snippet in node--my_content_type.tpl.php (where my_content_type is the machine name of the content type in which you want the teaser to appear) worked for me:

  <?php
    $teaser = field_view_field('node', $node, 'body', array(
      'label'=>'hidden',
      'type' => 'text_summary_or_trimmed',
      'settings'=>array('trim_length' => 500),
    ));
    print render($teaser);
  ?>

This will give you complete control over the rendering of the teaser by 1) displaying the summary of the body field if it exists, 2) hiding the label 'Body' from output, and 3) If there is no summary, trim the text to 500 characters. Props to this post for pointing me in the right direction.