From 236638bc7a75ad37c9beeff77d19dc7e4fa07e7f Mon Sep 17 00:00:00 2001
From: Owen Bush <ojb@ukhhf.co.uk>
Date: Sat, 23 Mar 2019 22:51:36 -0600
Subject: [PATCH] Added new field formatter for eventinstance dates

---
 ...view_display.eventinstance.inline_date.yml |  26 ---
 ...ty_view_mode.eventinstance.inline_date.yml |  12 --
 src/Entity/EventSeries.php                    |   6 +-
 .../EventInstanceDateFormatter.php            | 154 ++++++++++++++++++
 4 files changed, 158 insertions(+), 40 deletions(-)
 delete mode 100644 config/install/core.entity_view_display.eventinstance.inline_date.yml
 delete mode 100644 config/install/core.entity_view_mode.eventinstance.inline_date.yml
 create mode 100644 src/Plugin/Field/FieldFormatter/EventInstanceDateFormatter.php

diff --git a/config/install/core.entity_view_display.eventinstance.inline_date.yml b/config/install/core.entity_view_display.eventinstance.inline_date.yml
deleted file mode 100644
index f367d46..0000000
--- a/config/install/core.entity_view_display.eventinstance.inline_date.yml
+++ /dev/null
@@ -1,26 +0,0 @@
-langcode: en
-status: true
-dependencies:
-  enforced:
-    module:
-      - recurring_events
-  config:
-    - core.entity_view_mode.eventinstance.inline_date
-  module:
-    - recurring_events
-id: eventinstace.inline_date
-targetEntityType: eventinstance
-bundle: eventinstance
-mode: inline_date
-content:
-  date:
-    weight: 0
-    label: above
-    settings:
-      separator: '-'
-      format_type: medium
-      timezone_override: ''
-    third_party_settings: {  }
-    type: daterange_default
-hidden:
-  body: true
diff --git a/config/install/core.entity_view_mode.eventinstance.inline_date.yml b/config/install/core.entity_view_mode.eventinstance.inline_date.yml
deleted file mode 100644
index e56ab6f..0000000
--- a/config/install/core.entity_view_mode.eventinstance.inline_date.yml
+++ /dev/null
@@ -1,12 +0,0 @@
-langcode: en
-status: false
-dependencies:
-  enforced:
-    module:
-      - recurring_events
-  module:
-    - recurring_events
-id: eventinstance.inline_date
-label: 'Inline date'
-targetEntityType: eventinstance
-cache: true
diff --git a/src/Entity/EventSeries.php b/src/Entity/EventSeries.php
index 7f78159..bf10834 100644
--- a/src/Entity/EventSeries.php
+++ b/src/Entity/EventSeries.php
@@ -433,11 +433,13 @@ class EventSeries extends EditorialContentEntityBase implements EventInterface {
       ->setSetting('target_type', 'eventinstance')
       ->setTranslatable(FALSE)
       ->setDisplayOptions('view', [
-        'type' => 'entity_reference_entity_view',
+        'type' => 'recurring_events_eventinstance_date',
         'label' => 'above',
         'weight' => 10,
         'settings' => [
-          'view_mode' => 'inline_date',
+          'link' => TRUE,
+          'date_format' => 'F jS, Y h:iA',
+          'separator' => ' - ',
         ],
       ])
       ->setDisplayConfigurable('view', TRUE)
diff --git a/src/Plugin/Field/FieldFormatter/EventInstanceDateFormatter.php b/src/Plugin/Field/FieldFormatter/EventInstanceDateFormatter.php
new file mode 100644
index 0000000..b205285
--- /dev/null
+++ b/src/Plugin/Field/FieldFormatter/EventInstanceDateFormatter.php
@@ -0,0 +1,154 @@
+<?php
+
+namespace Drupal\recurring_events\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
+use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Core\Url;
+use Drupal\Core\Link;
+
+/**
+ * Plugin implementation of the 'recurring events eventinstance date' formatter.
+ *
+ * @FieldFormatter(
+ *   id = "recurring_events_eventinstance_date",
+ *   label = @Translation("EventInstance Date"),
+ *   description = @Translation("Display the date of the referenced eventinstance."),
+ *   field_types = {
+ *     "entity_reference"
+ *   }
+ * )
+ */
+class EventInstanceDateFormatter extends EntityReferenceFormatterBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function defaultSettings() {
+    return [
+      'link' => TRUE,
+      'date_format' => 'F jS, Y h:iA',
+      'separator' => ' - ',
+    ] + parent::defaultSettings();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsForm(array $form, FormStateInterface $form_state) {
+    $elements['link'] = [
+      '#title' => t('Link date to the referenced entity'),
+      '#type' => 'checkbox',
+      '#default_value' => $this->getSetting('link'),
+    ];
+
+    $php_date_url = Url::fromUri('https://secure.php.net/manual/en/function.date.php');
+    $php_date_link = Link::fromTextAndUrl($this->t('PHP date/time format'), $php_date_url);
+
+    $elements['date_format'] = [
+      '#type' => 'textfield',
+      '#title' => t('Date Format @link', [
+        '@link' => $php_date_link->toString(),
+      ]),
+      '#required' => TRUE,
+      '#default_value' => $this->getSetting('date_format'),
+    ];
+
+    $elements['separator'] = [
+      '#title' => t('Separator'),
+      '#type' => 'textfield',
+      '#description' => t('Enter the separator to use between start and end dates.'),
+      '#default_value' => $this->getSetting('separator'),
+    ];
+
+    return $elements;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsSummary() {
+    $summary = [];
+    $summary[] = $this->getSetting('link') ? t('Link to the referenced entity') : t('No link');
+    $summary[] = t('Format: %format', [
+      '%format' => $this->getSetting('date_format'),
+    ]);
+    $summary[] = t('Separator: %separator', [
+      '%separator' => $this->getSetting('separator'),
+    ]);
+    return $summary;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function isApplicable(FieldDefinitionInterface $field_definition) {
+    return ($field_definition->getFieldStorageDefinition()->getSetting('target_type') == 'eventinstance');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function viewElements(FieldItemListInterface $items, $langcode) {
+    $elements = [];
+    $output_as_link = $this->getSetting('link');
+
+    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
+      $date_string = '';
+      if (!empty($entity->date->start_date) && !empty($entity->date->end_date)) {
+        /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
+        $start_date = $entity->date->start_date;
+        /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
+        $end_date = $entity->date->end_date;
+
+        $date = [];
+        $date[] = $start_date->format($this->getSetting('date_format'));
+        $date[] = $end_date->format($this->getSetting('date_format'));
+
+        $date_string = implode($this->getSetting('separator'), $date);
+      }
+
+      // If the link is to be displayed and the entity has a uri, display a
+      // link.
+      if ($output_as_link && !$entity->isNew()) {
+        try {
+          $uri = $entity->toUrl();
+        }
+        catch (UndefinedLinkTemplateException $e) {
+          // This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
+          // and it means that the entity type doesn't have a link template nor
+          // a valid "uri_callback", so don't bother trying to output a link for
+          // the rest of the referenced entities.
+          $output_as_link = FALSE;
+        }
+      }
+
+      if ($output_as_link && isset($uri) && !$entity->isNew()) {
+        $elements[$delta] = [
+          '#type' => 'link',
+          '#title' => $date_string,
+          '#url' => $uri,
+          '#options' => $uri->getOptions(),
+        ];
+
+        if (!empty($items[$delta]->_attributes)) {
+          $elements[$delta]['#options'] += ['attributes' => []];
+          $elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
+          // Unset field item attributes since they have been included in the
+          // formatter output and shouldn't be rendered in the field template.
+          unset($items[$delta]->_attributes);
+        }
+      }
+      else {
+        $elements[$delta] = ['#plain_text' => $date_string];
+      }
+      $elements[$delta]['#cache']['tags'] = $entity->getCacheTags();
+    }
+
+    return $elements;
+  }
+
+}
-- 
GitLab