Overriding Drupal7 node display with Views
Back in the Drupal6 days overriding node displays with a View was a pretty common thing for me but since using Drupal7 most of the time it is not necessary. That time has finally come, and I did a bunch of searching around for a better/automatic way but I did not find anything that did not involve over-engineering so I did it in my theme. First, create a view, as a block (to not to have extra pages floating around).
- Set the title to %1 to set your view title the same as the node
- Do not use a pager
- Display only one item
- Add a Contextual Filter using the NID field, set the fallback behaviour to "hide" Then, override the node.tpl.php file in your theme. If you do not have this file in your theme, find out which theme your custom theme is based on, and copy over the relevant file to the correct location for your theme. In your node.tpl.php file, look for the following code:
print render($content); ...and replace it with a view_embed_view.print views_embed_view('node','block_1', $node->nid); In this example, the first parameter, node, is the machine name for my view, and block_1 is the first block in the view. When you are editing your view you will probably see both of these parameters in your URL. Finally, the last parameter is the node ID. This last parameter will probably be the exact same that you will use unless you are doing something truly crazy (like showing the wrong node on purpose? har har). I only wanted to apply the view to nodes which use the 'article' content type so I combined the old behaviour and the new behaviour with a test for the content type:if ($node->type == 'article') { print views_embed_view('node','block_1', $node->nid); } else { print render($content); } There you have it! Now you can use Views to render your fields for a specific node type... and using Views you can customize your HTML as much as you like using the awesomeness of Views without the overhead of Panels or Display Suite.
Fund writing like this (and consent to cookies) 🍪 ☕
Send a comment 💬
© 2006-2024 Ryan Weal. Created with JavaScript using Svetle.