2

I'm displaying the latest 5 nodes (teaser view mode) in a sidebar region using views. The nodes have an image field but I only need to render that on the first node on the list.

So, since the teaser view mode does include the image, I'm trying to remove the image from all nodes apart the first upon rendering.

I've tried the following in my theme's template.php:

function foo_preprocess_views_view(&$vars) {
  if (isset($vars['view']) &&
      isset($vars['view']->style_plugin) &&
      isset($vars['view']->style_plugin->row_plugin->nodes)) {
    $first = true;
    foreach($vars['view']->style_plugin->row_plugin->nodes as $node) {
      if (!$first) {
        unset($node->field_image);
      }
      $first = false;
    }
  }
}

But the image is still being rendered. I think that this has to do with the fact that I'm trying to temporarily remove the image from the wrong datastructure which represents the node state.

I then tried using the hook_preprocess_node with something like:

unset($vars['field_image']);

But it still has no effect. The image was still there.

How is this solved? And at which layer is it solved best?

I know I could simply hide via CSS by using the .views-row-first class but I don't think this is optimal.

2 Answers 2

1

try hook_views_pre_render() and unset the field there if you dont want to show it. THen in the view configuration set the field to "hide if empty".

1

Take a look at Views Nodes Split. Module Monday: Views Nodes Split gives more information about how to use the module.

Setting up the module is simple: just turn it on, and build your View as you normally would. Then, click on the View's Format option and select the newly available "Splited Content" row style. While the grammar is awkward, the results are great: you can specify separate build modes and display styles for the first node and all subsequent nodes from a simple config screen. In fact, you can even adjust where the cutoff happens: you might want the first three nodes to display in full, for example. No other changes are needed for your View; this module's new row style handles the rest.

screenshot

5
  • 1
    @kiamlaluno I'm not a member of Lullabot. Its rude to just nab images from a site without some form of attribution. That's why I merely linked the blog post to begin with -- let a needy programmer feed their thirst of knowledge from the source I found the answer at. The whole blogpost is more meaningful than your hack-up copypaste of their content. I did link the project page url so drupal.org gets some direct traffic ......
    – tenken
    Commented Jul 5, 2012 at 15:12
  • Answers that are just a link are not considered answers, on Stack Exchange. If you link an external page, you need to provide a summary of what said in the linked page, or an excerpt.
    – avpaderno
    Commented Jul 5, 2012 at 15:41
  • Thanks. I'd like to find a programmatic solution which will only remove a single field.
    – cherouvim
    Commented Jul 5, 2012 at 15:51
  • 1
    try views_pre_render() and unset the field there if you dont want to show it. THen in the view configuration set the field to "hide if empty". That should do it .... @kiamlaluno -- ok thank you for the clarification. In the future I'll try to make my replies longer than a single link (eg include a summary).
    – tenken
    Commented Jul 5, 2012 at 19:18
  • @tenken: Thanks, using hook_views_pre_render worked. Feel free to post it as a new answer so I can accept
    – cherouvim
    Commented Aug 4, 2012 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.