When creating a view you are presented with two types of row styling: fields and node. If you choose node, you have the ability to choose to show the teaser of the node. In certain situations, you might want to choose fields row styling, but also show the teaser version of a field. This can be accomplished by creating a template file for your field and modify its contents with a bit of PHP code.
By clicking on the "Theme: Information" section on the view edit screen, you can see different options you have for overriding a view. In this case, I looked for the theme file that matched the field, name of the view, and the view type:
views-view-field--MYVIEWNAME--block--MYFIELD.tpl.phpClicking on the link to the left of the theme file name options will show you the default contents of that file. Example: "Field Node: Body (ID: body)"
<?php
// $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $
/**
* This template is used to print a single field in a view. It is not
* actually used in default Views, as this is registered as a theme
* function which has better performance. For single overrides, the
* template is perfectly okay.
*
* Variables available:
* - $view: The view object
* - $field: The field handler object that can process the input
* - $row: The raw SQL result that can be used
* - $output: The processed output that will normally be used.
*
* When fetching output from the $row, this construct should be used:
* $data = $row->{$field->field_alias}
*
* The above will guarantee that you'll always get the correct data,
* regardless of any changes in the aliasing that might happen if
* the view is modified.
*/
?>
<?php print $output; ?>Create a new file in your theme directory that matches the naming convention and copy the default contents into it. Click on the "Rescan template files" button below the list of options. If everything is working correctly, your newly created theme file will be bold in the list, representing that its now the template file being used. If it's not, there's a chance you named it incorrectly.
In my example, I wanted to show the teaser view of a field and then append a "read more" link to the content. I replaced the contents of my newly created theme file with the following:
<?php
// generate the teaser for this field
$teaser = node_teaser($output);
// output the teaser
print $teaser;
// if the teaser if different from the original output,
// show a more link
if ($teaser!=$output) {
print ' ' . l('> Read More', 'node/'.$row->nid);
}
?>










Slight tweak
Great stuff Eric as always.
Just a slight tweak to pick up aliases (and to add styling, not essential) ....
// if the teaser if different from the original output,// show a more link
if ($teaser!=$output) {
$teaser = ' ' . l('More...', drupal_get_path_alias('node/'.$row->nid),array('attributes' => array('class' => 'moreTeaserLink')));
}
John McGeechan
I can't get the custom view
I can't get the custom view to show up as bold letters the way you describe.
I've named the file views-view-field--art_science_life--page--teaser.tpl.php where art_science_life is the view name and page is the view and teaser is the node field teaser.
"If you choose node, you have
"If you choose node, you have the ability to choose to show the teaser of the node."
Hi Eric,
I have node row-styling so where can I find the option to show or not show the teaser of the node?
Thanks,
Ju
JohnCH
how can I add the author name and the link to "View User Profile" ??
thanks in advance,
Can't seem to get this to work...
For whatever reason I can't seem to get this to work properly. I'm trying to set this up for a teaser field within a content pane... I've tried every php template within the listed themes of the views with your code, though none seem to catch it.
Thoughts?
thanks for this code! it
thanks for this code! it worked for me.
Thanks :)
This was a life saver. I've been searching for this for two days, and all I found was the edit read more module - http://drupal.org/project/ed_readmore - things about moving and styling it. It isn't very helpful when the actual "read more" link is not there.
It works like a charm ... except that I would like to use the Danish character "æ" in the link ("Read more" is "Læs mere" in Danish), but the link disappears when I use 'æ', and it doesn't work using the html-equivalent
æ- then it will print "læs mere". Normallyæwould work. Anyone got an idea why I cannot get it to print 'æ'?Thanks agian anyways !
/Anders:)
l function arguments
Check this page out: http://api.drupal.org/api/function/l
Try setting the 3rd argument in the l() function...
<?phpl('text', 'path', array('html' => true));
?>
Thanks & Question
Thanks, this is exactly what I want to do on my site. I've done what you suggested, and it worked perfectly except that "
" is now printed between the teaser and the read more link. How can I remove this? Thank you.
just a thought
Well, one thing you could do is manually remove the break:
<?php$teaser = node_teaser($output);
$teaser = str_replace('<!--break-->','',$teaser);
print $teaser;
?>
re: just a thought
Thanks for your quick reply, Eric! That's great, and it almost works, but PHP balks when I try to get it to replace a string that starts with "" part and leave a bracket. !">
I'm new to PHP, and I know that you are suppose to put a "\" before apostrophes in strings, but that doesn't work here. I'm still trying to figure out how to deal with the " !">
let's take a peak
Can you post your code?
the code
Thanks Eric, here's the code:
<?php
// generate the teaser for this field
$teaser = node_teaser($output);
// remove break marker if present
$teaser = str_replace('<!--break-->', '', $teaser);
// output the teaser
print $teaser;
// if the teaser if different from the original output,
// show a more link
if ($teaser!=$output)
{
print ' ' . l('> Read More', 'node/'.$row->nid);
}
?>
I can put any string that doesn't start with " !">
hmm
I just tried this code snippet:
<?php$text = "AAA<!--break-->BBB";
$text2 = str_replace('<!--break-->','',$text);
var_dump($text2);
?>
And, got the following:
string(6) "AAABBB"Can you send screenshots, html source, contents of the variables, or anything else that might help? :)
Got it!
Thanks for your help. I was just looking at the source and I've figured it out. Even though the body source of the node contained the string "
" by the time it reached the view it had been converted to:
<!--break-->So using this worked fine:
$teaser = str_replace('<!--break-->', '', $teaser);Thank you!
ahh, yes
glad you figured it out