SectionId.php in Workbench Access 8
File
src/Plugin/views/filter/SectionId.php
View source
<?php
namespace Drupal\workbench_access\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Drupal\views\Views;
use Drupal\views\ManyToOneHelper;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\workbench_access\UserSectionStorageInterface;
use Drupal\workbench_access\WorkbenchAccessManager;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SectionId extends ManyToOne {
protected $manager;
protected $userSectionStorage;
public function setManager(WorkbenchAccessManagerInterface $manager) {
$this->manager = $manager;
return $this;
}
public function setUserSectionStorage(UserSectionStorageInterface $userSectionStorage) {
$this->userSectionStorage = $userSectionStorage;
return $this;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['operator']['default'] = 'in';
$options['value']['default'] = [
'All',
];
$options['output_format']['default'] = 'label';
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['output_format'] = [
'#type' => 'select',
'#title' => $this
->t('Output format'),
'#options' => [
'label' => $this
->t('Section label'),
'id' => $this
->t('Section id'),
],
'#default_value' => $this->options['output_format'],
];
}
public function defaultExposeOptions() {
parent::defaultExposeOptions();
$this->options['expose']['reduce'] = TRUE;
$this->options['output_format']['default'] = 'label';
}
public function getValueOptions() {
if (isset($this->valueOptions)) {
return $this->valueOptions;
}
$schemes = \Drupal::entityTypeManager()
->getStorage('access_scheme')
->loadMultiple();
foreach ($schemes as $scheme) {
$tree = $scheme
->getAccessScheme()
->getTree();
foreach ($tree as $items) {
foreach ($items as $id => $item) {
$text = $this->options['output_format'] == 'label' ? $item['label'] : $id;
$options[$id] = str_repeat('-', $item['depth']) . ' ' . $text;
}
}
}
$this->valueOptions = $options;
return $this->valueOptions;
}
public function operators() {
$operators = [
'in' => [
'title' => $this
->t('Is one of'),
'short' => $this
->t('in'),
'short_single' => $this
->t('='),
'method' => 'opSimple',
'values' => 1,
],
'not in' => [
'title' => $this
->t('Is not one of'),
'short' => $this
->t('not in'),
'short_single' => $this
->t('<>'),
'method' => 'opSimple',
'values' => 1,
],
];
return $operators;
}
}