View source
<?php
namespace Drupal\media_directories\Plugin\views\argument;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaDirectoryArgument extends ArgumentPluginBase {
protected $configFactory;
public $operator;
public $value;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $configFactory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->definition = $plugin_definition + $configuration;
$this->configFactory = $configFactory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
public function title() {
if (!$this->argument) {
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this
->t('Uncategorized');
}
if (!empty($this->options['break_phrase'])) {
$break = static::breakString($this->argument, FALSE);
$this->value = $break->value;
$this->operator = $break->operator;
}
else {
$this->value = [
$this->argument,
];
$this->operator = 'or';
}
if (empty($this->value)) {
return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : $this
->t('Uncategorized');
}
if ($this->value === [
MEDIA_DIRECTORY_ROOT,
]) {
return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : $this
->t('Invalid input');
}
return implode($this->operator == 'or' ? ' + ' : ', ', $this
->titleQuery());
}
public function titleQuery() {
return $this->value;
}
public function query($group_by = FALSE) {
$this
->ensureMyTable();
$config = $this->configFactory
->get('media_directories.settings');
$placeholder = $this
->placeholder();
$null_check = empty($this->options['not']) ? '' : " OR {$this->tableAlias}.{$this->realField} IS NULL";
if ((int) $this->argument === MEDIA_DIRECTORY_ROOT) {
$group = 0;
if ($config
->get('all_files_in_root')) {
$group = 9;
$this->query
->setWhereGroup('OR', $group);
$this->query
->addWhereExpression($group, "{$this->tableAlias}.{$this->realField} IS NOT NULL");
}
$this->query
->addWhereExpression($group, "{$this->tableAlias}.{$this->realField} IS NULL");
}
else {
$operator = empty($this->options['not']) ? '=' : '!=';
$this->query
->addWhereExpression(0, "{$this->tableAlias}.{$this->realField} {$operator} {$placeholder}" . $null_check, [
$placeholder => $this->argument,
]);
}
}
public function getSortName() {
return $this
->t('Numerical', [], [
'context' => 'Sort order',
]);
}
public function getContextDefinition() {
if ($context_definition = parent::getContextDefinition()) {
return $context_definition;
}
return new ContextDefinition('integer', $this
->adminLabel(), FALSE);
}
}