View source
<?php
namespace Drupal\civicrm_entity\Plugin\views\filter;
use Drupal\views\Plugin\views\filter\Date as BaseDate;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Datetime\DateFormatterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Date extends BaseDate {
protected $dateFormat = 'Y-m-d H:i:s';
public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('date.formatter'));
}
protected function opBetween($field) {
$a = intval(strtotime($this->value['min'], 0));
$b = intval(strtotime($this->value['max'], 0));
if ($this->value['type'] == 'offset') {
$now = time();
$a = $now + sprintf('%+d', $a);
$b = $now + sprintf('%+d', $b);
}
$a = DateTimePlus::createFromTimestamp($a);
$a = $this->dateFormatter
->format($a
->getTimestamp(), 'custom', $this->dateFormat);
$a = $this
->getFieldDateFormat("'" . $a . "'");
$b = DateTimePlus::createFromTimestamp($b);
$b = $this->dateFormatter
->format($b
->getTimestamp(), 'custom', $this->dateFormat);
$b = $this
->getFieldDateFormat("'" . $b . "'");
$operator = strtoupper($this->operator);
$field = $this
->getFieldDateFormat($field);
$this->query
->addWhereExpression($this->options['group'], "{$field} {$operator} {$a} AND {$b}");
}
protected function opSimple($field) {
$value = intval(strtotime($this->value['value'], 0));
if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
$value = time() + sprintf('%+d', $value);
}
$value = DateTimePlus::createFromTimestamp($value);
$value = $this->dateFormatter
->format($value
->getTimestamp(), 'custom', $this->dateFormat);
$value = $this
->getFieldDateFormat("'" . $value . "'");
$field = $this
->getFieldDateFormat($field);
$this->query
->addWhereExpression($this->options['group'], "{$field} {$this->operator} {$value}");
}
protected function getFieldDateFormat($field) {
return $this->query
->getDateFormat($this->query
->getDateField($field, TRUE, FALSE), $this->dateFormat);
}
}