1

I use Drupal 7 with Views and want to override some node field output in a view (namely change its Title to something I want). To accomplish that I set the view to display Fields and not Context or Rendered Entity and select Title and Body from the list of fields. And here is where the problem lies:

I have a (homemade) module that makes use of hook_node_view() hook to modify Body and Teaser text. When I display article body as a Field in the Views this hook for some strange reason is not called (on Context and Rendered Entity all works correctly). So I need a way to either:

  1. override Title output while NOT setting my view to display Fields, or
  2. find a way to call hook_node_view() while setting my view to display Fields.

Is there a way to do it?

Upd: provided a screenshot showing that hook_node_view() is not called on teaser when setting Show to Fields (left pane) but called when setting Show to Content (right pane):

screenshot showing that hook_node_view is called on teaser when setting Show to Content and is not called when setting Show to Fields http://ari-linn.nightmail.ru/views_and_hook_node_view.jpg

Upd 2: Okay, I did some Drupal colonoscopy and it even works though I can't say whether it is the right way to do things (probably not). I found out that when you render context Drupal apparently thinks you want to render a normal node but when you render fields Drupal is at a loss and doesn't think you're dealing with nodes anymore. Thus it doesn't fire any node hooks including hook_node_view(). If anyone still needs to run their functionality of hook_node_view($node, $view_mode, $langcode) they can use hook_views_pre_render(&$view) though access to node $view_mode and $langcode will be gone.

In case people out there are interested how I override node titles in taxonomy views, here is the scenario. My task was to override node Title with the value of another field, SortKey, and do it only in taxonomy views. I'm happy with "Britney Spears" node title at the main page but when it comes to taxonomy I want her to display "Spears, Britney".

I personally thought it to be tiresome and bothersome to deal with fields (they mess up all my CSS) and found a hack in Views 7.x-3.3: if you set Show parameter in your view to Fields and select Material:Rendered Material AND any other field (you can exclude it from display, just make sure Rendered Material is not the only field you load) you'll get a neat and tidy Node object at $view->result->_field_data. I fail to explain how it is so but it's there, and you can bend it to your needs, and what is even better Drupal will think you're rendering a node and fire all node hooks on it. I pray though that Views coders won't remove this wonderful hack in later versions ruining my code.

Here is the code:

/*
* Changes MYARTICLETYPE title to sortkey on taxonomy views pre-render
*/
function MYMODULE_views_pre_render(&$view)
{
    if($view->name == 'taxonomy_term')
    {
        foreach($view->result as $i => $value)
        {
            $node = &$value->_field_data['nid']['entity']; //it's a real node, yes.
            if($node->type == 'MYARTICLETYPE') //override titles only for this type
            {
                $node->title = $node->field_sort_key['und'][0]['value'];
            }
        }
    }
}
1

The title of your question is.

Views module doesn't call hook_node_view() when set to display fields

That is working as intended. The reason is that it's not a node that's being rendered, but just a bunch of fields and attributes that belong to a node that is being rendered.

One way of fixing hook_node_view not being called, would be to actually render the nodes.

If the problem is that the field is not rendered as you which, the best way of dealing with that, would be to alter the rendering of the fields, instead of trying to make Drupal act like you're rendering a node when in fact you're not.

The easiest, cleanest and most flexible way to do that, would be to create some custom field formatters for the fields in question. Field formatters are like theme functions for the fields, and you can in views select which field formatter to use when you want to render a field. It requires some work, but gives great flexibility. That would make your hook_node_view obsolete as you can use field formatters when fields are rendered under the manage display tab in the field settings interface.

-1

in view you can overide field output (settings from view) and to call hook_node_view you have to display teaser of full content or any other display of the node

to override the field you can check this image

enter image description here

  • I tried really hard to understand your answer but failed. This is the very nature of my question: I display node as "Summary or trimmed" in Views and it DOESN'T call hook_node_view so I wonder how to deal with it. I've updated my initial post to provide a screenshot. Maybe it will be more comprehensible this way in case I messed up with my English or said something unintelligible. – Ari Linn Apr 20 '12 at 19:53
  • you don't seem to understand my question. I'm not asking how to override a field. I'm asking why hook_node_view() stops being called on node Body/Teaser when I select it to be displayed in my view among other fields. – Ari Linn Apr 20 '12 at 21:36
  • If you choose field Drupal will make a select statement and render the output of it (body is a field) but If you choose content drupal it will use render(node_view($node,'teaser')) so it will fire hook_node_view – Ahmed Apr 20 '12 at 22:03
  • Understood. Can I use any other hook (and what hook exactly) that fires on rendering fields to make my functionality run when hook_node_view is not fired? – Ari Linn Apr 20 '12 at 22:19
  • I didn't make something like that but I think these hook can help you function hook_views_pre_execute(&$view) { //print_r($view); } function hook_views_pre_render(&$view) { //print_r($view); } – Ahmed Apr 20 '12 at 22:23

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.