Score:0

Nested conditions groups in views filter plugin

ni flag

I need to nest some "where Groups" on a QueryPluginBase query, but I can only nest first depth group to the main "where" using setWhereGroup. I need to add groups into another groups but there isn't a function to do it because setWhereGroup let you add a new OR/AND group but I can't set the parent group ID I'd like to nest. For example, I'd need:

((Condition1 OR Condition2) AND ( (Condition3 OR Condicion4) AND (Condition5 OR Condition6) )) OR (Condition7)

This is my custom views filter plugin:

namespace Drupal\my_module\Plugin\views\filter;

use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;

/**
 * My custom filter
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("my_custom_filter") 
 */
class MyCustomFilter extends FilterPluginBase {

    /**
     * {@inheritdoc}
     */
    public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
        parent::init($view, $display, $options);
        $this->valueTitle = t('Filtro agendas usuario');
    }

    public function query() {
      $this->query->setWhereGroup('OR', '90');
      $user_org = $user->field_usr_org;
      
      foreach ($user_org as $org) {
         $this->query->addWhere('90', 'mytable.myfieldid', $org->target_id, '=');
      }

      $this->query->setWhereGroup('OR', '91');
      ....
    }
}  
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.