From f17eb0c1540136d376102bcda17da8e579b36bbc Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 8 May 2020 11:43:28 +0100 Subject: [PATCH] Include event information in migrated events --- .../migrate_plus.migration.talk_node.yml | 6 +- .../Plugin/migrate/destination/OpdTalk.php | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 web/modules/custom/custom/src/Plugin/migrate/destination/OpdTalk.php diff --git a/web/modules/custom/custom/migrations/migrate_plus.migration.talk_node.yml b/web/modules/custom/custom/migrations/migrate_plus.migration.talk_node.yml index 850bb28..c08b6bd 100644 --- a/web/modules/custom/custom/migrations/migrate_plus.migration.talk_node.yml +++ b/web/modules/custom/custom/migrations/migrate_plus.migration.talk_node.yml @@ -42,6 +42,10 @@ source: selector: speakerdeck label: Speakerdeck + - name: events + selector: events + label: Events + process: body/0/format: plugin: default_value @@ -72,7 +76,7 @@ process: default_value: 1 destination: - plugin: 'entity:node' + plugin: opd_talk migration_dependencies: required: { } diff --git a/web/modules/custom/custom/src/Plugin/migrate/destination/OpdTalk.php b/web/modules/custom/custom/src/Plugin/migrate/destination/OpdTalk.php new file mode 100644 index 0000000..01f4f8c --- /dev/null +++ b/web/modules/custom/custom/src/Plugin/migrate/destination/OpdTalk.php @@ -0,0 +1,67 @@ +getDestination(); + + if ($nodes = $this->storage->loadByProperties(['title' => $data['title']])) { + $node = current($nodes); + } + else { + $node = $this->storage->create($data); + } + + $eventData = $row->getSourceProperty('events'); + $this->createEventParagraphs($node, $eventData); + + $node->save(); + + return [$node->id()]; + } + + private function createEventParagraphs(EntityInterface $node, array $eventData): void { + Collection::make($eventData)->map(function (array $event): array { + $paragraph = Paragraph::create([ + 'field_date' => DrupalDateTime::createFromTimestamp($event['date']) + ->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), + 'field_link' => $event['url'], + 'field_location' => $event['location'], + 'field_name' => $event['name'], + 'field_remote' => $event['remote'] == 'true' ? 1 : 0, + 'type' => 'event', + ]); + + $paragraph->save(); + + return [ + 'target_id' => $paragraph->id(), + 'target_revision_id' => $paragraph->getRevisionId(), + ]; + })->pipe(function (Collection $events) use ($node) { + $node->set('field_events', $events->toArray()); + $node->save(); + }); + } + +}