tags:

views:

624

answers:

2

i created a view that displays my homepage fine but now a modification is needed: i load 2 fields (images) in my view but need to only display one of those, depending on the value of a third (date) field and today's date. if date field is later than today, show image y and if its earlier than today show image x. this kind of logic cant be done in a view.

so in my template.php id like to output x or y as $vars['img'] in the preprocess_page function. im just wondering, how do i get at the values of those fields? its not a node but a list of node teasers.

  • the function gets passed &$vars but a print_r of those just shows the html output.
  • custom sql seems not the way to go.
  • when i load the view, i just get the html it outputs but (i think) i need the raw data to make the date comparison.

thanks for any pointers!

A: 

I'm sure there are some ways to do this, some more hackier than others. I would:

  1. Make a template specific to your view
  2. Create a preprocess function for the view and create a boolean variable, by checking which img that should be displayed.
  3. Lastly I would alter the template slightly by making an if statement that checks if it should display img x or y.

This solution is pretty easy and straight forward, the downside is that it's not completely general as you most likely will need to know the names of the cck field names you are using. It's doubtful you would be able to generalize this anyways.

Edit:

To clarify a bit. You can look at the general views template that's being used, it will give you some insight as to how views prints the different fields. Generally getting at the fields is usually not a big problem when you have the $node object. The reason is that cck adds the fields to the $node object so you can access it there. I believe you can do something in the line of $node->field_[the_name] to get to the field. I would suggest that you use the devel module if you don't already and do a dpm($node) somewhere in the template where you loop through the nodes. That will enable you to see what has been defined on the $node object. It shouldn't be a big problem to print the img from there.

googletorp
stef
A: 

How about using preprocess_node() instead?

threecheeseopera

related questions