How to trim text on the user listing page
| Project: | Site User List |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Greetings,
I just wrote a little addition for our portal that made site_user_list a lot more usable, so I'm going to share it.
This patch will trim the fields to a desired length if they are too long.
First, let's create a trim function in template.php, this actually does folding as well, which I use elsewhere, so I'll include the javascript for that as well.
<?php
/**
* TRIM FOLD TEXT
* used by user_profile.tpl.php and site_user_list.module
*/
function trim_fold_text($text, $length=NULL, $nofold=NULL, $notags=NULL, $id=NULL) {
// note: there are always no tags for folding/trimming, use $notags for no tags always
if ($length <= 0) { $length = 255; } // fallback to default max length
$full_text = $text;
$text = strip_tags($text);
if (strlen($text) <= $length) { // text is not longer than max length, return full text
if ($notags) {
return strip_tags($full_text); // $notags was toggled, strip all tags
} else {
return $full_text; // return full text without any changes
}
}
if (!$id) { // no id - generate a random id
$id = rand(1,1000);
}
$ellipsis = " ...";
$words = explode(" ", $text);
$curindex = 0;
$wcount = 0;
foreach ($words as $value) {
$wcount += strlen($value)+1;
if($wcount > $length - strlen($ellipsis)){
break;
}
$curindex++;
}
if ($nofold) {
// do not create a fold, just trim the text
return implode(' ', array_slice($words, 0, $curindex)).$ellipsis;
} else {
// syntax: array_slice($array, offset, $length)
$short = "<div id='short".$id."'>".implode(' ', array_slice($words, 0, $curindex)).$ellipsis."<div><a href='javascript:;' onClick=\"showHide(['short".$id."'],false); showHide(['full".$id."'],true)\">[show more]</a></div></div>";
$long = "<div id='full".$id."' style='display:none'>".$full_text."<div><a href='javascript:;' onClick=\"showHide(['short".$id."'],true); showHide(['full".$id."'],false)\">[show less]</a></div></div>";
}
return $short.$long;
}
?>This is the javascript - put it in the [head] section of your page.tpl.php
<script type="text/javascript">
<!--
/* SHOW/HIDE OBJECT - syntax: onClick="showHide(['div_id'],true/false)"
* used by trim_fold_text() function
*/
function showHide(id,b){
for(var i=0;i<id.length;i++)
document.getElementById(id[i]).style.display=b?'':'none';
}
-->
</script>Very simple: true = visible, false = hidden.
Now here are some changes for site_user_list.module. There are two tricks here:
1) We need to trim the text.
2) We need to take out any tags (images can easily break the neat layout), but keep the avatar image.
- $row[] = strtr($fields[$f]['template'], $r);
+ // !custom - trim text if too long
+ if (preg_match('/\@picture/', $fields[$f]['template'])) {
+ $row[] = trim_fold_text(strtr($fields[$f]['template'], $r), '', 'nofold');
+ } else {
+ $row[] = trim_fold_text(strtr($fields[$f]['template'], $r), '', 'nofold', 'notags');
+ }This will now look if any text field is longer than 255 chars, trim the text if so, and take out all the tags, keeping the avatar photo.
For a discussion about avatar photos, see here: http://drupal.org/node/144100
Best,
Andrey.

#1
A small correction:
There's no need to strip tags from the username column, as it will remove the link to profile.
In the above patch to site_user_list.module, replace this:
if (preg_match('\@picture/', $fields[$f]['template'])) {With this:
if (preg_match('/(@picture)|(@name)/', $fields[$f]['template'])) {Also, if you want to keep tags in other columns, just add them to the list of preg_match.
Best,
Andrey.