From c2955f11de4a45810e44070faf3a78f9366e95a5 Mon Sep 17 00:00:00 2001
From: Owen Bush <ojb@ukhhf.co.uk>
Date: Wed, 27 Mar 2019 22:21:52 -0600
Subject: [PATCH] Initial commit of the views integration

---
 .../recurring_events.eventinstance.config.yml |  1 +
 config/schema/recurring_events.schema.yml     | 17 +++++--
 recurring_events.views.inc                    | 51 +++++++++++++++++++
 src/Entity/EventInstance.php                  | 20 ++++++++
 src/Entity/EventSeries.php                    | 12 ++++-
 src/EventSeriesListBuilder.php                |  2 +-
 .../views/field/EventInstanceDescription.php  | 32 ++++++++++++
 src/Plugin/views/field/EventInstanceTitle.php | 32 ++++++++++++
 .../views/field/EventSeriesInstanceCount.php  | 32 ++++++++++++
 .../views/field/EventSeriesStartDate.php      | 40 +++++++++++++++
 10 files changed, 234 insertions(+), 5 deletions(-)
 create mode 100644 config/install/recurring_events.eventinstance.config.yml
 create mode 100644 recurring_events.views.inc
 create mode 100644 src/Plugin/views/field/EventInstanceDescription.php
 create mode 100644 src/Plugin/views/field/EventInstanceTitle.php
 create mode 100644 src/Plugin/views/field/EventSeriesInstanceCount.php
 create mode 100644 src/Plugin/views/field/EventSeriesStartDate.php

diff --git a/config/install/recurring_events.eventinstance.config.yml b/config/install/recurring_events.eventinstance.config.yml
new file mode 100644
index 00000000..cea247ac
--- /dev/null
+++ b/config/install/recurring_events.eventinstance.config.yml
@@ -0,0 +1 @@
+date_format: 'F jS, Y h:iA'
diff --git a/config/schema/recurring_events.schema.yml b/config/schema/recurring_events.schema.yml
index 9de82051..4e945027 100644
--- a/config/schema/recurring_events.schema.yml
+++ b/config/schema/recurring_events.schema.yml
@@ -4,16 +4,27 @@ recurring_events.eventseries.config:
   mapping:
     interval:
       type: integer
-      label: 'The interval between times when creating events'
+      label: 'The interval between times when creating event series'
     min_time:
       type: string
       label: 'The earliest an event can start'
     max_time:
       type: string
       label: 'The latest an event can start'
+    date_format:
+      type: string
+      label: 'The formatting of dates when displaying event series'
     time_format:
       type: string
-      label: 'The formatting of times when creating events'
+      label: 'The formatting of times when creating event series'
     days:
       type: string
-      label: 'The days of the week available when creating events'
\ No newline at end of file
+      label: 'The days of the week available when creating event series'
+
+recurring_events.eventinstance.config:
+  type: config_object
+  label: 'Recurring Events Instance Config'
+  mapping:
+    date_format:
+      type: string
+      label: 'The formatting of dates when displaying event instances'
\ No newline at end of file
diff --git a/recurring_events.views.inc b/recurring_events.views.inc
new file mode 100644
index 00000000..765e8085
--- /dev/null
+++ b/recurring_events.views.inc
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * @file
+ * Views functionality for the recurring_events module.
+ */
+
+/**
+ * Implements hook_views_data_alter().
+ */
+function recurring_events_views_data_alter(array &$data) {
+  // Create a field to show the number of instances of an eventseries.
+  $data['eventseries_field_data']['eventseries_instance_count'] = [
+    'title' => t('Event series instance count'),
+    'field' => [
+      'title' => t('Event series instance count'),
+      'help' => t('The number of event instances in a series.'),
+      'id' => 'eventseries_instance_count',
+    ],
+  ];
+
+  // Create a field to show the start date of an event series.
+  $data['eventseries_field_data']['eventseries_start_date'] = [
+    'title' => t('Event series start date'),
+    'field' => [
+      'title' => t('Event series start date'),
+      'help' => t('The date on which an event first occurs.'),
+      'id' => 'eventseries_start_date',
+    ],
+  ];
+
+  // Create a field to show the inherited title of an event instance.
+  $data['eventinstance_field_data']['eventinstance_title'] = [
+    'title' => t('Event instance title'),
+    'field' => [
+      'title' => t('Event instance title'),
+      'help' => t('The inherited title of an event instance.'),
+      'id' => 'eventinstance_title',
+    ],
+  ];
+
+  // Create a field to show the inherited description of an event instance.
+  $data['eventinstance_field_data']['eventinstance_description'] = [
+    'title' => t('Event instance description'),
+    'field' => [
+      'title' => t('Event instance description'),
+      'help' => t('The inherited description of an event instance.'),
+      'id' => 'eventinstance_description',
+    ],
+  ];
+}
diff --git a/src/Entity/EventInstance.php b/src/Entity/EventInstance.php
index 89ccd045..90b37361 100644
--- a/src/Entity/EventInstance.php
+++ b/src/Entity/EventInstance.php
@@ -426,4 +426,24 @@ class EventInstance extends EditorialContentEntityBase implements EventInterface
     return $this->get('eventseries_id')->entity;
   }
 
+  /**
+   * Get event instance inherited title.
+   *
+   * @return string
+   *   The event instance inherited title.
+   */
+  public function getInheritedTitle() {
+    return $this->get('title')->value;
+  }
+
+  /**
+   * Get event instance inherited description.
+   *
+   * @return string
+   *   The event instance inherited description.
+   */
+  public function getInheritedDescription() {
+    return $this->get('description')->value;
+  }
+
 }
