OpignoGroupMembershipBoolean.php in Opigno Learning path 3.x
File
src/Plugin/views/filter/OpignoGroupMembershipBoolean.php
View source
<?php
namespace Drupal\opigno_learning_path\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\BooleanOperator;
class OpignoGroupMembershipBoolean extends BooleanOperator {
public $no_operator = TRUE;
protected function operators() {
return [];
}
public function getValueOptions() {
$this->valueOptions = [
0 => $this
->t('All trainings'),
1 => $this
->t('My trainings'),
];
}
protected function valueForm(&$form, FormStateInterface $form_state) {
if (empty($this->valueOptions)) {
$this
->getValueOptions();
}
$exposed = $form_state
->get('exposed');
$form['value'] = [
'#type' => 'radios',
'#title' => $this->value_value,
'#options' => $this->valueOptions,
'#default_value' => $this->value,
];
if (!empty($this->options['exposed'])) {
$identifier = $this->options['expose']['identifier'];
$user_input = $form_state
->getUserInput();
if ($exposed && !isset($user_input[$identifier])) {
$user_input[$identifier] = $this->value;
$form_state
->setUserInput($user_input);
}
}
}
public function query() {
$this
->ensureMyTable();
$user = \Drupal::currentUser();
$value = $this->value;
if ($value) {
$this->query
->addWhere($this->options['group'], 'group_content_field_data_groups_field_data.entity_id', $user
->id(), '=');
}
}
protected function exposedTranslate(&$form, $type) {
parent::exposedTranslate($form, $type);
if ($form['#type'] == 'select') {
$form['#type'] = 'radios';
}
}
}