I've been trying to makeup a little bit the output for text that are trimmed and/or summary to have a look similar to the one get from the views module, but I haven't had to much luck.
I read issue #841188: About the teaser size but this is for Drupal 6 so I tried to find a way around and this is what I came with:
<?php
function mytheme_preprocess_field(&$variables) {
if (isset($variables['element']['#field_name'])) {
switch ($variables['element']['#field_name']) {
case 'body':
if ($variables['element']['#formatter'] = 'text_summary_or_trimmed') {
$item = $variables['element']['#items'][0];
if (!empty($item['safe_summary'])) {
$text = $item['safe_summary'];
}
else {
$text = $item['safe_value'];
}
$trimmed = views_trim_text(
array(
'max_length' => 100, /* we need the lenght set by UI */
'word_boundary' => TRUE,
'ellipsis' => TRUE,
'html' => TRUE,
),
$text
);
$variables['items'][0]['#markup'] = $trimmed;
}
break;
}
}
}
?>
As you can see I'm using the function views_trim_text($alter, $value) that does what I need, but I haven't been able to find the actual length value for this trim.
I feel that I'm really close to the solution but my search in the multiple dpm() that I have placed around are not helping me.
Any ideas of how to get this value? By the way, how can I do it with a display suit hook alter? I read the API but I didn't have much luck either.
I wish this was already part of Display Suite.
Comments
Comment #1
swentel commentedYou should use hook_field_formatter_info() and hook_field_formatter_view(), that will make it easier to create additional formatters for a field which you can put into a module.
Comment #2
altrugon commentedThank you I will try it.