ApplicantCfpFilter.php

PHOTO EMBED

Mon Sep 12 2022 17:17:17 GMT+0000 (UTC)

Saved by @igor #drupal #mysql

<?php

namespace Drupal\pos_entity_applicant\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Symfony\Contracts\Translation\TranslatorTrait;

/**
 * Class ProgramCoordinatorFilter
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("applicant_cfp")
 */
class ApplicantCfpFilter extends FilterPluginBase {

  use TranslatorTrait;

  /**
   * {@inheritDoc}
   */
  public function acceptExposedInput($input) {
    return TRUE;
  }

  public function buildExposedForm(&$form, FormStateInterface $form_state) {
    if (empty($this->options['exposed'])) {
      return;
    }
    $identifier = $this->options['expose']['identifier'];
    $exposed_input = $this->view->getExposedInput();

    $form[$identifier] = [
      '#type' => 'select',
      '#title' => $this->t('Da li aplikant ima konkurs?'),
      '#options' => [
        NULL => $this->t('- Any -'),
        'yes' => $this->t('Da'),
        'no' => $this->t('Ne'),
      ],
      '#default_value' => $exposed_input,
      '#validated' => TRUE,
    ];
    return $form;
  }

  public function query() {
    $this->ensureMyTable();
    /** @var \Drupal\views\Plugin\views\query\Sql $query */
    $query = $this->query;
    $input = $this->view->getExposedInput();

    if (isset($input['applicant_cfp']) && !empty($input['applicant_cfp'])) {
      $input['applicant_cfp'] === 'yes' ?
        $query->addWhereExpression(0,"users_field_data.uid IN (SELECT DISTINCT user_id FROM users_field_data JOIN cfp ON cfp.user_id)")
      : $query->addWhereExpression(0,"users_field_data.uid NOT IN (SELECT DISTINCT user_id FROM users_field_data JOIN cfp ON cfp.user_id)");
    }
  }
}
content_copyCOPY