0

I created a view. And I created a custom filter in a module via hook_views_pre_render.

In this view I have and exposed filter and ajax in enabled.

When the view page is loaded it works perfectly, but when I use the filter doesn't work any more cause the hook_views_pre_render is not invoked.

How can I make this filter work even when ajax is enabled?

A views filter plugin is suggested, I looked at the tutorial on webomelette but I don't understand how I can change my custom filter to a views filter with the plugin. I want to remove all duplicate results in the same month. I tried all the standard views solutions but they don't work so I need a custom module to do this. This is the code I've written:

//I create an array to save for all nodes
$nodes = [];

//I loop through all view results
foreach ($view->result as $i => $val) {

  /* REMOVE DUPLICATE RESULT IN SAME MONTH */

  //get nid
  $nid = $val->node_nid;

  //get month
  $month = explode('-', $val->_relationship_entities['field_datum']->get('field_begin_datum')->value)[1];

  //set month-id code
  $idmonth = $nid . '-' . $month;

  //if current nid in same month is already in array
  if (in_array($idmonth, $nodes)) {
    //unset this duplicate result
    unset($view->result[$i]);
  }

  //else
  else {
    //save nid and month in array
    $nodes[$i] = $idmonth;
  }

  /* END */
}

Thanks in advance

  • Did you try a filter plugin? See webomelette.com/creating-custom-views-filter-drupal-8 – 4k4 Jan 21 '18 at 10:36
  • Yes, I already saw this tutorial but I don't understand it, that's why I choose the method I do understand. I updated my question with more information on what I want to filter by. – Joost Jan 21 '18 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.