Skip to content

Instantly share code, notes, and snippets.

@orakili
Created October 31, 2012 06:24
Show Gist options
  • Select an option

  • Save orakili/3985387 to your computer and use it in GitHub Desktop.

Select an option

Save orakili/3985387 to your computer and use it in GitHub Desktop.
Drupal 7 - Smart trim module - Combined patch for 1819902 and 1806498
diff --git a/smart_trim.module b/smart_trim.module
index 1065112..88d9562 100644
--- a/smart_trim.module
+++ b/smart_trim.module
@@ -16,6 +16,7 @@ function smart_trim_field_formatter_info() {
'more_text' => 'Read more',
'summary_handler' => 'full',
'trim_options' => array(),
+ 'trim_preserve_tags' => '',
),
)
);
@@ -49,15 +50,16 @@ function smart_trim_field_formatter_view($entity_type, $entity, $field, $instanc
if (!empty($settings['trim_options'])) {
if (!empty($settings['trim_options']['text'])) {
// Strip tags
- $output = strip_tags(str_replace('<', ' <', $output));
-
+ $preserve_tags = !empty($settings['trim_preserve_tags']) ? $settings['trim_preserve_tags'] : '';
+ $output = strip_tags(str_replace('<', ' <', $output), $preserve_tags);
+
// Strip out line breaks
$output = preg_replace('/\n|\r|\t/m', ' ', $output);
-
+
// Strip out non-breaking spaces
$output = str_replace('&nbsp;', ' ', $output);
$output = str_replace("\xc2\xa0", ' ', $output);
-
+
// Strip out extra spaces
$output = trim(preg_replace('/\s\s+/', ' ', $output));
}
@@ -72,11 +74,22 @@ function smart_trim_field_formatter_view($entity_type, $entity, $field, $instanc
$output2 = _filter_htmlcorrector($output2);
}
else {
- //See http://api.drupal.org/api/drupal/modules%21field%21modules%21text%21text.module/function/text_summary/7
- //text_summary is smart about looking for paragraphs, sentences, etc, not strictly just length. Uses truncate_utf8 as well
- $output2 = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $settings['trim_length']);
+ // Use views_trim_text() if available.
+ if (module_exists('views')) {
+ $output2 = views_trim_text(array(
+ 'max_length' => $settings['trim_length'],
+ 'word_boundary' => TRUE,
+ 'ellipsis' => FALSE,
+ 'html' => TRUE,
+ ), $output);
+ }
+ else {
+ //See http://api.drupal.org/api/drupal/modules%21field%21modules%21text%21text.module/function/text_summary/7
+ //text_summary is smart about looking for paragraphs, sentences, etc, not strictly just length. Uses truncate_utf8 as well
+ $output2 = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $settings['trim_length']);
+ }
}
-
+
if (drupal_strlen($output) != drupal_strlen($output2)) {
$shortened = TRUE;
}
@@ -187,6 +200,13 @@ function smart_trim_field_formatter_settings_form($field, $instance, $view_mode,
'#default_value' => empty($settings['trim_options']) ? array() : $settings['trim_options'],
);
+ $element['trim_preserve_tags'] = array(
+ '#title' => t('Tags to preserve'),
+ '#description' => t('Which tags to preserve if "Strip HTML" is chosen above. Format as "&lt;p&gt;&lt;a&gt;" to preserve p and a tags.'),
+ '#type' => 'textfield',
+ '#default_value' => $settings['trim_preserve_tags'],
+ );
+
return $element;
}
@@ -211,4 +231,4 @@ function smart_trim_help($path, $arg) {
if ($path == 'admin/help#smart_trim') {
return '<p>' . t('This module creates a new text field formatter. There are no global configuration options.') . '</p>';
}
-}
\ No newline at end of file
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment