From f98e1896f857e8d7cc76a6bdace31daedbb3d44a Mon Sep 17 00:00:00 2001
From: Owen Bush <ojb@ukhhf.co.uk>
Date: Fri, 12 Apr 2019 14:05:10 -0700
Subject: [PATCH] Reworking the inheritance configurations

---
 ..._events_registration.registrant.config.yml | 10 ++---
 .../recurring_events_registration.tokens.inc  |  2 +-
 src/Entity/EventInstance.php                  |  4 +-
 src/FieldInheritanceFactory.php               |  2 +-
 .../FieldInheritancePluginBase.php            | 40 +++++++++++++------
 5 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/modules/recurring_events_registration/config/install/recurring_events_registration.registrant.config.yml b/modules/recurring_events_registration/config/install/recurring_events_registration.registrant.config.yml
index 047c07a..7209fba 100644
--- a/modules/recurring_events_registration/config/install/recurring_events_registration.registrant.config.yml
+++ b/modules/recurring_events_registration/config/install/recurring_events_registration.registrant.config.yml
@@ -2,16 +2,16 @@ show_capacity: true
 email_notifications: true
 registration_notification_enabled: true
 registration_notification_subject: "You've Successfully Registered"
-registration_notification_body: "Your registration for the [eventinstance:title] event was successful."
+registration_notification_body: "Your registration for the [eventinstance:title] [eventinstance:reg_type] was successful."
 waitlist_notification_enabled: true
 waitlist_notification_subject: "You've Been Added To The Waitlist"
-waitlist_notification_body: "You have been added to the waitlist for the [eventinstance:title] event."
+waitlist_notification_body: "You have been added to the waitlist for the [eventinstance:title] [eventinstance:reg_type]."
 promotion_notification_enabled: true
 promotion_notification_subject: "You've Been Added To The Registration List"
-promotion_notification_body: "You have been promoted from the waitlist to the registration list for the [eventinstance:title] event."
+promotion_notification_body: "You have been promoted from the waitlist to the registration list for the [eventinstance:title] [eventinstance:reg_type]."
 cancellation_notification_enabled: true
 cancellation_notification_subject: "An Event Has Been Cancelled"
-cancellation_notification_body: "Unfortunately, the [eventinstance:title] event has been cancelled."
+cancellation_notification_body: "Unfortunately, the [eventinstance:title] [eventinstance:reg_type] has been cancelled."
 modification_notification_enabled: true
 modification_notification_subject: "An Event Has Been Modified"
-modification_notification_body: "The [eventinstance:title] event has been modified, please check back for details."
\ No newline at end of file
+modification_notification_body: "The [eventinstance:title] [eventinstance:reg_type] has been modified, please check back for details."
\ No newline at end of file
diff --git a/modules/recurring_events_registration/recurring_events_registration.tokens.inc b/modules/recurring_events_registration/recurring_events_registration.tokens.inc
index 02a7d98..e952461 100644
--- a/modules/recurring_events_registration/recurring_events_registration.tokens.inc
+++ b/modules/recurring_events_registration/recurring_events_registration.tokens.inc
@@ -56,7 +56,7 @@ function recurring_events_registration_tokens($type, $tokens, array $data, array
           break;
 
         case 'reg_type':
-          $replacements[$original] = $creation_service->getRegistrationType();
+          $replacements[$original] = $creation_service->getRegistrationType() == 'series' ?: 'event';
           break;
       }
     }
diff --git a/src/Entity/EventInstance.php b/src/Entity/EventInstance.php
index 051952d..1066cd0 100644
--- a/src/Entity/EventInstance.php
+++ b/src/Entity/EventInstance.php
@@ -382,7 +382,7 @@ class EventInstance extends EditorialContentEntityBase implements EventInterface
 
     // Inherited fields from eventseries entity.
     $fields['title'] = BaseFieldDefinition::create('string')
-      ->setLabel(t('Title'))
+      ->setLabel(t('Inherited Series Title'))
       ->setDescription(t('The title as inherited from the series.'))
       ->setComputed(TRUE)
       ->setClass('\Drupal\recurring_events\FieldInheritanceFactory')
@@ -399,7 +399,7 @@ class EventInstance extends EditorialContentEntityBase implements EventInterface
       ]);
 
     $fields['description'] = BaseFieldDefinition::create('text_long')
-      ->setLabel(t('Description'))
+      ->setLabel(t('Inherited Series Description'))
       ->setDescription(t('The description as inherited from the series.'))
       ->setComputed(TRUE)
       ->setClass('\Drupal\recurring_events\FieldInheritanceFactory')
diff --git a/src/FieldInheritanceFactory.php b/src/FieldInheritanceFactory.php
index 233b0d1..b8a2120 100644
--- a/src/FieldInheritanceFactory.php
+++ b/src/FieldInheritanceFactory.php
@@ -45,7 +45,7 @@ class FieldInheritanceFactory extends FieldItemList {
   }
 
   /**
-   * Compute the list property from state.
+   * Compute the field property from state.
    */
   protected function computeValue() {
     $entity = $this->getEntity();
diff --git a/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php b/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php
index a1a5afa..7a8e92e 100644
--- a/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php
+++ b/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php
@@ -102,11 +102,13 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
    * {@inheritdoc}
    */
   public function computeValue() {
+    $this->validateArguments();
     $method = $this->getMethod();
     $field = $this->getSourceField();
 
     $instance = $this->entity;
     $series = $instance->getEventSeries();
+    $value = '';
 
     switch ($method) {
       case 'inherit':
@@ -114,9 +116,6 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
         break;
 
       case 'prepend':
-        if (empty($this->getEntityField())) {
-          throw new \InvalidArgumentException("The definition's 'entity field' key must be set to prepend data.");
-        }
         $entity_field = $this->getEntityField();
 
         $fields = [];
@@ -130,9 +129,6 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
         break;
 
       case 'append':
-        if (empty($this->getEntityField())) {
-          throw new \InvalidArgumentException("The definition's 'entity field' key must be set to append data.");
-        }
         $entity_field = $this->getEntityField();
 
         $fields = [];
@@ -146,9 +142,6 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
         break;
 
       case 'fallback':
-        if (empty($this->getEntityField())) {
-          throw new \InvalidArgumentException("The definition's 'entity field' key must be set to fallback to series data.");
-        }
         $entity_field = $this->getEntityField();
 
         $value = '';
@@ -161,13 +154,36 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
         }
 
         break;
+    }
+    return $value;
+  }
 
-      default:
-        throw new \InvalidArgumentException("The definition's 'method' key must be one of: inherit, prepend, or append.");
+  /**
+   * Validate the configuration arguments of the plugin.
+   */
+  protected function validateArguments() {
+    if (empty($this->getMethod())) {
+      throw new \InvalidArgumentException("The definition's 'method' key must be set to inherit data.");
+    }
 
+    if (empty($this->getSourceField())) {
+      throw new \InvalidArgumentException("The definition's 'source field' key must be set to inherit data.");
     }
 
-    return $value;
+    $method = $this->getMethod();
+    $entity_field_methods = [
+      'prepend',
+      'append',
+      'fallback',
+    ];
+
+    if (array_search($method, $entity_field_methods)) {
+      if (empty($this->getEntityField())) {
+        throw new \InvalidArgumentException("The definition's 'entity field' key must be set to prepend, append, or fallback to series data.");
+      }
+    }
+
+    return TRUE;
   }
 
 }
-- 
GitLab