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!

CommentFileSizeAuthor
#14 view-export.txt6.09 KBmkeith
#3 Empty_Body_View_Export.txt3.63 KBAnonymous (not verified)

Comments

Anonymous’s picture

I 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!

epicsauce’s picture

Thanks 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!

Anonymous’s picture

StatusFileSize
new3.63 KB

I'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:

<?php if (!empty($fields['body']->content)) {
  // If the body is not empty, print the title as a link to the node
  print l($fields['title']->content, 'node/'. $fields['nid']->content);
} else {
  // Otherwise just print the title (not a link)
  print $fields['title']->content;
} ?>

This seems to give the output you're looking for.

mcrittenden’s picture

Status: Active » Fixed

For 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.

epicsauce’s picture

Thanks 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!

epicsauce’s picture

Status: Fixed » Active
mcrittenden’s picture

Priority: Minor » Normal

1) 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.

epicsauce’s picture

I 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.

epicsauce’s picture

Still struggling with this one -- let me know if anyone has an idea.

Thanks!

Anonymous’s picture

Can you post an export of your view and the code you have in any .tpl.php files?

epicsauce’s picture

Dug 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:

<?php print $content; ?>

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:

 // see if the field in question is empty
<?php if (empty($row['MYFIELD_with_potential_content'])) {
 // if field does not have content, print only the title
print $row['title'];
} else {
 // if field has content, print title as link
print l($row['title'], 'your_url/'.$row['nid'] );} 
?> 

//then print the rest of the fields 

<?php  print $row['MYFIELD_1'];?>   
<?php  print $row['MYFIELD_2'];?>
<?php  print $row['MYFIELD_3'];?> 

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!

epicsauce’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

mkeith’s picture

Category: support » task
Status: Closed (fixed) » Active
StatusFileSize
new6.09 KB

I’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.

dawehner’s picture

Category: task » support

Then have a look at $row and $view.

There is everything you need.

Please don't hijack old issues.

mkeith’s picture

sorry 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.

esmerel’s picture

Assigned: Unassigned » esmerel

Original issue fixed, should be reviewed for possible inclusion in documentation

esmerel’s picture

Status: Active » Closed (fixed)
vinoth.3v’s picture

Version: 6.x-2.6 » 7.x-3.x-dev
Assigned: esmerel » Unassigned

This 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.

knalstaaf’s picture

Issue summary: View changes

#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: link field 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.

mrpauldriver’s picture

Version: 7.x-3.x-dev » 8.x-3.x-dev

Confirm that the approach mentioned in #19 works just as well for Drupal 8.

Thank you @vinoth.3v