Skip to content
Snippets Groups Projects
Commit 555814b0 authored by Dan Lobelle's avatar Dan Lobelle Committed by Owen Bush
Browse files

Issue #3324055 by muriqui, endless_wander: Allow "Event series start date"...

Issue #3324055 by muriqui, endless_wander: Allow "Event series start date" views field to be click sortable
parent 27336397
No related branches found
No related tags found
1 merge request!62Issue #3324055: Allow "Event series start date" views field to be click sortable
...@@ -27,7 +27,7 @@ function recurring_events_views_data_alter(array &$data) { ...@@ -27,7 +27,7 @@ function recurring_events_views_data_alter(array &$data) {
'title' => t('Event series start date'), 'title' => t('Event series start date'),
'help' => t('The date on which an event first occurs.'), 'help' => t('The date on which an event first occurs.'),
'id' => 'eventseries_start_date', 'id' => 'eventseries_start_date',
'click sortable' => FALSE, 'click sortable' => TRUE,
], ],
]; ];
......
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
namespace Drupal\recurring_events\Plugin\views\field; namespace Drupal\recurring_events\Plugin\views\field;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow; use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -16,43 +20,120 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -16,43 +20,120 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class EventSeriesStartDate extends FieldPluginBase { class EventSeriesStartDate extends FieldPluginBase {
/** /**
* The date formatter service. * The config factory service.
* *
* @var \Drupal\Core\Datetime\DateFormatterInterface * @var \Drupal\Core\Config\ConfigFactoryInterface
*/ */
protected $dateFormatter; protected $configFactory;
/**
* The current active database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The views join handler service.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $joinHandler;
/**
* Constructs an EventSeriesStartDate object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Database\Connection $database
* The current active database connection.
* @param \Drupal\Component\Plugin\PluginManagerInterface $join_handler
* The views join handler service.
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, Connection $database, PluginManagerInterface $join_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->database = $database;
$this->joinHandler = $join_handler;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$instance->dateFormatter = $container->get('date.formatter'); $config_factory = $container->get('config.factory');
return $instance; /** @var \Drupal\Core\Database\Connection $database */
$database = $container->get('database');
/** @var \Drupal\Component\Plugin\PluginManagerInterface $join_handler */
$join_handler = $container->get('plugin.manager.views.join');
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$config_factory,
$database,
$join_handler
);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function query() { public function query() {
// Leave empty to avoid a query on this field. // Add a subquery to get the earliest instance start date for a series.
$subQuery = $this->database->select('eventinstance_field_data', 'eventinstance_field_data');
$subQuery->addField('eventinstance_field_data', 'eventseries_id');
$subQuery->addExpression("MIN(eventinstance_field_data.date__value)", 'eventseries_start_date');
$subQuery->groupBy("eventinstance_field_data.eventseries_id");
// Create a join for the subquery.
$joinDefinition = [
'table formula' => $subQuery,
'field' => 'eventseries_id',
'left_table' => 'eventseries_field_data',
'left_field' => 'id',
'adjust' => TRUE,
];
// Add the subquery join to the main query.
/** @var \Drupal\views\Plugin\views\join\JoinPluginBase $join */
$join = $this->joinHandler->createInstance('standard', $joinDefinition);
$this->query->addRelationship('eventseries_start_date', $join, 'eventseries_field_data');
// Add the field to the view.
$this->query->addField(NULL, 'eventseries_start_date', 'eventseries_start_date');
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function render(ResultRow $values) { public function render(ResultRow $values) {
$start_date = 'N/A'; if (!isset($values->eventseries_start_date)) {
return 'N/A';
}
$event = $values->_entity; $date = new DrupalDateTime($values->eventseries_start_date, 'UTC');
$event_start = $event->getSeriesStart(); $format = $this->configFactory->get('recurring_events.eventseries.config')->get('date_format');
$timezone = new \DateTimeZone(date_default_timezone_get()); return $date->format($format, [
'timezone' => date_default_timezone_get(),
]);
}
if (!empty($event_start)) { /**
$format = \Drupal::config('recurring_events.eventseries.config')->get('date_format'); * {@inheritdoc}
$start_date = $this->dateFormatter->format($event_start->getTimestamp(), 'custom', $format, $timezone->getName()); */
public function clickSort($order) {
if (isset($this->field_alias)) {
$params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
$this->query->addOrderBy(NULL, 'eventseries_start_date', $order, $this->field_alias, $params);
} }
return $start_date;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment