I am using Drupal 8.7.11 and have created a custom views exposed filter which provides users the option to filter an entity reference field using a checkbox list of names instead of autocomplete.
At the moment I have hardcoded the query conditions to build the list in a entity query with certain content checks e.g. date is in the future
However I would like to reuse the filter in a different view which doesn't need to check if a date is in the future.
Is it possible to either get the node ids from the view result, which I can then build the option list from or get the existing views filter criteria that the view uses to put into my list query?
Here is the code I have so far which works for on specific view.
views_mods.module
function views_mods_views_data_alter(&$data) {
$data['node__field_event_tutor']['tutor'] = array(
'title' => t('Tutor'),
'filter' => [
'title' => t('Tutor'),
'help' => t('Select by tutor - provided by views mods module.'),
'field' => 'field_event_tutor_target_id',
'id' => 'views_mods_tutor',
'group' => 'Content'
],
);
}
src/Plugin/views/filter/TutorViewsFilter.php
<?php
namespace Drupal\views_mods\Plugin\views\filter;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\user\Entity\User;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Drupal\views\ViewExecutable;
/**
* Filters by tutor.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("views_mods_tutor")
*/
class TutorViewsFilter extends ManyToOne {
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->valueTitle = t('Tutor');
$this->definition['options callback'] = [$this, 'generateOptions'];
}
/**
* Override the query so that no filtering takes place if the user doesn't
* select any options.
*/
public function query() {
if (!empty($this->value)) {
parent::query();
}
}
/**
* Skip validation if no options have been chosen so we can use it as a
* non-filter.
*/
public function validate() {
if (!empty($this->value)) {
parent::validate();
}
}
/**
* Helper function that generates the options.
* @return array
*/
public function generateOptions() {
$tutor_options = [];
$time = new DrupalDateTime('now');
// use entity query to get nids - this respects group memberships
// so won't show tutors that are attached to nodes the user does not have
// access to.
$eq = \Drupal::entityQuery('node');
$or_type = $eq->orConditionGroup()
->condition('type', 'tutorial')
->condition('title', 'workshop');
$or_exclude = $eq->orConditionGroup()
->condition('field_exclude_from_listings', 0)
->notExists('field_exclude_from_listings'); // IS NULL
$or_eas = $eq->orConditionGroup()
->condition('field_legacy_eas', 0)
->notExists('field_legacy_eas'); // IS NULL
$nids = $eq
->condition('status', 1)
->condition($or_type)
->condition($or_exclude)
->condition($or_eas)
->condition('field_event_time', $time->format('Y-m-d\TH:i:s'), '>=')
->execute();
$dq = \Drupal::database()->select('node__field_event_tutor', 'fe_tutor');
$dq->addField('fe_tutor', 'field_event_tutor_target_id', 'uid');
$dq->addField('f_name', 'field_prof_first_name_value', 'fname');
$dq->addField('l_name', 'field_prof_last_name_value', 'lname');
$dq->leftJoin('profile', 'profile', "fe_tutor.field_event_tutor_target_id = profile.uid AND profile.type = 'employee'");
$dq->leftJoin('profile__field_prof_first_name', 'f_name', 'profile.profile_id = f_name.entity_id');
$dq->leftJoin('profile__field_prof_last_name', 'l_name', 'profile.profile_id = l_name.entity_id');
$dq->condition('fe_tutor.entity_id', $nids, 'IN');
$dq->groupBy('fe_tutor.field_event_tutor_target_id');
$dq->groupBy('f_name.field_prof_first_name_value');
$dq->groupBy('l_name.field_prof_last_name_value');
$tutor_list = $dq->execute()->fetchAll();
if ($tutor_list) {
foreach ($tutor_list as $index => $user) {
$name = trim($user->fname) . ' ' . trim($user->lname);
if (empty($user->lname)) {
$account = User::load($user->uid);
$name = $account->getAccountName();
}
$tutor_options[$user->uid] = $name;
}
}
return $tutor_options;
}
}