Views module doesn't call hook_node_view() when set to display fields 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:


override Title output while NOT setting my view to display Fields, or
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(&amp;$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-&gt;result-&gt;_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(&amp;$view)
{
    if($view-&gt;name == 'taxonomy_term')
    {
        foreach($view-&gt;result as $i =&gt; $value)
        {
            $node = &amp;$value-&gt;_field_data['nid']['entity']; //it's a real node, yes.
            if($node-&gt;type == 'MYARTICLETYPE') //override titles only for this type
            {
                $node-&gt;title = $node-&gt;field_sort_key['und'][0]['value'];
            }
        }
    }
}
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.