8

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

6

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.

  • 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" – znat 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? – znat May 21 '12 at 20:05
3

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.

  • 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
  • Yes. You use "Exclude from Display" whenever you want to get the value of a field and use it in a subsequent custom field. – Scott Lahteine Oct 4 '14 at 4:32
  • 1
    You can use $data->field_field_body[0]['raw']['value'] to get the raw value. – user2534880 Apr 9 '15 at 8:15
1

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.

1

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.

  • This gives duplicate rows! – user219492 Sep 24 '14 at 11:32
0

I spent two days to find the solution. The easiest way is update the "Views Php Module" to 7.x-2.x-dev. The Maintainer fixed this problem in their 2.x version.

You can download from here (Make sure you download 7.x-2.x-dev): https://www.drupal.org/project/views_php

Hope can help someone.

0

You should NEVER store PHP code in your database (and with View PHP you will). Use a Views hook instead to modify the results.

  • Hi @ssibal, and thanks for contributing. Can you explain why this should be avoided? It would greatly increase the quality of your post. – Neograph734 Jun 8 '16 at 20:38

protected by kiamlaluno Oct 24 '17 at 9:16

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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