I am trying to access excluded fields in Views (they're added as a field, and then set to 'Exclude From Display') through a tpl.php file

I could get to them before by $fields['field_name]->content

But once they are excluded these variables aren't created.

Any help much appreciated.

Comments

stevenc’s picture

By using "Exclude from Display" these values are not passed into the theme layer.

If you are editing the TPL file anyway, you can let the field be displayed (NOT check "Exclude from Display") and then simply not include the variable in your TPL file output.

---------------------------------
Steven Wright

Slalom

leon kessler’s picture

Are you 100% about this?
On Views it says: Check this box to not display this field, but still load it in the view. Use this option to not show a grouping field in each record, or when doing advanced theming.

I found the variables anyway (keeping them excluded), they were under $view->result

I guess either way works so it doesnt really matter.

stevenc’s picture

No, I'm glad to hear (and know for my own benefit) how to get at the excluded values. I knew that the View UI could reference them (via tokens and so forth) but I had never tried to manually theme a view with excluded values.

Thanks for writing back with your solution!

---------------------------------
Steven Wright

Slalom

daniel-san’s picture

I wanted to include some documentation to this conversation since I just figured out how to accomplish this and hoping that some additional information will help others.
**NOTE: Huge thanks goes to lowtrak for helping me with the php code and the if statement. I researched for hours on how to accomplish this and he saved me by showing me how to do it.

My scenario:
I needed a table listing of vendors for a conference with basic information: title, Name of representative, and contact email. That was simple enough to create with cck fields and Views, of course, but we needed one more bit of information added to the content type. We added a cck checkbox to the content type that allowed us to select which vendors were "Title Sponsors" of the event using the word 'Yes'. The request was to have those few Title Sponsors stand out visually in the table, but not have the Title Sponsors column in the table. I saw the best way to do this was to add a class to the table row if that row held a title sponsor.

Process
Create a basic node view, add all the fields that should be displayed in the table view, add a Page display and have it use Table as the format.

Here were my fields for this view:
Title Sponsor
Title
Representative
Rep email

I then selected the Title Sponsor field and checked the Exclude box. This will call the field to the view, but not display it in the table.

I created a new template file for the table to include in my site's theme folder. The name of the template file was:

views-view-table--vendors.tpl.php

This was a duplicate of the views-view-table.tpl.php file found in the Views module theme folder. I copied that file into my theme folder, then duplicated and renamed it. Then, go into the view for this table, select the Theme information link and rescan for the file. Views will see the file and begin using it in displaying the page view.

Now add and if statement that will check to see if the node has a Yes for the Title Sponsor field. This is the code in the table body of the tpl file with the if statement added in for the table row.

<tbody>
    <?php foreach ($rows as $count => $row):
       if ($view->result[$count]->node_data_field_title_sponsor_field_title_sponsor_value == 'Yes') :?>
 		<tr class="title-sponsor <?php print implode(' ', $row_classes[$count]); ?>">
       <?php else: ?>
          <tr class="<?php print implode(' ', $row_classes[$count]); ?>">
       <?php endif; ?>
       <?php foreach ($row as $field => $content): ?>
          <td class="views-field views-field-<?php print $fields[$field]; ?>">
            <?php print $content; ?>
          </td>
        <?php endforeach; ?>
        </tr>
     <?php endforeach; ?>
  </tbody>

If you are going to do this, you need to change:
node_data_field_title_sponsor_field_title_sponsor_value

to include your own field name that is being excluded in the view:
node_data_field_FIELD_NAME_field_FIELD_NAME_value

kepford’s picture

Thank you for taking the time to post this, it was super helpful Dan & Lowtrak.

Bob Kepford
TheWeeklyDrop - A Weekly Drupal Newsletter

transmitter’s picture

Great!

Worked like a charm.

I went with the unformatted, so for me it was:

<?php foreach ($rows as $id => $row): 
       if ($view->result[$id]->node_data_field_foto_panorama_field_premium_value == 1) :?>
  <div class="<?php print $classes[$id]; ?> premium">
       <?php else: ?>
  <div class="<?php print $classes[$id]; ?>">
       <?php endif;  ?>
   <?php print $row; ?>

And:
views-view-unformatted--Viewname.tpl.php

Bye, Transmitter

Reager’s picture

It doesnt work in drupal 7 how to access to the field
this is my
echo "

";
print_r($view->result[$id]) ;
echo "

";

stdClass Object
(
[nid] => 13
[node_title] => sdfsdf sdf
[node_language] => ar
[node_created] => 1365172965
[field_data_field_important_node_entity_type] => node
[_field_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[vid] => 13
[uid] => 1
[title] => sdfsdf sdf
[log] =>
[status] => 1
[comment] => 0
[promote] => 0
[sticky] => 0
[nid] => 13
[type] => communiques
[language] => en
[created] => 1365172965
[changed] => 1369321525
[tnid] => 0
[translate] => 0
[revision_timestamp] => 1369321525
[revision_uid] => 1
[body] => Array
(
)

[field_fichier] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 15
[display] => 1
[description] =>
[uid] => 1
[filename] => 57.pdf
[uri] => public://communiques/57l.pdf
[filemime] => application/pdf
[filesize] => 144992
[status] => 1
[timestamp] => 1365172965
[type] => default
[focus_rect] =>
[crop_rect] =>
[rdf_mapping] => Array
(
)

)

)

)

[field_important] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 1
)

)

)

[metatags] => Array
(
)

[rdf_mapping] => Array
(
[rdftype] => Array
(
[0] => sioc:Item
[1] => foaf:Document
)

[title] => Array
(
[predicates] => Array
(
[0] => dc:title
)

)

[created] => Array
(
[predicates] => Array
(
[0] => dc:date
[1] => dc:created
)

[datatype] => xsd:dateTime
[callback] => date_iso8601
)

[changed] => Array
(
[predicates] => Array
(
[0] => dc:modified
)

[datatype] => xsd:dateTime
[callback] => date_iso8601
)

[body] => Array
(
[predicates] => Array
(
[0] => content:encoded
)

)

[uid] => Array
(
[predicates] => Array
(
[0] => sioc:has_creator
)

[type] => rel
)

[name] => Array
(
[predicates] => Array
(
[0] => foaf:name
)

)

[comment_count] => Array
(
[predicates] => Array
(
[0] => sioc:num_replies
)

[datatype] => xsd:integer
)

[last_activity] => Array
(
[predicates] => Array
(
[0] => sioc:last_activity_date
)

[datatype] => xsd:dateTime
[callback] => date_iso8601
)

)

[locations] => Array
(
)

[location] => Array
(
)

[name] => admin
[picture] => 0
[data] => b:0;
)

)

)

[field_field_important] => Array
(
[0] => Array
(
[rendered] => Array
(
[#markup] => 1
[#access] => 1
)

[raw] => Array
(
[value] => 1
)

)

)

)

How can i access to field_important??
i tried if ($view->result[$id]->node_data_field_important_field_important_value == 1)
but no response???
Any help are welcome

serjas’s picture

For easy selection of elements , just use print_r

echo "<pre>";
print_r($view->result[$count]) ;
echo "</pre>";