2

I'm having a view and I've created a template file for the row style output. I'm able to access the fields using the below code:

$fields: An array of $field objects. Each one contains:
$field->content: The output of the field.
$field->raw: The raw data for the field, if it exists. This is NOT output safe.

I'm getting output for the below code:

<?php print $fields['field_some_field_name']->content; ?>

But when I'm trying to print the fields nothing is getting returned:

<?php print_r($fields); ?>

I'm also unable to check the condition for the fields:

<?php if($fields['field_some_field_name']->content == 'Test') { } ?>
  • Enable this module drupal.org/project/devel and then instead of print_r use dpm(); and don't use die or exit functions. – Rishi Kulshreshtha Aug 12 '16 at 11:56
  • Is the template loaded or not? Did you clear cache after adding the template? – Jimmy Ko Aug 12 '16 at 11:57
  • @JimmyKo Yes I cleared cache after doing that. – John Melchior Aug 12 '16 at 12:51
1

I'm also unable to check the condition for the fields:

<?php if($fields['field_some_field_name']->content == 'Test') { } ?>

That is probably because in actuality:

$fields['field_some_field_name']->content = <div>Test</div>

Therefore, <div>Test</div> is not equal to Test

So what you could do is use raw instead, as raw has no html/markup around the value.

<?php if($fields['field_some_field_name']->raw == 'Test') { } ?>

else, you could still use content with the PHP striptags() function

<?php if(striptags($fields['field_some_field_name']->content) == 'Test') { } ?>

Or else, you could still use your original <?php print $fields['field_some_field_name']->content; ?> but as long as you strip all the markup added by views as shown in my answer to "Is there a more performant alternative than overriding template files?".

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.