diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 8d21b2084739cf223f1321a4d2ece32b15a0a59b..56b53869d6279daf441d22ce0b7476090e97367a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -75,6 +75,7 @@ Views 2.x-dev o #730454 by stella: Add "translate" link along with view/edit/delete links on nodes. o #474174 by dereine: Analyze will now warn about "node/%" not being a valid path for Views. o #619642 by dereine: Allow argument for user language. + o #675154 by dereine and Crashtest_: Improvements to rendering trim text and tag stripping. Views 2.8 (Dec 02, 2009) Bug fixes: diff --git a/handlers/views_handler_field.inc b/handlers/views_handler_field.inc index db385bdb2782e7448fe33b5c12be736ba244a1cc..9365950f5d9f45f753e63aaa2829410c4ea56ea5 100644 --- a/handlers/views_handler_field.inc +++ b/handlers/views_handler_field.inc @@ -362,17 +362,6 @@ class views_handler_field extends views_handler { ), ); - $form['alter']['strip_tags'] = array( - '#type' => 'checkbox', - '#title' => t('Strip HTML tags'), - '#description' => t('If checked, all HTML tags will be stripped.'), - '#default_value' => $this->options['alter']['strip_tags'], - '#process' => array('views_process_dependency'), - '#dependency' => array( - 'edit-options-alter-trim' => array(1) - ), - ); - $form['alter']['html'] = array( '#type' => 'checkbox', '#title' => t('Field can contain HTML'), @@ -383,7 +372,16 @@ class views_handler_field extends views_handler { 'edit-options-alter-trim' => array(1) ), ); + + $form['alter']['strip_tags'] = array( + '#type' => 'checkbox', + '#title' => t('Strip HTML tags'), + '#description' => t('If checked, all HTML tags will be stripped.'), + '#default_value' => $this->options['alter']['strip_tags'], + '#process' => array('views_process_dependency'), + ); } + $form['empty'] = array( '#type' => 'textfield', '#title' => t('Empty text'), @@ -498,6 +496,10 @@ class views_handler_field extends views_handler { $tokens = $this->get_render_tokens($alter); $value = $this->render_altered($alter, $tokens); } + + if (!empty($alter['strip_tags'])) { + $value = strip_tags($value); + } if (!empty($alter['trim']) && !empty($alter['max_length'])) { $value = $this->render_trim_text($alter, $value); @@ -529,45 +531,12 @@ class views_handler_field extends views_handler { */ function render_trim_text($alter, $value) { if (!empty($alter['strip_tags'])) { - $value = strip_tags($value); // NOTE: It's possible that some external fields might override the // element type so if someone from, say, CCK runs into a bug here, // this may be why =) $this->definition['element type'] = 'span'; } - - if (drupal_strlen($value) <= $alter['max_length']) { - return $value; - } - - $value = drupal_substr($value, 0, $alter['max_length']); - - // TODO: replace this with cleanstring of ctools - if (!empty($alter['word_boundary'])) { - $regex = "(.*)\b.+"; - if (function_exists('mb_ereg')) { - mb_regex_encoding('UTF-8'); - $found = mb_ereg($regex, $value, $matches); - } - else { - $found = preg_match("/$regex/us", $value, $matches); - } - if ($found) { - $value = $matches[1]; - } - } - // Remove scraps of HTML entities from the end of a strings - $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value)); - - if (!empty($alter['ellipsis'])) { - $value .= '…'; - } - - if (!empty($alter['html'])) { - $value = _filter_htmlcorrector($value); - } - - return $value; + return drupal_trim_text($alter, $value); } /** diff --git a/views.module b/views.module index 5275d2f13b1dc5f24e06c21a23ec5b7dcb8d3a9c..678d2b95d1d6df2c9010770ac50f68353610b199 100644 --- a/views.module +++ b/views.module @@ -1293,3 +1293,36 @@ function views_microtime() { list($usec, $sec) = explode(' ', microtime()); return (float)$sec + (float)$usec; } + +/** + * Trim the field down to the specified length. + * + * @param $alter + * - max_length: Maximum lenght of the string, the rest gets truncated. + * - word_boundary: Trim only on a word boundary. + * - ellipsis: Trim only on a word boundary. + * - html: Take sure that the html is correct. + */ +function drupal_trim_text($alter, $value) { + dsm('strlen $value ' . drupal_strlen($value)); + dsm('max_length ' . $alter['max_length']); + if (drupal_strlen($value) > $alter['max_length']) { + $value = drupal_substr($value, 0, $alter['max_length']); + if (!empty($alter['word_boundary'])) { + if (preg_match("/(.*)\b.+/us", $value, $matches)) { + $value = $matches[1]; + } + } + // Remove scraps of HTML entities from the end of a strings + $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value)); + + if (!empty($alter['ellipsis'])) { + $value .= '...'; + } + } + if (!empty($alter['html'])) { + $value = _filter_htmlcorrector($value); + } + + return $value; +}