diff --git a/recurring_events.api.php b/recurring_events.api.php index 1720f8fe28696a93e94f073918e7a4baee32d70d..272f86d437d73661262f6889f0c31664d468cfc8 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 7c630bd957710cf0dd14ae11fdef928bfb060eec..82094d5e4c593e90524a6a899fb2ed054cdd394b 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 8520ee018111e84905bc783ff2e0169073e0495a..b335ed8432a42ecb2c3d25b4120abf547fe1707e 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 964690b1be9f283d9c71bba9269eb846bf91b59e..c34be3a38b0baa6911ddbd4e13b3388ba265f884 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 b47c62d06327ed595acfb6c7aecb08549362e02f..bdd861a541c5082f37e014fe7ed50c220bdbc918 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 b7373880824be97cbe5613fe937f4c985f920d30..53051068b8be42374f725029420a5697d26310d8 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 622ec9a3f52685a465acd0ec2b147b19677dda1c..0000000000000000000000000000000000000000 --- 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 { -}