0

I want to edit my View so that it only displays a certain contents if some field in my content type had been set. I installed the Views PHP plugin, but it's currently not producing the desired results.

This is the current example I have - using the custom php field, where field_link_to_page is a checkbox field in my content type that decides what contents are to be displayed:

<?php
// Field is configured to return "1" if it's checked, or "0" if it's unchecked.
if ($row->field_link_to_page =="1") {
    echo $row->body."<a href=\"node/"

?>
[NID]
<?php
echo "\">more...</a>";
}
else
{
    echo $row->body;
}
?>

Removing this code, and doing a var_dump() on the field, returns something like this:

object(stdClass)#250 (7) { ["field_year"]=> string(3) "113" ["field_month"]=>
string(3) "113" ["field_day"]=> string(3) "113" ["title"]=> string(73) 
"Forecast opening day for Talking Rock Golf Course (conditions permitting)" 
["body"]=> string(3) "113" ["field_link_to_page"]=> string(3) "113" ["php"]=> 
NULL } 113

which is confusing because the contents get rendered nicely by Views but here it only looks like it's handles to something (113). How can I render the contents correctly in php?

  • You may be getting 113 because of the way your code snippet is written. You should probably take out the [NID] and see what happens - sort of like I suggest doing in my response. 113 is just probably your node id being printed out on each field - repeatedly. – amateur barista Jan 6 '14 at 14:49
2

I have figured out a way to do what I want without the Views PHP module: I added the link to my fields as well and rewrite the link token in my checkbox field (field_link_to_page) only if it isn't empty. That seems to have worked out well.

  • Hmm. Still doesn't really answer your question - only provide an alternative. Somebody else trying to do what you were doing with the Views plugin might run into this, and become frustrated because they don't really have an answer. It might be good for you to try it out with the Views plugin anyways - or at least improve your answer with links to and examples. Cheers. – amateur barista Jan 6 '14 at 14:51
1

Views has an option to see the value 0 as an empty result. And you can disable rewriting for empty results (thus 0 value too).

Then you can use a simple rewrite like [body]<a href="node/[nid]">more</a>.

I do think you don't need php here to accomplish what you want.

0

The second line of code in your example has a typo, you're missing a semicolon. Also, this number you're doing here feels squishy:

<?php 
// code ... 
?>[NID]<?php 
// code ...
?>

Is that even working? Wouldn't it be better to just var_dump() the whole $row object, find where the node nid for the current row is present, and create a link using l()?

You should always use the Drupal API when creating links, unless for a very specific reason.

  • I did a var_dump() but it gives me something like what I've posted above under This code outputs something like – stdcerr Jan 6 '14 at 14:37
  • I edited the answer to reflect that, it a little bit unclear before ;) – amateur barista Jan 6 '14 at 14:43

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.