6

I have a view which fetches nodes from content type A, arranges them in a grid and uses a displaysuite buildmode to render them...nothing fancy so far. Now I want to insert a specific node of content type B at a specific position in the grid...like this:

+----------+----------+
|          |          |
|  type A  |  type A  |
|          |          |
+----------+----------+
|          |          |
|  type B  |  type A  |
|          |          |
+----------+----------+
|  type A  |  type A  |
.          .          .

I'm not sure where to start. I was hoping there would be some hook, preprocess function or whatever where I get an array of node objects to insert my special node into which then gets rendered...

  • 1
    I think that you can create a View with your two content types, and in the template of the view, use the array of the items (or create a new array) to control the sort that do you want. It's an idea, so I can not give you any information about performance area. Also review this, I guess that could give an idea drupal.stackexchange.com/questions/8898/… – cigotete Aug 31 '11 at 15:46
  • Thanks for your comment! That pointed me in the right direction. – Philipp Horn Sep 1 '11 at 9:07
8

I found the function I was looking for. You can insert nodes in the results array just after the query has been executed.

This goes in the module:

function modulename_views_post_execute(&$view) {
  // wait for the right view
  if($view->name == "name_of_the_view") {
    // create the object you want to insert in the result
    $temp = new stdClass;
    // creation date of that node   
    $temp->node_created = 1314807643;
    // nodeID
    $temp->nid = 13;

    $new_result_array = array();
    $i = 0;
    foreach($view->result as $single_result) {
      if($i == 2) {
        // insert the extra node at 3rd position
        $new_result_array[] = $temp;
        $new_result_array[] = $single_result;
      } else {
        $new_result_array[] = $single_result;
      }
      $i++;
    }
    // replace the old result array
    $view->result = $new_result_array;
  }
}
| improve this answer | |
  • interesting solution! – cigotete Sep 1 '11 at 14:26
0

You can do it also using views theming feature. Place PHP file views-view-unformatted--taxonomy-term--page.tpl.php right in your theme's folder and with this code you can print custom code after every 3th row for Taxonomy term page default views:

<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
  </div>
  <?php if ($id == 2): ?>**My custom code after first row in Drupal views**<?php endif; ?>
<?php endforeach; ?>

This line does the trick

<?php if ($id == 2): ?>**My custom code after first row in Drupal views**<?php endif; ?>

Source and more details.

| improve this answer | |
0

For any latecomers to this question, there's a module that does just this Views Row Insert

It has it's limitations when you start to push it, but it would do just as OP requests with very minimal effort and no hooks needed.

| improve this answer | |

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.