delivery-8.x-1.x-dev/src/Plugin/views/filter/WorkspacesList.php
src/Plugin/views/filter/WorkspacesList.php
<?php
namespace Drupal\delivery\Plugin\views\filter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Filters by given list of workspaces options.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("delivery_workspaces_list")
*/
class WorkspacesList extends InOperator {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* WorkspacesList constructor.
*
* @param array $configuration
* @param string $plugin_id
* @param \Drupal\delivery\Plugin\views\filter\mixed $plugin_definition
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->valueTitle = t('Allowed workspaces');
$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() {
$workspaces = $this->entityTypeManager->getStorage('workspace')->loadMultiple();
$options = [];
foreach ($workspaces as $workspace) {
$options[$workspace->id()] = $workspace->label();
}
return $options;
}
}
