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):
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'];
}
}
}
}