2

I am trying to call a field by using the line of code:

<?php print $field->handler->view->field['title_1']; ?>

But i keep getting this error:

enter image description here

I used devel dpm() to look for what was the correct path to call it and it said handler->view->field['title_1']. I am absolutely stumped and I am not sure if I am using the right .tpl.php file or what. Anyone have any ideas?

---EDIT---

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
  <?php print $field->separator; ?>
<?php endif; ?>

<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php print $field->content; ?>
<div>
    <?php print $field->handler->view->field['title_1']; ?>
</div>
<?php print $field->wrapper_suffix; ?>
  • I've found the best way to look at Drupal objects/arrays is to: echo '<pre>'; print_r($field->handler); echo '</pre>'; are you sure $field->handler contains "view" etc? the error says you're trying to convert an object to a string, but it can't do it. – jdu Feb 7 '14 at 15:53
  • You most probably are looking for $field->content here; have you looked at and read the comments in the top of the default views-view-fields.tpl.php template? – Jimajamma Feb 7 '14 at 15:59
  • @jdu - when i do handler->view->field it displays as array so i am assuming that there is does contain a view. that is without the <pre> tags around it. how would i go about converting that object to a string? – scapegoat17 Feb 7 '14 at 16:05
  • @Jimajamma - i have looked into using $field->content, but that does not have what I want to display in it. that is the whole display of <span class="field-content"><a href="/sandbox/node/6" data-thmr="thmr_11 thmr_12">Event Sample 2</a></span>. what i would like to do is grab specific fields. even ones that are not set to display normally. – scapegoat17 Feb 7 '14 at 16:07
  • 2
    @scapegoat17 I think you're jumping ahead.. the point of looking at it using print_r(); is to show you how it's formed. find out what is inside: print_r($field->handler->view->field); ... then go one level deeper, etc. do it step by step. – jdu Feb 7 '14 at 16:42
2

From the commentary, some points are being missed, but this template is used to create the "row" of fields from a View.

Let's create a simple one:

enter image description here

which has three fields, a title, a new field and a custom field, which when run looks likes this:

enter image description here

Now, let's create a views-view-fields--for-da.tpl.php, rescan for templates, and add some debugging information to the top of it:

drupal_set_message('<pre>' . print_r(array_keys($fields), TRUE) . '</pre>');

and running it again, this will give us:

enter image description here

So, now, anyplace we want inside this template, we have access to the individual field's content as:

$fields['title']->content
$fields['field_new_field']->content
$fields['field_my_custom_field']->content

so throwing them in similar drupal_set_message()s produces:

enter image description here

But now, let's say you are exluding one of these fields from display, eg:

enter image description here

then you will get this:

enter image description here

but some more digging into $row via:

drupal_set_message('<pre>' . print_r($row->field_field_my_custom_field, TRUE) . '</pre>');

will give you this:

enter image description here

or access to the content of the excluded field via:

$row->field_field_my_custom_field[0]['rendered']['#markup']

Obviously, the field names are different here than in your specific example, but I hope you can follow the logic.

Also, if you don't want to dig into $row you could always include the field in the display via the Views UI and then exclude it in this template, eg, something like this:

<?php foreach ($fields as $id => $field): ?>
  <?php if ($id != 'field_my_custom_field'): ?>
    <?php if (!empty($field->separator)): ?>
      <?php print $field->separator; ?>
    <?php endif; ?>

    <?php print $field->wrapper_prefix; ?>
      <?php print $field->label_html; ?>
      <?php print $field->content; ?>
      <div class="insert-custom-field-content-here">
        <?php print $fields['field_my_custom_field']->content; ?>
      </div>
    <?php print $field->wrapper_suffix; ?>
  <?php endif; ?>
<?php endforeach; ?>

which would print all the fields with the exception of the custom one, but would then include its content after the content of each of the other fields.

  • That is an amazing write up, but i still have some questions about $row->field_field_my_custom_field[0]['rendered']['#markup']... the [0] portion - wat is being accomplished there? also the same thing for ['#markup']? Again i am sorry to do this, i am just trying to understand better – scapegoat17 Feb 7 '14 at 18:04
  • the [0] is the delta, it'll be 0 for single value fields, but will be any number of values if you have a multiple value field. – Jimajamma Feb 7 '14 at 18:06
  • the #markup is the rendered html output of the field. – Jimajamma Feb 7 '14 at 18:06
  • if you have only one field excluded from the View, it may be easier to understand if you exclude it via the template, see my addition to the answer. – Jimajamma Feb 7 '14 at 18:09
  • So you are saying that in your new edit, that if i want to add another field with a tag around it the way to do it would be to set it to display, and then in the template, set it up how you have it. And by doing that, it will take it out of field content and put it in the div tags that wrap it? Also you wrote <?php print $fields['field_my_custom_field']->content; ?> did you mean $field instead of $fields? – scapegoat17 Feb 7 '14 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.