diff --git a/src/Entity/EventSeries.php b/src/Entity/EventSeries.php
index 815989f9..b7e722e2 100644
--- a/src/Entity/EventSeries.php
+++ b/src/Entity/EventSeries.php
@@ -70,7 +70,7 @@ use Drupal\user\UserInterface;
  *
  * @ContentEntityType(
  *   id = "eventseries",
- *   label = @Translation("Event entity"),
+ *   label = @Translation("Event series entity"),
  *   handlers = {
  *     "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
  *     "list_builder" = "Drupal\recurring_events\EventSeriesListBuilder",
@@ -499,6 +499,16 @@ class EventSeries extends EditorialContentEntityBase implements EventInterface {
     return $date;
   }
 
+  /**
+   * Get the number of instances.
+   *
+   * @return int
+   *   A count of instances.
+   */
+  public function getInstanceCount() {
+    return count($this->get('event_instances')->getValue());
+  }
+
   /**
    * Get EventSeries recur type.
    *
diff --git a/src/EventSeriesListBuilder.php b/src/EventSeriesListBuilder.php
index 4f2b99d4..4838d2ce 100644
--- a/src/EventSeriesListBuilder.php
+++ b/src/EventSeriesListBuilder.php
@@ -120,7 +120,7 @@ class EventSeriesListBuilder extends EntityListBuilder {
       '#url' => $entity->toUrl(),
     ];
     $row['type'] = $entity->recur_type->value;
-    $row['instances'] = count($entity->event_instances->getValue());
+    $row['instances'] = $entity->getInstanceCount();
     $row['starts'] = $this->t('None');
     if (!empty($entity->getSeriesStart())) {
       $config = $this->config->get('recurring_events.eventseries.config');
diff --git a/src/Plugin/views/field/EventInstanceDescription.php b/src/Plugin/views/field/EventInstanceDescription.php
new file mode 100644
index 00000000..e1c38ba1
--- /dev/null
+++ b/src/Plugin/views/field/EventInstanceDescription.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Drupal\recurring_events\Plugin\views\field;
+
+use Drupal\views\Plugin\views\field\FieldPluginBase;
+use Drupal\views\ResultRow;
+
+/**
+ * Field handler to show the inherited event instance description.
+ *
+ * @ingroup views_field_handlers
+ *
+ * @ViewsField("eventinstance_description")
+ */
+class EventInstanceDescription extends FieldPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    // Leave empty to avoid a query on this field.
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function render(ResultRow $values) {
+    $event = $values->_entity;
+    return $event->getInheritedDescription();
+  }
+
+}
diff --git a/src/Plugin/views/field/EventInstanceTitle.php b/src/Plugin/views/field/EventInstanceTitle.php
new file mode 100644
index 00000000..a676c9fb
--- /dev/null
+++ b/src/Plugin/views/field/EventInstanceTitle.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Drupal\recurring_events\Plugin\views\field;
+
+use Drupal\views\Plugin\views\field\FieldPluginBase;
+use Drupal\views\ResultRow;
+
+/**
+ * Field handler to show the inherited event instance title.
+ *
+ * @ingroup views_field_handlers
+ *
+ * @ViewsField("eventinstance_title")
+ */
+class EventInstanceTitle extends FieldPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    // Leave empty to avoid a query on this field.
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function render(ResultRow $values) {
+    $event = $values->_entity;
+    return $event->getInheritedTitle();
+  }
+
+}
diff --git a/src/Plugin/views/field/EventSeriesInstanceCount.php b/src/Plugin/views/field/EventSeriesInstanceCount.php
new file mode 100644
index 00000000..f9512bf7
--- /dev/null
+++ b/src/Plugin/views/field/EventSeriesInstanceCount.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Drupal\recurring_events\Plugin\views\field;
+
+use Drupal\views\Plugin\views\field\FieldPluginBase;
+use Drupal\views\ResultRow;
+
+/**
+ * Field handler to show the count of event instances.
+ *
+ * @ingroup views_field_handlers
+ *
+ * @ViewsField("eventseries_instance_count")
+ */
+class EventSeriesInstanceCount extends FieldPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    // Leave empty to avoid a query on this field.
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function render(ResultRow $values) {
+    $event = $values->_entity;
+    return $event->getInstanceCount();
+  }
+
+}
diff --git a/src/Plugin/views/field/EventSeriesStartDate.php b/src/Plugin/views/field/EventSeriesStartDate.php
new file mode 100644
index 00000000..91504864
--- /dev/null
+++ b/src/Plugin/views/field/EventSeriesStartDate.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\recurring_events\Plugin\views\field;
+
+use Drupal\views\Plugin\views\field\FieldPluginBase;
+use Drupal\views\ResultRow;
+
+/**
+ * Field handler to show the start date of an event series.
+ *
+ * @ingroup views_field_handlers
+ *
+ * @ViewsField("eventseries_start_date")
+ */
+class EventSeriesStartDate extends FieldPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    // Leave empty to avoid a query on this field.
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function render(ResultRow $values) {
+    $start_date = 'N/A';
+
+    $event = $values->_entity;
+    $event_start = $event->getSeriesStart();
+
+    if (!empty($event_start)) {
+      $format = \Drupal::config('recurring_events.eventseries.config')->get('date_format');
+      $start_date = $event_start->format($format);
+    }
+    return $start_date;
+  }
+
+}
-- 
GitLab