For the last two versions of the views module I have had the requirement of putting img thumbnails for uploaded files in the returned results. I have solved my problem by hacking the code to add the additional feature of a field where a place holder value gets replaced by the field value. I would really like to see this feature make it into the released version at some point. Here is the code I used, it works for what I need but could use a little refinement for the general public.
file: "handlers/views_handler_field_prerender_list.inc"
find:
$form['empty'] = array(
'#type' => 'textfield',
'#title' => t('Empty list text'),
'#default_value' => $this->options['empty'],
'#description' => t('If the list is empty, you may enter text here that will be displayed.'),
);
append:
+ $form['replace'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Replace Placeholders'),
+ '#default_value' => $this->options['replace'],
+ '#description' => t('If this field is filled in then the value [value] will be replaced with the value of the field.'),
+ );
then a little lower in the file a few additions to the render function:
function render($values) {
$field = $values->{$this->field_alias};
+ if(strlen($this->options['replace']) > 0) {
+ foreach ($this->items[$field] as $key => $value) {
+ $this->items[$field][$key] = str_replace('[value]',$value,$this->options['replace']);
+ }
+ }
if (!empty($this->items[$field])) {
if ($this->options['type'] == 'separator') {
return implode(check_plain($this->options['separator']), $this->items[$field]);
}
else {
return theme('item_list', $this->items[$field], NULL, $this->options['type']);
}
}
else if (!empty($this->options['empty'])) {
return $this->options['empty'];
}
}
}
Comments
Comment #1
merlinofchaos commentedI've read through this code 3 or 4 times and I can't figure out what this does. I suspect that means none of the Views users will be able to figure out what it does either.
Comment #2
jereme.guenther commentedthanks for reading it that many times, most probably would have given up on the first attempt.
I made this code specifically to modify the "Upload: Attached Files" field; though it probably modifies other fields as well. If you add that field to a view, running on a code base with this modification, it will add a text field with a label of "Replace Placeholders:".
In this text field you can enter anything you want, in my case it was the following html code:
<a href="/sites/default/files/images/[value]"><img src="/sites/default/files/images/[value]" width="100px"/></a>When the view was run it rendered this code replacing "[value]" with the file name of each uploaded file. So I ended up with a list of thumbnail images that linked to the original image file.
I can think of lots of other potential uses for this field, but that is the particular reason I created the mod in the first place.
Comment #3
merlinofchaos commentedOk, I see what you're trying to accomplish now.
My instinct is that what you're doing should be done at the theming layer. Since we're doing it in a pre render list that's a little harder than I first thought, though, but it seems like there ought to be a way to do it right. I'm uncomfortable with replacement tokens and a textfield; it's difficult to tell what's going on here.
Comment #4
esmerel commentedThere's been a huge number of changes since this was originally opened; even if this shouldn't be done at the themeing layer, the code almost certainly needs a complete rework, and would be better opened if necessary against a more current release.