Skip to content
Snippets Groups Projects
Commit 3e6ed305 authored by Owen Bush's avatar Owen Bush
Browse files

Allow altering of inheritance class

parent 3ef64bad
No related branches found
No related tags found
No related merge requests found
...@@ -93,6 +93,20 @@ function hook_recurring_events_diff_array_alter(array &$diff = []) { ...@@ -93,6 +93,20 @@ function hook_recurring_events_diff_array_alter(array &$diff = []) {
unset($form_config['custom_dates']); 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. * Execute custom code before event instances are deleted.
* *
......
...@@ -54,11 +54,15 @@ function recurring_events_entity_base_field_info_alter(&$fields, EntityTypeInter ...@@ -54,11 +54,15 @@ function recurring_events_entity_base_field_info_alter(&$fields, EntityTypeInter
} }
$class = '\Drupal\recurring_events\FieldInheritanceFactory'; $class = '\Drupal\recurring_events\FieldInheritanceFactory';
if ($type === 'entity_reference') { if ($field->plugin() === 'entity_reference_inheritance') {
$class = '\Drupal\recurring_events\EntityReferenceFieldInheritanceFactory'; $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) $fields[$field->id()] = BaseFieldDefinition::create($type)
->setLabel($field->label()) ->setLabel(t('Inherited @label', ['@label' => $field->label()]))
->setName($field->id()) ->setName($field->id())
->setDescription(t('The inherited field: @field', ['@field' => $field->label()])) ->setDescription(t('The inherited field: @field', ['@field' => $field->label()]))
->setComputed(TRUE) ->setComputed(TRUE)
......
...@@ -62,6 +62,9 @@ class EntityReferenceFieldInheritanceFactory extends EntityReferenceFieldItemLis ...@@ -62,6 +62,9 @@ class EntityReferenceFieldInheritanceFactory extends EntityReferenceFieldItemLis
$this->list[$key] = $this->createItem($key, $value); $this->list[$key] = $this->createItem($key, $value);
} }
} }
else {
$this->applyDefaultValue();
}
} }
/** /**
......
...@@ -58,6 +58,9 @@ class FieldInheritanceFactory extends FieldItemList { ...@@ -58,6 +58,9 @@ class FieldInheritanceFactory extends FieldItemList {
$this->list[$key] = $this->createItem($key, $value); $this->list[$key] = $this->createItem($key, $value);
} }
} }
else {
$this->applyDefaultValue();
}
} }
/** /**
......
...@@ -11,7 +11,9 @@ use Drupal\recurring_events\FieldInheritancePluginInterface; ...@@ -11,7 +11,9 @@ use Drupal\recurring_events\FieldInheritancePluginInterface;
* id = "entity_reference_inheritance", * id = "entity_reference_inheritance",
* name = @Translation("Entity Reference Field Inheritance"), * name = @Translation("Entity Reference Field Inheritance"),
* types = { * types = {
* "entity_reference" * "entity_reference",
* "image",
* "webform"
* } * }
* ) * )
*/ */
......
...@@ -201,6 +201,8 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh ...@@ -201,6 +201,8 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
$series = $this->getEventSeries(); $series = $this->getEventSeries();
$instance = $this->getEventInstance(); $instance = $this->getEventInstance();
$values = [];
if (!empty($instance->{$this->getEntityField()}->getValue())) { if (!empty($instance->{$this->getEntityField()}->getValue())) {
$values = $instance->{$this->getEntityField()}->getValue(); $values = $instance->{$this->getEntityField()}->getValue();
} }
......
<?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 {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment