0

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.

1

Controlling the rendering of information at the views level is preferable. If you control it at template.php it's just one step before the information is going to be displayed to the end user. So, it will be an overload. Just try to render the $data object, you can easily get the required information. Yes, $row has issue of returning nid in D7.

Hope it helps

  • This was helpful. It is logical that controlling via views is better than waiting until it hits template PHP. Thanks for your input djs! – Paul Trotter Jul 12 '13 at 10:53
0

To solve this first i used dpm($data); in the output field of the Global PHP. This gave me the data value through devel/krumo (unsure that the devel module is installed for this). Interestingly this gave me a different name to reference the field with than when I did print_r($data);

Using print_r($data); suggested i needed field_guest_blogger_company_or_o

but dpm($data); indicated that i needed to use field_field_guest_blogger_company_or_o

So the working Global PHP to only print the author and company name when the content type returned is a blog post (article) was:

    <?php
    /* dpm($data); */

    if ($row->type == 'article'){
         print ("Author: ");
         print($row->realname);

         if (isset($data->field_field_guest_blogger_company_or_o[0]['raw']['value'])) {
            print (", ");
            print($data->field_field_guest_blogger_company_or_o[0]['raw']['value']);
         } 
    }
    ?>

Uncomment the dpm($data); to see all the available variables. Note you won't have the realname field if you aren't using the realname module. For the values to be available to this code you will have to include them in your view fields and then hide them if necessary.

The above code uses an isset to check that there is a company name before printing the comma after the author name.

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.