1

I am in a dilly of a pickle. I have a view that returns indexed content types. The problem is that the view is returning multiple instances of the same content. All of these are indexed nodes.

I read about someone else having the same problem here: Results in view page having multiple duplicate result They say you can use hook_views_pre_render() in your template.php file to filter the extra content types out of your view.

Specifically the article said to

function YOURTHEMENAME_views_pre_render(&$view) {
  $used_nids = array();
  $new_view_result = array();

  if ($view->name == 'YOURVIEWNAME') {
    foreach ($view->result as $row) {
        if (!in_array($row->nid, $used_nids)) {
          $new_view_result[] = $row;
          $used_nids[] = $row->nid;
        }
    }

    $view->result = $new_view_result;
  }
}

This did not work for me. There was no $row->nid property. I think this is because they are using regular nodes and I am using indexed nodes.

Here is an example of my "$row printed out. (from $row above)

object(stdClass)[489]
      public 'entity' => int 4
      public '_entity_properties' => 
        array (size=2)
          'search_api_relevance' => string '1' (length=1)
          'search_api_excerpt' => string '' (length=0)

I altered this to try and filter by the property on "$row" called entity, which holds the id of the indexed item.

function BootstrapBlocks_views_pre_render(&$view) {
  $used_nids = array();
  $new_view_result = array();

  if ($view->name == 'search_the_mangroves') {
    $count = 0;
    foreach ($view->result as $row) {

        if (!in_array($row->entity, $used_nids)) {

          $new_view_result[$count] = $row;

          $used_nids[$count] = $row->entity;
          $count++;
        }
    }

    $view->result = $new_view_result;
  }
}

Indeed this does filter out the extra content. I know this because in template.php I printed it out to be sure, but when I set the view to use the new content ie: $view->result = $new_view_result; I get no content type back from the view. Any idea what might be the issue?

Please help me.

Sincerely,

David Pugh

PS: if you need any more info, let me know.

1

I solved my issue. Turns out the filter TEMPLATE_views_pre_render function I programmed in was working, the problem was in the template it's self. I had tried to solve this problem on the template before trying this issue. Before I was trying to only allow $row 1 to return, which didn't solve my problem. I then tried the _views_pre_render function, and saw that it was filtering but my view was not showing any data. That is because only one row was being returned due to the _views_pre_render function, but the only row being returned was row #0, and in the template below I was specifying to only show row 1.

So, in conclusion, if you have views with search api and find that

<div class="row-fluid container-folio">

        <div class="thumbnails">
        <?php $lastItem = 0;?>
            <?php foreach ($rows as $row_number => $columns): ?>    
                <?php if($row_number == 1):?>
                <?php $array = array();?>
                <?php foreach ($columns as $column_number => $item): ?>

                    <?php if($item != null && $item != "" && strlen($item) != $lastItem):?>
                        <div class="span3">
                            <div class="thumbnail2">                                                                
                                <?php print $item?>                                 
                            </div>                  
                        </div>
                        <?php $lastItem = strlen($item) ?>
                    <?php endif ?>


                <?php endforeach; ?>
                <?php endif ?>  
            <?php endforeach; ?>
        </div>
    </div>  

So, in conclusion, if you have views with search api and find that the solution listed here, Does not work for you, then your data returned to the _views_pre_render function is probably different due to your search api, and you should follow my BootstrapBlocks_views_pre_render function above, and you should be filtering out the duplicates.

0

I recently found that for the Search API I had set under "Use As" Search keys

I changed this to Search filter and the duplication problem went away. This seems like a bug because using the search string as a key (breaking up the words and searching with each independently) should not have caused a duplication issue.

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.