I want to change the output of a view. More precisely i want to reduce the resulting list to exact 3 matches: one specific row and its previous and next row.

As far as I understand I need to generate the whole list first, because otherwise i cant find the previous row. So I thought I have to do something in the views-view.tpl where all the rows will printed.

My question then is: How can I get the content of a specific field in each row in $rows so i can check for that specific row and its surrounding rows?

Or is there any better way?

ps. ( i am a php noob )


A custom module which implements hook_views_pre_render() will give your that ability:

MYMODULE_views_pre_render(&$view) {
  if ($view->name=='myview') {
    // limit the results to 3
    array_splice($view->result, 3);

    //other modifications can be done as well:
    foreach($view->result as $r => $result) {
      // do something

to find a specific row you'll have to code a bit, but that's not drupal related, check https://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php for further informations.


Try to avoid under all circumstances to iterate over an unknown result set to find three unspecific items.

By this this is not really a question about Drupal and Views but about SQL Query Performance.

It maybe sound like a good idea to select one item by ID and than the one "before" and the "next" one but for SQL and growing databases this is a very bad idea. Now you created a view that always selects a list of unknown and probably growing size. This might kill your system in the future.

Please change the query and by this the view to something you can order. Maybe the creation time and than just 3 items. By this you have a much more reliable query that gets you three items from your database and hopefully can use an index to find them.

  • the thing is, my view is ordered by a draggableview weight. i tried a lot, but cant find a solution with the views UI to give the rows a good value to filter for that 3 rows. – guest123 Mar 27 '15 at 13:29
  • But you have an order than. The draggable views weight is fine. And than you don't have an every growing list of nodes. So what you do seems much more useful than what it sounded like before. And there is an index to the weight column cgit.drupalcode.org/draggableviews/tree/draggableviews.install. Than maybe you could write a views filter plugin that uses a weight context - 1 and select 3 items. – Kars-T Mar 27 '15 at 14:34

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.