add some logic coding to the view

artatac - July 6, 2009 - 07:04
Project:Views
Version:6.x-2.6
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Is there a way of using " Rewrite the output of this field" to add some logic coding to the view that says "if Content:image thumbnail is empty - use default.jpg

I have a view block on my front page that shows a thumbnail and title of each of the latest four nodes and it looks odd if the story happens to be one without a pic. In Drupal 5 I solved it by hand coding. But now I use the excelent views 2 I seem to have lost this opportunity

<?php if ($count == 0 ) { ?>

    <div class="top">
  <?php print $title?>
  <?php
             
             
if ($node->node_data_field_image_field_image_fid != 0) {
                print
$field_image_fid  ;
            } else {
                  print
'<img src="fruit.jpg" />';
            }
             
               
               
             
?>

</div>


<?php } else { ?>


<div class="nottop">
  <?php print $title?>
</div>
<?php } ?>

#1

artatac - July 6, 2009 - 07:08

An example of the correct result in Drupal 5 can be seen at www.allonsong.com

and the (limited) drupal 6 result at www.venturacottage.homedns.org/610startextra04 (middle left block fruit)

thanks

joe

#2

merlinofchaos - July 6, 2009 - 15:41
Status:active» fixed

Rewrites with coding can be done at the theme layer. You can theme the field or theme the style.

#3

artatac - July 6, 2009 - 17:03

Thank you are there any handbooks /tutorials that could get me started with this please?

The biggest block I have found is (as far as styling goes...

<?php
print $topleft
?>

which it simply gets from views - I am at a loss how I intercept this content AFTER views 2 has created it and before the frontpage gets it to put in the if else pic logic

Regards

Joe

#4

danielb - July 7, 2009 - 08:54

Click 'theme information' inside your views admin on the view, it will give you the info you need to create the new template. Some more info on this here: http://www.group42.ca/theming_views_2_the_basics

#5

System Message - July 21, 2009 - 09:00
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

#6

artatac - July 21, 2009 - 10:43

I tried adding the following code to the theming area of my view as views-view-field--Flowers-latest.tpl.php it resulted in all the block content disappearing and being replaced by the default image (that bit worked!). But it ignored the request to print the $title and ignored the fact that most stories already had an image so didnt need the default

<?php if ($count == 0 ) { ?>

    <div class="top">
  <?php print $title?>
  <?php
             
             
if ($node->node_data_field_image_field_image_fid != 0) {
                print
$field_image_fid  ;
            } else {
                  print
'<img src="flower.jpg" />';
            }
             
               
               
             
?>

</div>


<?php } else { ?>


<div class="nottop">
  <?php print $title?>
</div>
<?php } ?>

 
 

Drupal is a registered trademark of Dries Buytaert.