I am working in Drupal 7. I have a view that filters on 'Is one of' content type 'blog post' or 'member news story'. I need to adjust the output of the view so that when the content returned in the view is a blog post the author and the author company are shown, but when the returned content is a member news story the author and author company are hidden. My view has a relationship 'Content: Author'.
First of all I tried using Global PHP. I am using the realname module and was able to return the realname of the author by adding the following code to the output field of a Global PHP field:
<?php
if ($row->type == 'member_news_story'){
print ("Author: ");
print($row->realname);
}
?>
I would have thought that I could just as easily use the replacement pattern for my field for the authors company, but adding the following code beneath 'print($row->realname);' simply prints a number i'm guessing this is a NID.
print($row->field_guest_blogger_company_or_o);
I tried following advice like that give in questions like returning nod ID in views php and global php in view using print_r($data);
to return the available data and then trying to drill down into it to get the variable I need.
Unfortunately I was not able to return the company name. I expect this was more complex as the the company name is not in the returned node, but is a user field. After spending ages on this and getting no where I decided to try a different approach -
I thought that it might be better to work with the template.php file and create a hook for the company name views field. In template.php I added:
function amrc_new_views_view_field__news_blog_front__block__field_guest_blogger_company_or_o(&$vars) {
return "views theme view field working";
}
I could see the text 'views theme view field working' in the correct place showing the hook was working. I was able to use dpm($vars);
instead of the return statement above to return a devel/krumo display of the available variables. However I struggled to be able to print any of the variables to the screen, which I would like to be able to do before I try using them to only print the company name if the returned node is of the type 'blog post'.
To recap, my question is: When the content returned in the view is a blog post I need to show the author and the author company, but when the returned content is a member news story the author and author company are hidden. I would like to know whether using global php in views or a hook in template.php would be the best way of going about achieving this. I would also like suggestions on how to access the company name variable so I can work with it. I have been struggling with this for a long time, so your input would be really appreciated.
If required I can easily supply the output of the using print_r($data)
in the view and the dpm($vars);
in the hook, but left these out for brevity.