I'd like to make use of functionality similar to the "display summary if filled in, if not display shortened body" that is used by Drupal's text area with summary widget. Is there a publicly available function that will concatenate body text to a certain word length, taking into account that there may be embedded HTML?
1 Answer
The code used to render that field is contained in text_field_formatter_view():
case 'text_summary_or_trimmed':
foreach ($items as $delta => $item) {
if (!empty($item['summary'])) {
$output = _text_sanitize($instance, $langcode, $item, 'summary');
}
else {
$output = _text_sanitize($instance, $langcode, $item, 'value');
$output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
}
$element[$delta] = array('#markup' => $output);
}
break;
You are interested in how $output
is set. _text_sanitize() is a function that you can use to write similar code more specific for your case, while text_summary() is a generic enough function you can use for your own code.
function _text_sanitize($instance, $langcode, $item, $column) {
// If the value uses a cacheable text format, text_field_load() precomputes
// the sanitized string.
if (isset($item["safe_$column"])) {
return $item["safe_$column"];
}
return $instance['settings']['text_processing'] ? check_markup($item[$column], $item['format'], $langcode) : check_plain($item[$column]);
}