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

Issue #3132898 by owenbush, shanahan@virtualwavemedia.com: Inherited fields...

Issue #3132898 by owenbush, shanahan@virtualwavemedia.com: Inherited fields not appearing in Display for Non-Default Events
parent de30f8b5
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,31 @@ use Drupal\Core\StringTranslation\TranslatableMarkup; ...@@ -10,6 +10,31 @@ use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Config\FileStorage; use Drupal\Core\Config\FileStorage;
/**
* Set up the config to allow field inheritance for event instances.
*/
function recurring_events_install() {
// Enable the eventinstance bundles to allow inheritance.
$config = \Drupal::configFactory()->getEditable('field_inheritance.config');
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('eventseries');
$data = $config->getRawData();
$included_entities = $data['included_entities'];
$included_entities = explode(',', $included_entities);
$included_entities[] = 'eventinstance';
sort($included_entities);
$data['included_entities'] = implode(',', $included_entities);
$included_bundles = $data['included_bundles'];
$included_bundles = explode(',', $included_bundles);
foreach ($bundles as $bundle_key => $bundle) {
$included_bundles[] = 'eventinstance:' . $bundle_key;
}
sort($included_bundles);
$data['included_bundles'] = implode(',', $included_bundles);
$config->setData($data)->save();
}
/** /**
* Install the excluded and included date fields. * Install the excluded and included date fields.
*/ */
......
...@@ -227,47 +227,67 @@ function recurring_events_eventseries_insert(EntityInterface $entity) { ...@@ -227,47 +227,67 @@ function recurring_events_eventseries_insert(EntityInterface $entity) {
// the series at that point. Now that the eventseries is properly saved we can // the series at that point. Now that the eventseries is properly saved we can
// go and set the eventseries_id on the eventinstances. // go and set the eventseries_id on the eventinstances.
$instances = $entity->event_instances->referencedEntities(); $instances = $entity->event_instances->referencedEntities();
$creation_service = \Drupal::service('recurring_events.event_creation_service');
if (!empty($instances)) { if (!empty($instances)) {
foreach ($instances as $instance) { foreach ($instances as $instance) {
if (empty($instance->eventseries_id->value)) { if (empty($instance->eventseries_id->target_id)) {
$instance->set('eventseries_id', $entity->id()); $instance->set('eventseries_id', $entity->id());
$instance->setNewRevision(FALSE); $instance->setNewRevision(FALSE);
$instance->save();
// Configure the field inheritances for this instance. $creation_service->configureDefaultInheritances($instance, $entity->id());
$entity_type = $instance->getEntityTypeId(); }
$bundle = $instance->bundle(); }
$inherited_field_ids = \Drupal::entityQuery('field_inheritance') }
->condition('sourceEntityType', 'eventseries') }
->condition('destinationEntityType', $entity_type)
->condition('destinationEntityBundle', $bundle)
->execute();
if (!empty($inherited_field_ids)) { /**
$state_key = $entity_type . ':' . $instance->uuid(); * Implements hook_ENTITY_TYPE_insert().
$state = \Drupal::keyValue('field_inheritance'); */
$state_values = $state->get($state_key); function recurring_events_field_inheritance_insert(EntityInterface $entity) {
if ($entity->sourceEntityType() === 'eventseries' && $entity->destinationEntityType() === 'eventinstance') {
$creation_service = \Drupal::service('recurring_events.event_creation_service');
$bundle = $entity->destinationEntityBundle();
$inherited_fields = \Drupal::entityTypeManager()->getStorage('field_inheritance')->loadMultiple($inherited_field_ids); $instances = \Drupal::entityTypeManager()->getStorage('eventinstance')->loadByProperties(['type' => $bundle]);
$state_values = [ if (!empty($instances)) {
'enabled' => TRUE, foreach ($instances as $instance) {
]; $creation_service->addNewDefaultInheritance($instance, $entity);
if (!empty($inherited_fields)) { }
foreach ($inherited_fields as $inherited_field) { }
$name = $inherited_field->idWithoutTypeAndBundle(); }
$state_values[$name] = [ }
'entity' => $instance->getEventSeries()->id(),
];
}
}
$state->set($state_key, $state_values);
}
$instance->save(); /**
* Implements hook_ENTITY_TYPE_update().
*/
function recurring_events_field_inheritance_update(EntityInterface $entity) {
if ($entity->sourceEntityType() === 'eventseries' && $entity->destinationEntityType() === 'eventinstance') {
$creation_service = \Drupal::service('recurring_events.event_creation_service');
$bundle = $entity->destinationEntityBundle();
$instances = \Drupal::entityTypeManager()->getStorage('eventinstance')->loadByProperties(['type' => $bundle]);
if (!empty($instances)) {
foreach ($instances as $instance) {
$creation_service->addNewDefaultInheritance($instance, $entity);
} }
} }
} }
} }
/**
* Implements hook_ENTITY_TYPE_update().
*/
function recurring_events_eventseries_update(EntityInterface $entity) {
$creation_service = \Drupal::service('recurring_events.event_creation_service');
$instances = $entity->event_instances->referencedEntities();
if (!empty($instances)) {
foreach ($instances as $instance) {
$creation_service->configureDefaultInheritances($instance, $entity->id());
}
}
}
/** /**
* Implements hook_ENTITY_TYPE_insert(). * Implements hook_ENTITY_TYPE_insert().
*/ */
...@@ -316,6 +336,16 @@ function recurring_events_eventseries_type_insert(EntityInterface $entity) { ...@@ -316,6 +336,16 @@ function recurring_events_eventseries_type_insert(EntityInterface $entity) {
$instance_description->save(); $instance_description->save();
} }
// Ensure that field_inheritance is enabled for the new instance bundle.
$config = \Drupal::configFactory()->getEditable('field_inheritance.config');
$data = $config->getRawData();
$included_bundles = $data['included_bundles'];
$included_bundles = explode(',', $included_bundles);
$included_bundles[] = 'eventinstance:' . $series_type_id;
sort($included_bundles);
$data['included_bundles'] = implode(',', $included_bundles);
$config->setData($data)->save();
if (\Drupal::moduleHandler()->moduleExists('recurring_events_registration')) { if (\Drupal::moduleHandler()->moduleExists('recurring_events_registration')) {
$registrant_types = \Drupal::entityTypeManager()->getStorage('registrant_type')->load($series_type_id); $registrant_types = \Drupal::entityTypeManager()->getStorage('registrant_type')->load($series_type_id);
if (empty($registrant_types)) { if (empty($registrant_types)) {
......
services: services:
recurring_events.event_creation_service: recurring_events.event_creation_service:
class: Drupal\recurring_events\EventCreationService class: Drupal\recurring_events\EventCreationService
arguments: ['@string_translation', '@database', '@logger.factory', '@messenger', '@plugin.manager.field.field_type', '@entity_field.manager', '@module_handler', '@entity_type.manager'] arguments: ['@string_translation', '@database', '@logger.factory', '@messenger', '@plugin.manager.field.field_type', '@entity_field.manager', '@module_handler', '@entity_type.manager', '@keyvalue']
...@@ -15,6 +15,9 @@ use Drupal\Core\Field\FieldTypePluginManager; ...@@ -15,6 +15,9 @@ use Drupal\Core\Field\FieldTypePluginManager;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Extension\ModuleHandler; use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\KeyValueStore\KeyValueFactory;
use Drupal\recurring_events\Entity\EventInstance;
use Drupal\field_inheritance\Entity\FieldInheritanceInterface;
/** /**
* EventCreationService class. * EventCreationService class.
...@@ -79,6 +82,13 @@ class EventCreationService { ...@@ -79,6 +82,13 @@ class EventCreationService {
*/ */
protected $entityTypeManager; protected $entityTypeManager;
/**
* The key value storage service.
*
* @var \Drupal\Core\KeyValueStore\KeyValueFactory
*/
protected $keyValueStore;
/** /**
* Class constructor. * Class constructor.
* *
...@@ -98,8 +108,10 @@ class EventCreationService { ...@@ -98,8 +108,10 @@ class EventCreationService {
* The module handler service. * The module handler service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service. * The entity type manager service.
* @param \Drupal\Core\KeyValueStore\KeyValueFactory $key_value
* The key value storage service.
*/ */
public function __construct(TranslationInterface $translation, Connection $database, LoggerChannelFactoryInterface $logger, Messenger $messenger, FieldTypePluginManager $field_type_plugin_manager, EntityFieldManager $entity_field_manager, ModuleHandler $module_handler, EntityTypeManagerInterface $entity_type_manager) { public function __construct(TranslationInterface $translation, Connection $database, LoggerChannelFactoryInterface $logger, Messenger $messenger, FieldTypePluginManager $field_type_plugin_manager, EntityFieldManager $entity_field_manager, ModuleHandler $module_handler, EntityTypeManagerInterface $entity_type_manager, KeyValueFactory $key_value) {
$this->translation = $translation; $this->translation = $translation;
$this->database = $database; $this->database = $database;
$this->loggerFactory = $logger->get('recurring_events'); $this->loggerFactory = $logger->get('recurring_events');
...@@ -108,6 +120,7 @@ class EventCreationService { ...@@ -108,6 +120,7 @@ class EventCreationService {
$this->entityFieldManager = $entity_field_manager; $this->entityFieldManager = $entity_field_manager;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->keyValueStore = $key_value;
} }
/** /**
...@@ -491,6 +504,77 @@ class EventCreationService { ...@@ -491,6 +504,77 @@ class EventCreationService {
return $entity; return $entity;
} }
/**
* Configure the default field inheritances for event instances.
*
* @param Drupal\recurring_events\Entity\EventInstance $instance
* The event instance.
* @param int $series_id
* The event series entity ID.
*/
public function configureDefaultInheritances(EventInstance $instance, int $series_id = NULL) {
if (is_null($series_id)) {
$series_id = $instance->eventseries_id->target_id;
}
if (!empty($series_id)) {
// Configure the field inheritances for this instance.
$entity_type = $instance->getEntityTypeId();
$bundle = $instance->bundle();
$inherited_fields = $this->entityTypeManager->getStorage('field_inheritance')->loadByProperties([
'sourceEntityType' => 'eventseries',
'destinationEntityType' => $entity_type,
'destinationEntityBundle' => $bundle,
]);
if (!empty($inherited_fields)) {
$state_key = $entity_type . ':' . $instance->uuid();
$state = $this->keyValueStore->get('field_inheritance');
$state_values = $state->get($state_key);
if (empty($state_values)) {
$state_values = [
'enabled' => TRUE,
];
if (!empty($inherited_fields)) {
foreach ($inherited_fields as $inherited_field) {
$name = $inherited_field->idWithoutTypeAndBundle();
$state_values[$name] = [
'entity' => $series_id,
];
}
}
$state->set($state_key, $state_values);
}
}
}
}
/**
* When adding a new field inheritance, add the default values for it.
*
* @param Drupal\recurring_events\Entity\EventInstance $instance
* The event instance for which to configure default inheritance values.
* @param Drupal\field_inheritance\Entity\FieldInheritanceInterface $field_inheritance
* The field inheritance being created or updated.
*/
public function addNewDefaultInheritance(EventInstance $instance, FieldInheritanceInterface $field_inheritance) {
$state_key = 'eventinstance:' . $instance->uuid();
$state = $this->keyValueStore->get('field_inheritance');
$state_values = $state->get($state_key);
$name = $field_inheritance->idWithoutTypeAndBundle();
if (!empty($state_values[$name])) {
return;
}
$state_values[$name] = [
'entity' => $instance->eventseries_id->target_id,
];
$state->set($state_key, $state_values);
}
/** /**
* Get exclude/include dates from form. * Get exclude/include dates from form.
* *
......
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