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

silverflame757’s picture

Code is neater here:

<?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)){
     ?><strong><?php print '<a href="' . $website . '">' . $title . '</a>'; ?></strong><?php
}else {
     ?><strong><? print $title; ?></strong><?php
}
merlinofchaos’s picture

Status: Active » Fixed

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

Status: Fixed » Closed (fixed)

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

kharbat’s picture

Status: Closed (fixed) » Needs review

i'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.

merlinofchaos’s picture

Status: Needs review » Fixed

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

Status: Fixed » Closed (fixed)

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

johnv’s picture

I had the same problem using views-data. Just for reference, I add my working code here:

foreach ($records as $row_index => $row) {
  foreach ($this->view->field as $key => $field) {
    $object_value = $field->get_value($row);
    $object_value = isset($object_value[0]['value']) ? $object_value[0]['value'] : 0 ;  // this would be nicer in 1 line.

    ... use $object_value...
  }
}
my-family’s picture

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

dawehner’s picture

Use $data = $view->field['FIELD_ID']->get_value($view->result[view->row_index]);

bpeicher’s picture

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

Nikdilis’s picture

Version: 7.x-3.x-dev » 7.x-3.5
Status: Closed (fixed) » Active

See #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...

<a href="[field_url]"><div class="db_entry">
 VIEWS ROW OUTPUT with a bunch of fields
</div></a>

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)

Nikdilis’s picture

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

  <div class="db_entry">
     <div class="description">[field_description]</div>
     <div class="title">[title]</div>
  </div>

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

cbrasfield’s picture

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

foreach($view->result as $key=>$result) { print$view->style_plugin->rendered_fields[$key][$field_id]; }

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.

ehalber’s picture

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

apmsooner’s picture

Issue summary: View changes

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

 * When fetching output from the $row, this construct should be used:
 * $data = $row->{$field->field_alias}

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.

theabacus’s picture

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

MustangGB’s picture

Status: Active » Closed (outdated)

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