I have a site where in most of the articles there is an image at the top (inserted using Media + WYSIWYG). Using the standard “Summary or Trimmed” format, this image was a problem because I just want the first lines of the first paragraph as a summary…

My solution – and I am posting this here as it could help others – was to create a very simple module that adds an extra format that I could use for the summaries.

Here it goes the code, in two files (media_stripper.info and media_stripper.module):

media_stripper.info

;$Id$

name = Media Stripper
description = A module that strips [[media]] before trimming.
package = Drupal 7 Development
core = 7.x
files[] = media_stripper.module

;dependencies[] = autoload
;php = 5.2

media_stripper.module

// $Id$

/**
 * @file
 * A module that strips [[media]] before trimming.
 *
 */

/**
 * Implements hook_help().
 */
function media_stripper_help($path, $arg) {
  if ($path == 'admin/help#media-strippper') {
    return t('This module is used to strip [[media]] tags before trimming to create a summary.');
  }
}

/**
 * Implements hook_field_formatter_info().
 */
function media_stripper_field_formatter_info() {
  return array(
    // the key must be unique, so it's best to prefix with your module's name.
    'media_stripper_trimmed' => array(
      // the label is is what is displayed in the select box in the UI.
      'label' => t('Trimmed - without media'),
      // field types is the important bit!! List the field types your formatter is for.
      'field types' => array('text', 'text_long', 'text_with_summary'),
      // You can usually leave 'multiple values' as follows:
      'multiple values' => FIELD_BEHAVIOR_DEFAULT,
    ),
  );
}

/**
 * Implements hook_field_formatter_view(). This code just passes straight
 * through to a theme function, media_stripper_formatter_FORMATTER
 * (e.g. media_stripper_formatter_media_stripper_absolute_url).
 */
function media_stripper_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  foreach ($items as $delta => $item) {
  
    if (!empty($element['summary'])) {
      $output = _text_sanitize($instance, $langcode, $item, 'summary');
    }
    else {
      // Take out all the media first. Also the unconfortable <p></p>
      $pattern = array('/<div class="media[^"]+">.+<\/div>/im', '/<p><\/p>/im', '/<p[^>]+><\/p>/im');
      $item['safe_value'] = preg_replace($pattern, '', $item['safe_value']);
      
      // Then produce the output
      $output = _text_sanitize($instance, $langcode, $item, 'value');
      $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, '500');
    }
  
    $elements[$delta] = array(
      // There is another possibility here, calling the theme version.
      // For the time being, I don't use it.
      // '#markup' => theme('media_stripper_formatter_'. $display['type'], array('element' => $item, 
      '#markup' => $output, 
      'field' => $instance)),
    );
  }
  return $elements;
}

/**
 * Implements hook_theme().
 */
function media_stripper_theme() {
  return array(
    // The theme function that our formatter uses:
    'media_stripper_formatter_media_stripper_trimmed' => array(
      // Don't forget that all Drupal 7 theme functions have only one argument,
      // so you declare what variables get passed within that argument instead.
      // See drupal.org/node/224333#theme_changes
      'variables' => array('element' => NULL),
    ),
  );
}

/**
 * Theme function for 'media_stripper_trimmed' text field formatter.
 */
function theme_media_stripper_formatter_media_stripper_trimmed($element) {
  // This code is specific to the Link field. It is based on theme_link_formatter_plain().
  return empty($element['element']['url']) ? check_plain($element['element']['title']) : url($element['element']['url'], array('absolute' => TRUE, 'attributes' => $element['element']['attributes']));
}

This could be expanded this a bit more, i.e. adding an extra configuration value to set the number of characters, etc.

Comments

dnotes’s picture

Category: task » feature

I am in the exact same position, and I think that something like this belongs in core media module. I'm switching this to a feature request so that the community can discuss it.

dnotes’s picture

Status: Active » Needs review
StatusFileSize
new2.8 KB

Maybe something like this.

Patched against 7.x-1.0-RC2.

Caveat: I'm not quite sure how the "safe_value" of a field gets calculated for cached text formats, so there may be a better way to do this than deleting that data.

dave reid’s picture

Status: Needs review » Closed (won't fix)

Please feel free to post your solution as a sandbox, but I do not believe this is the right way to handle this. I have a feeling this should be done on a more general basis of stripping tags or applying a different filter to a text field's summary than handling it as a special case here in Media.