Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am using Views PHP to rewrite fields in Views. I can access all variables BUT those that have a build mode. In my case, I have Content: Node field (which can be build according to any display), which should is accessible using the variable $row->nodefield. However, the content of that variable is always empty. I have tried with other variables, and every field with a build mode will not be rendered in Views PHP .

enter image description here

Is this a design issue or am I missing something?

I'm using Drupal 7 and Views 3

EDIT

Using dpm() on available values return the following:

dpm($row)

enter image description here

dpm($data)

enter image description here

share|improve this question

4 Answers 4

Above the "Output Code" section you should check the $data variable in the "Value" field.

Usually I just dpm($data) to find the field I need. Then you can save it to the $static['myvalue'] variable for use in the "Output Code" field.

It is confusing but for some reason if you try just the $row->xxx variables in the "Output Code" section they are empty or set to the node-id.

share|improve this answer
    
Thank you. I am new to this, so could you please help me further: when I copy dpm($data) in the value field, Views returns a "call to undefined function dpm()" error" –  NathanZ May 21 '12 at 13:27
    
OK. DPM is a devel function, so it will not work if you do not have the devel module installed. Either install that or just use print_r($data). –  Chance G May 21 '12 at 16:41
    
I did not find any relevant content. (see my edit) All fields seem null in the $row object. I have tried other variables such as $view or $handler with no success. The token [nodefield] is available in Rewrite Results though, so it should be somewhere. Any other idea? –  NathanZ May 21 '12 at 20:05

The Available Variables $row items in Views are kind of useless for getting field values. But you don't need to use noad_load to get the data you need. First, as usual, hide the output of the field you want to rewrite, then in the Output Code field of your PHP field you can use $data like so:

<?php echo check_markup($data->field_field_body[0]['rendered']['#markup'], 'nitf_body'); ?>

In this case I'm stripping disallowed tags and attributed from content for use in an NITF export. If you're not sure what's inside of $data, you can of course print_r($data) to have a look at it.

share|improve this answer
    
what exactly do you mean by: "First, as usual, hide the output of the field you want to rewrite" ? Are you reffering to "Exclude from display" checkbox on the field we want to rewrite? –  Antonio Nov 4 '13 at 21:21

Additional tip: The $data array indeed contain the raw node data and can be printed. But to render it correctly we need to process the fields with functions like field_view_field.

share|improve this answer

Are you trying to do this inside views? Why not just theme the views template itself instead of using the PHP global? I'm not sure if print_r will work inside the view field. If it were me, I would just click theme-->information, you'll see something like views-view-field--MyViewName.tpl.php

(edit, clarification) Under the views module theme folder, copy "views-view-field.tpl.php" then rename it to "views-view-field--MyViewName.tpl.php" and put it under your /templates folder in your site them. In the file, you'll see something like

<?php print $output; ?> 

You can delete that and add all your fields manually like

<div class="MyPrettyTitle"><?php print $row->_field_data['nid']['entity']->title; ?></div> 
<div class="MyNodeBody">
<?php print $row->_field_data['nid']['entity']->body['und'][0]['value'];?>
</div>

etc. Or wrap the $output with whatever you want.

Also, I've never had much luck with DEVEL, I always just use:

<?php print var_dump($row);?> 

Then looking at the source of the page to get the variables. Maybe I'm just using DEVEL wrong, but I find I can get the variables for each row much easier with a regular old PHP var_dump.

share|improve this answer
    
This gives duplicate rows! –  user219492 Sep 24 at 11:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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