Hi, I have two different content types on my site. Articles and News links. I have a view set up that displays the five most recent pieces of content on my site, lumping the Articles and News Links together in the same list. I would like to set it up so that if a user clicks on the title of an "Article", they are directed to the nodes page on my site, but when they click the title of a "News Link", they are directed off of my site to the links url.
I have a fews fields that make up my view (in this order):
- Content: Type
- Content: NewsLink URL
- Content: Link (this is the link to the nodes page)
- Content: Title

I have everything excluded except the "Content: Title" field. Because the "rewrite output" doesn't support php, I have tried a number of things. The first of which was the Views PHP module, but it doesn't work and is in early dev stages, so moving on. Now I am trying to rewrite this in my custom views-view-fields.tpl.php template file (I first tried this in a views-view-FIELD.tpl.php template file, but it wasn't working).
When I go to print the title, print fields['title']->content; it works fine and prints the title. When I go to print the news link url, print fields['field_link']->content; it does not and I get a bunch of errors.

Notice: Undefined index: field_link in include() (line 38 of [redacted]/sites/all/themes/MYTHEME/views/home_page/story 1-5/views-view-fields--home-page--ta.tpl.php).
Notice: Trying to get property of non-object in include() (line 38 of [redacted]/sites/all/themes/MYTHEME/views/home_page/story 1-5/views-view-fields--home-page--ta.tpl.php).

This is the full markup of my .tpl file and what I'm trying to do,

<?php if ($row->node_type == 'newslink'): ?>
  <a href="www.somesite.com/<?php print $fields['field_link']->content; ?>"><?php print $fields['title']->content; ?></a>
<?php else: ?>
  <a href="<?php print $fields['view_node']->content; ?>"><?php print $fields['title']->content; ?></a>
<?php endif; ?> 

Why won't this work? How else would I go about getting a custom field in here?
Please Help!! Thanks.

Comments

dawehner’s picture

Title: Im trying to do something very simple, but its proving to be very difficult. Please Help! » Cats!!!

You know the most important thing about a title is that it actually tells the reader what this is all about. So change to another also non-helpful title but slightly more fun :)

fortunately the issue itself describe more things :)
Okay let's get to the main problem: You should know how $fields looks like.

Therefore you can use the devel module and the function "dpm". So just put dpm($fields); and you will see which fields are
availible. Does this help you enough?

oobie11’s picture

Title: Cats!!! » How to have the title field link dependent on the content type (thanks dereine)

Thanks dereine, I have put the

 dpm($fields); 

function in my views-view-fields.tpl file and it is not showing anything. Its breaking the theme of my site. I have Devel installed. Do I have it in the right .tpl file? I've also tried it in the page.tpl.php file of my theme, and nothing shows up.

oobie11’s picture

Ok, so I went into my view, and un-excluded my "News Link" field, and now when I print the field,
print fields['field_link']->content;
it shows. The problem now is that the "->content" outputs the field with div wrappers and such and all I want is the value or the data inside. If I try "->raw" it just gives me the node id.

merlinofchaos’s picture

Status: Active » Fixed

If you want to get rid of the div wrappers, go into the field ocnfiguration and uncheck the options near the top to apply wrappers.

If you want to do this for all fields, go into the style configuration (uh, I think it's called Format in D7?) and uncheck the box that provides default markup to fields).

If you want the rawest of the raw data, look at $view->style_plugin->rendered_fields[$row_index][$field_id]

oobie11’s picture

Status: Fixed » Active

So, I started from scratch and came up with a working solution. There absolutely has to be a better way!! and if not this functionality needs to be added.
In my view, I have excluded all of the fields except the "Content:Title" field, and I have it set to link to its node. I created a new views-view-FIELD.tpl.php file for my title field and rewrote it like so,

<?php if ($row->node_type =='newslink'): ?>
<a href="http://www.somesite.com/<?php print $row->_field_data['nid'] ["entity"]->field_link["und"][0]["value"]; ?>"><?php print $row->node_title; ?></a>
<?php else: ?>
<?php print $output; ?>
<?php endif; ?>

This is allows me to make it so when a user clicks a title of a node in the list that is of content type "newslink", they are taken off the site to the links url. If a user clicks the title of a node that is of content type "article" (or any other content type besides newslink) they are brought to the node page on my site.
I feel like this is an incredibly hackish solution and that there has to be a better way. It would be really nice if we were able to put php if statements in the "Rewrite the output of this field" section of our views. Therefore saving all the time and frustration.
This solution is very limited. I am assuming that views does not think of the title,type and url of a node to be a field, which is why if I try and print,
print $row->_field_data['nid'] ["entity"]->title["und"][0]["value"];
or
print $row->_field_data['nid'] ["entity"]->node_url["und"][0]["value"];
it does not work.
Separately, if I try and print,
print $row->node_url;
it does not work either
Please let me know that there is a much better way to do this, I feel dirty having hacked together this solution!!

oobie11’s picture

Thanks Merlin, I went into the format config and unchecked the box for the wrappers. After printing,
print $fields['field_link']->content;
I am still getting wrappers such as "field-items""field-items-even" and so on.
Also, if I try and print
print $view->style_plugin->rendered_fields[$row_index][$field_link];
I get this error

Notice: Undefined variable: row_index in include() (line 27 of [redacted]sites/all/themes/MYTHEME/views/home_page/story 1-5/views-view-fields--home-page--ta.tpl.php).
Notice: Undefined index: in include() (line 27 of [redacted]sites/all/themes/MYTHEME/views/home_page/story 1-5/views-view-fields--home-page--ta.tpl.php).
Notice: Undefined variable: field_link in include() (line 27 of [redacted]/sites/all/themes/MYTHEME/views/home_page/story 1-5/views-view-fields--home-page--ta.tpl.php).
merlinofchaos’s picture

Status: Active » Fixed

Stuff like this is proof that Views does so much, people assume it should be able to do everything.

It would be really nice if we were able to put php if statements in the "Rewrite the output of this field" section of our views.

Embedding PHP into the database is unsafe, difficult to maintain, and we've generally decided it's a bad idea over time. We've been working to limit ways of doing this, and instead requiring you to put PHP where it belongs: In the code. PHP in the database is an attack vector.

I am assuming that views does not think of the title,type and url of a node to be a field, which is why if I try and print,

At that point you're going directly for the entity, which is fully loaded. IT's not what *Views* thinks is a field, it's the internal data structure of the entity. In your case:

$row->_field_data['nid'] ["entity"]->title

There is nothing really wrong with what you did, other than digging directly into entity data.

Also, please see my response in #4.

Finally, it seems like what you've got here is functionality that link.module more or less already provides. Not sure, though.

oobie11’s picture

Thanks Merlin, what I meant by

It would be really nice if we were able to put php if statements in the "Rewrite the output of this field" section of our views.

is that it would be cool if along with the "Available variables" there was also an option to set conditions based on other fields in the view, but if that is not safe then never mind.
Thanks for clearing things up for me :)

merlinofchaos’s picture

The class "field-item" is, I believe, added by the field template in Field API. There is a checkbox in the settings for the field in the "Style settings" fieldset named "Use field template". This should be unchecked. This is normally unchecked by default.

Status: Fixed » Closed (fixed)

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