View source
<?php
namespace Drupal\search_api\Plugin\views\argument;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Datetime\DateFormatterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SearchApiDate extends SearchApiStandard {
protected $dateFormatter;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$plugin
->setDateFormatter($container
->get('date.formatter'));
return $plugin;
}
public function getDateFormatter() {
return $this->dateFormatter ?: \Drupal::service('date.formatter');
}
public function setDateFormatter(DateFormatterInterface $date_formatter) {
$this->dateFormatter = $date_formatter;
return $this;
}
public function query($group_by = FALSE) {
$this
->fillValue();
if ($this->value === FALSE) {
$this
->abort();
return;
}
$outer_conjunction = strtoupper($this->operator);
if (empty($this->options['not'])) {
$condition_operator = '=';
$inner_conjunction = 'OR';
}
else {
$condition_operator = '<>';
$inner_conjunction = 'AND';
}
if (!empty($this->value)) {
$outer_conditions = $this->query
->createConditionGroup($outer_conjunction);
foreach ($this->value as $value) {
$value_conditions = $this->query
->createConditionGroup($inner_conjunction);
$values = explode(';', $value);
$values = array_map([
$this,
'getTimestamp',
], $values);
if (in_array(FALSE, $values, TRUE)) {
$this
->abort();
return;
}
$is_range = count($values) > 1;
$inner_conditions = $is_range ? $this->query
->createConditionGroup('AND') : $value_conditions;
$range_op = empty($this->options['not']) ? '>=' : '<';
$inner_conditions
->addCondition($this->realField, $values[0], $is_range ? $range_op : $condition_operator);
if ($is_range) {
$range_op = empty($this->options['not']) ? '<=' : '>';
$inner_conditions
->addCondition($this->realField, $values[1], $range_op);
$value_conditions
->addConditionGroup($inner_conditions);
}
$outer_conditions
->addConditionGroup($value_conditions);
}
$this->query
->addConditionGroup($outer_conditions);
}
}
public function title() {
if (!empty($this->argument)) {
$this
->fillValue();
$dates = [];
foreach ($this->value as $date) {
$date_parts = explode(';', $date);
$ts = $this
->getTimestamp($date_parts[0]);
$date_string = $this
->getDateFormatter()
->format($ts, 'short');
if (count($date_parts) > 1) {
$ts = $this
->getTimestamp($date_parts[1]);
$date_string .= ' – ' . $this
->getDateFormatter()
->format($ts, 'short');
}
if ($date_string) {
$dates[] = $date_string;
}
}
return $dates ? implode(', ', $dates) : $this->argument;
}
return $this->argument;
}
protected function getTimestamp($value) {
if (is_numeric($value)) {
return (int) $value;
}
return strtotime($value);
}
protected function unpackArgumentValue($force_int = FALSE) {
if (!isset($this->value)) {
$this->value = [];
}
if (!isset($this->operator)) {
$this->operator = 'or';
}
if (empty($this->argument)) {
return;
}
if (preg_match('/^([-\\d;:\\s]+\\+)*[-\\d;:\\s]+$/', $this->argument)) {
$this->value = explode('+', $this->argument);
}
elseif (preg_match('/^([-\\d;:\\s]+,)*[-\\d;:\\s]+$/', $this->argument)) {
$this->operator = 'and';
$this->value = explode(',', $this->argument);
}
if (!empty($this->argument) && (empty($this->value) || !is_array($this->value))) {
$this->value = FALSE;
}
}
protected function abort() {
$variables['@field'] = $this->definition['group'] . ': ' . $this->definition['title'];
$this->query
->abort(new FormattableMarkup('Illegal argument passed to @field contextual filter.', $variables));
}
}