In views--view-field.tpl.php I want to print the content of a field a certain way if the content of two other fields exist or equal a certain value. However the FIELD_ID's I've tried don't work. The only one that does is 'title' and all others end up printing the value of the nid field.
I've tried:
$row->{$view->field['FIELD_ID']->field_alias};
$view->result->{$view->field['FIELD_ID']->field_alias};
For FIELD_ID I use the machine name of the field: title, field_website, field_package etc.
Also $row->{$field->field_alias} doesn't the nid instead of the raw value of the field.
For an example of what I'm trying to do:
In views--view-field--field-name.tpl.php:
$website = $view->result->{$view->field['field_website']->field_alias};
$title= $view->result->{$view->field['title']->field_alias};
$package= $view->result->{$view->field['package']->field_alias};
if ($package == 'featured' && !empty($website)){
?> print '<a href="' . $website . '">' . $title . '</a>';
}else {
<? print $title; ?><?php
}
Comments
Comment #1
silverflame757 CreditAttribution: silverflame757 commentedCode is neater here:
Comment #2
merlinofchaos CreditAttribution: merlinofchaos commentedTry not to reference $view->result -- your code as printed has XSS security holes if any of that data is user input by untrusted users.
I recommend $view->style_plugin->rendered_fields[$row_index][$field_id] instead.
Comment #4
kharbat CreditAttribution: kharbat commentedi'm having the same problem here..
the code below always refers to node nid
$row->{$view->field['FIELD_ID']->field_alias};
Is there any other workarounds ?! i suggest we add the ability to set aliases or a machine name just like the administrative title feature in views.
Comment #5
merlinofchaos CreditAttribution: merlinofchaos commentedIn the case of Field API, we are not allowed to read the actual field api data in the query. Therefore, the field alias in the query will always point ot the entity ID. That is how field api was designed, and views has no choice but to conform to it.
If you use $view->field[$field_id]->get_value($row_index) you will be able to get the actual value, which is going to vary in type form field to field.
Sidenote: Please read the "Status" values documentation in the issue queue handbook. "Needs review" is reserved for patches.
Comment #7
johnvI had the same problem using views-data. Just for reference, I add my working code here:
Comment #8
my-family CreditAttribution: my-family commented#5 - merlinofchaos, could you explain it a little bit more in-detail, please?
If I use
$data = $view->field['FIELD_ID']->get_value($row_index);
dpm($data);
>> I obtain:
"Notice: Undefined variable: row_index..." and NO data.
I tried also
$data = $view->field['FIELD_ID']->get_value($view->row_index);
dpm($data);
>> "Notice: Trying to get property of non-object in views_handler_field_field->get_value() ..." and NO data again.
Thank you in advance for help.
Comment #9
dawehnerUse $data = $view->field['FIELD_ID']->get_value($view->result[view->row_index]);
Comment #10
bpeicher CreditAttribution: bpeicher commentedI am not finding the information on this page to be clear. In Views 3, how do I access other field values from a field template?
I have a template called views-view-field--commerce-cart-form--default--line-item-title.tpl.php, which means I am overriding the field line_item_title. In the same view I am also displaying field_custom_1 and field_custom_2. Within my field template, how do I access those two custom fields? I need to create an IF statement based on the other field values.
How exactly can I fetch those values?
The template documentation says to do this:
$row->{$view->field['field_custom_1']->field_alias};
Like comment 4 says, that only returns the node ID.
Comment 1 says to do this:
$custom1 = $view->result->{$view->field['field_custom_1']->field_alias};
That doesn't work.
@merlinofchaos recommends this:
$view->style_plugin->rendered_fields[$row_index][$field_id]
No further explanation is given
@merlinofchaos then recommends this:
$view->field[$field_id]->get_value($row_index)
No further explanation is given
Comment 9 recommends this:
$data = $view->field['FIELD_ID']->get_value($view->result[view->row_index]);
This throws a PHP exception. No further explanation is given
Can anyone offer some easy to understand assistance? This is simple but I must be missing something...
Comment #11
Nikdilis CreditAttribution: Nikdilis commentedSee #10: I'm also having trouble with that. None of the suggested 'solutions' worked for me.
@Merlin or other Views-specialists: Can you please explain a little bit more detailed? An example is highly welcome! Thank you so much and thanks Merlin for this outstanding module!
In more detail I try to put the following html-code around each row-output...
It would be nice if I could do this in the views-settings without the need of templates. Is it possible? (Maybe you can add 2 fields - one at the beginning and one at the end - and then use the rewrite option - I have to try that! - Anyway, that would be a kind of 'dirty' way)
Comment #12
Nikdilis CreditAttribution: Nikdilis commentedRegarding to my plans in #11 I found the following approach I want to share as I considered it as very useful:
- First pick all the fields you want in the view (exclude them from display and don't link them).
- Duplicate i.e. the title-field, make it "Link this field to the original piece of content" and use the rewrite-option in which you can use replacement-tokens in combination with html-code for the fields above: i.e. it might look like that:
Views, Token and some css means so much power and it's defintely worth to invest some hours in the views module!
Thanks to:
http://drupal.org/node/595194
Comment #13
cbrasfield CreditAttribution: cbrasfield commentedI believe it all depends on what is used in the underlying foreach statements for accessing the elements.
I had success with
$view->style_plugin->rendered_fields[$row_index][$field_id]
. Code was used in a preprocess function as:Each attempt to use $view->field resulted in in the error "Notice: Trying to get property of non-object....". This was of course due to not using $view->field in the foreach statement.
Comment #14
ehalber CreditAttribution: ehalber commentedThis is how to correctly use @merlinofchaos's code. I was having the same problem as everyone else...
$view->style_plugin->rendered_fields[$view->row_index]['field_id'];
They key here is to grab the correct row_index value which is part of the $view object.
This should be clear by doing a dms($view) from the field.tpl.php template.
I hope this helps!
Comment #15
apmsooner CreditAttribution: apmsooner commentedThis is an old post but there are alot of different answers here and it seems the template comments are either outdated or not making sense at least for me....
None of the other methods seem to work for me and I just needed the raw value of the node title so I did this which finally worked:
$myTitle = $row->node_title;
You can use this function if devel module is enabled to get machine names:
dpm($row);
Not sure why there is so many variations of answers here or why the template comments instruct a way that doesn't seem to work.
Comment #16
theabacus CreditAttribution: theabacus commentedThis post needs to be updated; as apmsooner states. I've been able to accomplish this task in Drupal in previous version, however, it seems to be a dead-end street right now (probably due to lack of documentation).
$field->field_alias ALWAYS returns "nid"; regardless of the field. So ultimately, you just have to use the current field alias and pray it never changes; very poor usage, but no other option at this point. There doesn't appear to be any relevant, accurate, updated documentation on this subject anywhere.
Comment #17
MustangGB CreditAttribution: MustangGB commentedClosing this as outdated to tidy up a bit around here. If you're still having problems with the latest release please create a new issue.