I recently created a Bay Area concert calender site, and ran into a little snag.
I have a basic frontpage that spits out the Title (bands playing) of each node as a link. Some of these nodes have content in a specific CCK field (a brief preview of the concert), while most nodes only have data/content in the Title field -- the show preview field value is null. Ideally, I would only like the node title to appear as a link when the show preview field has content.
Is there anyway to create a view that will display all node titles, but only have these titles link to the node when a specific node title (or even the body) is not null?
I apologize in advance if that has already been addressed, but I have difficulty finding anything that touches on this request.
Thanks!
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | view-export.txt | 6.09 KB | mkeith |
| #3 | Empty_Body_View_Export.txt | 3.63 KB | Anonymous (not verified) |
Comments
Comment #1
Anonymous (not verified) commentedI can't think of a way to do it in the normal Views interface (but that doesn't mean you can't...), so I'd suggest overriding one of the views template files and using PHP if() statements to check if a specific field is empty or not. If the field has a value, you can make the title a link in the template file.
See the Views help index for more information. Hope that helps!
Comment #2
epicsauce commentedThanks for the update here -- starting to dig into it, but its admittedly a bit outside of my knowledge base.
If anyone has tackled a related issue and has a snippet I could use as a starting point, please let me know.
Also, generally speaking would an if statement that checks to see whether a field is NOT NULL be unusually expensive?
Thanks!
Comment #3
Anonymous (not verified) commentedI've attached an export of a test view I setup. It's basically just a list of stories showing the title, body and NID. I then overrode one of the views template files to add PHP code to check if the body is empty and make the title a link.
If you click the 'Theme: Information' link in the view, you'll see a list of the overridable template files. The one I chose was the 'Row style output', so I created a file with the name 'views-view-fields--Empty-Body--page-1.tpl.php' and added the following code to that file:
This seems to give the output you're looking for.
Comment #4
mcrittenden commentedFor more information on theming Views and template.php hierarchies, see http://www.group42.ca/theming_views_2_the_basics
Feel free to unfix this issue if you still have questions.
Comment #5
epicsauce commentedThanks so much for this update! Using a slightly edited version of this code, I am now successfully able to create a basic row style view that has the title link to the node only when the specified CCK field is not empty.
Been pulling my hair out all last night and today, however, trying to get this to work with my specific view type... While I have learned a lot about Views/Theming today, after hours and hours on this I am still unable to get this to work just right (was really hoping I could avoid posting again). It is all admittedly a bit over my head...
My specific view is set to Style -> Table... no matter how much I try to play with this snippet in various views tpl.php, I cannot get this to work when using a Table style view.
Just as a starting point (so I don't spend the rest of the week barking up the wrong tree)
1) Will the provided snippet still be roughly accurate when I am trying to theme a specific field when the Style is set to "table" ? Or is the " (!empty($fields['body']->content))" method only relevant when editing a "row style" template (clearly my ignorance is showing)?
2) As I am after the "Title" field, will I want to be editing a base tpl.php that is for "Style output" or the "Field Node: Title (ID: title)" ?
My guess had been that I would want to add a basic if statement in a template that overrides Field Node: Title (ID: title)" (my "title" field), as this is the only template I had edited in the past... I am starting to worry, however, that I am grasping this less than anticipated.
Thanks again for all the help/patience here!
Comment #6
epicsauce commentedComment #7
mcrittenden commented1) No, that part will be relevant no matter which style you choose.
2) I think you're better off editing the style output's tpl.php although you could probably get it to work either way.
How far have you gotten? Do you have a style output for the table style for that view that is getting picked up? That's the most important step.
Comment #8
epicsauce commentedI do have the style output setup (views-view-table--VIEWNAME.tpl.php) and have confirmed that it is being picked up.
I also have confirmed that the code snippet provided works as expected when placing it towards the top of the 'fields' output when using that style, so know I am on the right track.
I cannot, however, figure out where to place the snippet in the template (views-view-table--VIEWNAME-linktest.tpl.php), or what else I may need to add in this instance to get it all to work.
Comment #9
epicsauce commentedStill struggling with this one -- let me know if anyone has an idea.
Thanks!
Comment #10
Anonymous (not verified) commentedCan you post an export of your view and the code you have in any .tpl.php files?
Comment #11
epicsauce commentedDug into this a bit more, and was able to resolve the issue. If it is useful for anyone down the road, here is a quick summary...
As this was a Table style in question, I created the file views-view-table--YOURVIEW--page.tpl.php and added the defaults...
With the default, all content is spit out together:
As we want to target a specific field, we need to print each row individually... trick was to remove "print $content" and print the desired rows out one by one:
You will obviously need to add css to the rows to keep the tables, but should be pretty straightforward.
Not sure if this is super DB heavy or not -- any insight would be appreciated.
Thanks again for the help!
Comment #12
epicsauce commentedComment #14
mkeith commentedI’ve hit a wall on this same issue. I’ll try to set the scene:
We have a listing of 180 top businesses in in the area. All of these businesses have a "profile" page that were created by using a specific content type. Some of the businesses paid a extra and have "advanced" profiles which include extra text, photos, video, etc. Everything is of the same content type. I created a basic view that lists the Company Name, Location, CEO and Membership Level in a table format. Here’s comes the catch,
I want to turn the Company Name text into a link to it's node in the listing ONLY IF the company has purchased an "advanced" profile. I've tried to override the view template for the title field using views-view-field--nc-power-180-listing--title.tpl.php. I've tried both coding solutions listed above and cannot get either to work. The CCK field I'm using as my "if" field is "field_about_web." The "advanced" profiles are the only ones that have a value it that field. I think I'm getting hung up on the variables available for the template I'm trying to override. The above examples use variables that the default code says is not available. When viewing the default template for Field Node: Title (ID: title), the only variables I can use are:
$view
$field
$row
$output
I've attached an export of my view for anyone that would like to help. I'm by no means a PHP master, so any help, if any, would be amazing.
Comment #15
dawehnerThen have a look at $row and $view.
There is everything you need.
Please don't hijack old issues.
Comment #16
mkeith commentedsorry dereine. i'm fairly new to the forums and have had a hard time figuring out the etiquette around here and where in the world to post my problems. i've reposted here (http://drupal.org/node/553586).
can you give me any more direction on $row and $view? i'm not exactly sure where to star here.
Comment #17
esmerel commentedOriginal issue fixed, should be reviewed for possible inclusion in documentation
Comment #18
esmerel commentedComment #19
vinoth.3v commentedThis is How I did that
1. Add 2 title fields one with link and one with no link, and make both them exclude from display.
2. Add body field,
3. In No result behavior put title with no link replacement token to it.
4. In Rewrite results behavior put title with link replacement token to it.
5. Tick hide if empty.
Comment #20
knalstaaf commented#19: clever approach! Works perfect.
Depending on the circumstances (use of relations, path field) some fields may not pull off this behaviour properly. In these cases it may be useful to use an extra (rewritten)
Node: linkfield on top of it, to get the hide if no results behaviour going. This way the approach described in #19 should work out after all. The rewritten field doesn't necessarely has to have the hide if empty option ticked in this case.Comment #21
mrpauldriver commentedConfirm that the approach mentioned in #19 works just as well for Drupal 8.
Thank you @vinoth.3v