diff --git a/modules/newsroom_connector_item/newsroom_connector_item.install b/modules/newsroom_connector_item/newsroom_connector_item.install index a80ade2..acea607 100644 --- a/modules/newsroom_connector_item/newsroom_connector_item.install +++ b/modules/newsroom_connector_item/newsroom_connector_item.install @@ -111,6 +111,17 @@ function newsroom_connector_item_update_9004(&$sandbox) { _newsroom_connector_item_install_field('field_newsroom_is_machine_trans'); } +/** + * Make documents field translatable. + */ +function newsroom_connector_item_update_9005(&$sandbox) { + $fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'newsroom_item'); + $field_config = $fields['field_newsroom_documents']->getConfig('newsroom_item'); + $field_config + ->setTranslatable(TRUE) + ->save(); +} + /** * Install field from module config files. * diff --git a/modules/newsroom_connector_item/src/EventSubscriber/NewsroomItemMigrateSubscriber.php b/modules/newsroom_connector_item/src/EventSubscriber/NewsroomItemMigrateSubscriber.php new file mode 100644 index 0000000..ff857db --- /dev/null +++ b/modules/newsroom_connector_item/src/EventSubscriber/NewsroomItemMigrateSubscriber.php @@ -0,0 +1,83 @@ +dispatcher = $dispatcher; + $this->entityData = $entity_data; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events = []; + $events[MigrateEvents::POST_ROW_SAVE][] = ['updateDocuments']; + return $events; + } + + /** + * Event callback to sync source and destination. + * + * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event + * The migration import event. + */ + public function updateDocuments(MigratePostRowSaveEvent $event) { + $migration_id = $event->getMigration()->id(); + if (strpos($migration_id, 'newsroom_item_translations') !== FALSE || $migration_id == NewsroomItemNewsroomProcessor::MIGRATION_ITEM) { + $nid = $event->getDestinationIdValues()[0] ?? NULL; + if (empty($nid)) { + return; + } + $row = $event->getRow(); + $related_documents_urls = $row->getSourceProperty('related_documents_url'); + $related_documents_machine_translations = $row->getSourceProperty('related_documents_machine_translation'); + + if (!empty($related_documents_urls)) { + if (is_array($related_documents_urls)) { + foreach ($related_documents_urls as $key => $item) { + $this->entityData->set('newsroom_connector_item', $nid, $related_documents_urls[$key], 'node', $related_documents_machine_translations[$key] == 'True' ? 1 : 0); + } + } + else { + $this->entityData->set('newsroom_connector_item', $nid, $related_documents_urls, 'node', $related_documents_machine_translations == 'True' ? 1 : 0); + } + } + } + } + +}