From 3e6ed305e289318ade63bacec5ccc50f189dfadc Mon Sep 17 00:00:00 2001
From: Owen Bush <ojb@ukhhf.co.uk>
Date: Fri, 17 May 2019 15:38:32 -0600
Subject: [PATCH] Allow altering of inheritance class

---
 recurring_events.api.php                      | 14 ++++++++++++++
 recurring_events.module                       |  8 ++++++--
 ...EntityReferenceFieldInheritanceFactory.php |  3 +++
 src/FieldInheritanceFactory.php               |  3 +++
 .../EntityReferenceFieldInheritancePlugin.php |  4 +++-
 .../FieldInheritancePluginBase.php            |  2 ++
 .../ImageFieldInheritancePlugin.php           | 19 -------------------
 7 files changed, 31 insertions(+), 22 deletions(-)
 delete mode 100644 src/Plugin/FieldInheritance/ImageFieldInheritancePlugin.php

diff --git a/recurring_events.api.php b/recurring_events.api.php
index 1720f8f..272f86d 100644
--- a/recurring_events.api.php
+++ b/recurring_events.api.php
@@ -93,6 +93,20 @@ function hook_recurring_events_diff_array_alter(array &$diff = []) {
   unset($form_config['custom_dates']);
 }
 
+/**
+ * Alter the inheritance class used to build the inherited basefield.
+ *
+ * @var string $class
+ *   The class to alter.
+ * @var Drupal\Core\Field\FieldDefinitionInterface $field
+ *   The field context.
+ */
+function hook_recurring_events_inheritance_class_alter(&$class, $field) {
+  if ($field->plugin() === 'entity_reference_inheritance') {
+    $class = '\Drupal\my_module\EntityReferenceFieldInheritanceFactory';
+  }
+}
+
 /**
  * Execute custom code before event instances are deleted.
  *
diff --git a/recurring_events.module b/recurring_events.module
index 7c630bd..82094d5 100644
--- a/recurring_events.module
+++ b/recurring_events.module
@@ -54,11 +54,15 @@ function recurring_events_entity_base_field_info_alter(&$fields, EntityTypeInter
         }
 
         $class = '\Drupal\recurring_events\FieldInheritanceFactory';
-        if ($type === 'entity_reference') {
+        if ($field->plugin() === 'entity_reference_inheritance') {
           $class = '\Drupal\recurring_events\EntityReferenceFieldInheritanceFactory';
         }
+
+        // Allow developers to override the class to use for a field.
+        \Drupal::moduleHandler()->alter('recurring_events_inheritance_class', $class, $field);
+
         $fields[$field->id()] = BaseFieldDefinition::create($type)
-          ->setLabel($field->label())
+          ->setLabel(t('Inherited @label', ['@label' => $field->label()]))
           ->setName($field->id())
           ->setDescription(t('The inherited field: @field', ['@field' => $field->label()]))
           ->setComputed(TRUE)
diff --git a/src/EntityReferenceFieldInheritanceFactory.php b/src/EntityReferenceFieldInheritanceFactory.php
index 8520ee0..b335ed8 100644
--- a/src/EntityReferenceFieldInheritanceFactory.php
+++ b/src/EntityReferenceFieldInheritanceFactory.php
@@ -62,6 +62,9 @@ class EntityReferenceFieldInheritanceFactory extends EntityReferenceFieldItemLis
         $this->list[$key] = $this->createItem($key, $value);
       }
     }
+    else {
+      $this->applyDefaultValue();
+    }
   }
 
   /**
diff --git a/src/FieldInheritanceFactory.php b/src/FieldInheritanceFactory.php
index 964690b..c34be3a 100644
--- a/src/FieldInheritanceFactory.php
+++ b/src/FieldInheritanceFactory.php
@@ -58,6 +58,9 @@ class FieldInheritanceFactory extends FieldItemList {
         $this->list[$key] = $this->createItem($key, $value);
       }
     }
+    else {
+      $this->applyDefaultValue();
+    }
   }
 
   /**
diff --git a/src/Plugin/FieldInheritance/EntityReferenceFieldInheritancePlugin.php b/src/Plugin/FieldInheritance/EntityReferenceFieldInheritancePlugin.php
index b47c62d..bdd861a 100644
--- a/src/Plugin/FieldInheritance/EntityReferenceFieldInheritancePlugin.php
+++ b/src/Plugin/FieldInheritance/EntityReferenceFieldInheritancePlugin.php
@@ -11,7 +11,9 @@ use Drupal\recurring_events\FieldInheritancePluginInterface;
  *   id = "entity_reference_inheritance",
  *   name = @Translation("Entity Reference Field Inheritance"),
  *   types = {
- *     "entity_reference"
+ *     "entity_reference",
+ *     "image",
+ *     "webform"
  *   }
  * )
  */
diff --git a/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php b/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php
index b737388..5305106 100644
--- a/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php
+++ b/src/Plugin/FieldInheritance/FieldInheritancePluginBase.php
@@ -201,6 +201,8 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
     $series = $this->getEventSeries();
     $instance = $this->getEventInstance();
 
+    $values = [];
+
     if (!empty($instance->{$this->getEntityField()}->getValue())) {
       $values = $instance->{$this->getEntityField()}->getValue();
     }
diff --git a/src/Plugin/FieldInheritance/ImageFieldInheritancePlugin.php b/src/Plugin/FieldInheritance/ImageFieldInheritancePlugin.php
deleted file mode 100644
index 622ec9a..0000000
--- a/src/Plugin/FieldInheritance/ImageFieldInheritancePlugin.php
+++ /dev/null
@@ -1,19 +0,0 @@
-<?php
-
-namespace Drupal\recurring_events\Plugin\FieldInheritance;
-
-use Drupal\recurring_events\FieldInheritancePluginInterface;
-
-/**
- * Image Inheritance plugin.
- *
- * @FieldInheritance(
- *   id = "image_inheritance",
- *   name = @Translation("Image Field Inheritance"),
- *   types = {
- *     "image"
- *   }
- * )
- */
-class ImageFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
-}
-- 
GitLab