RawSQLArgument.php in Views Raw SQL 8
File
src/Plugin/views/argument/RawSQLArgument.php
View source
<?php
namespace Drupal\views_raw_sql\Plugin\views\argument;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RawSQLArgument extends ArgumentPluginBase {
protected $account;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->account = $account;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['where_raw_sql'] = [
'default' => 0,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if ($this->account
->hasPermission('edit views raw sql')) {
$form['where_raw_sql'] = [
'#type' => 'textfield',
'#title' => t('Raw SQL'),
'#default_value' => $this->options['where_raw_sql'],
'#weight' => -6,
];
}
}
public function query($group_by = FALSE) {
$this
->ensureMyTable();
$raw_sql = $this->options['where_raw_sql'];
$argument = $this->argument;
$raw_sql = str_replace('%argument%', $argument, $raw_sql);
$this->query
->addWhereExpression($this->options['group'], $raw_sql);
}
}