From 6f67a4e198210d58f3de6cd94db1df8bb9d84d75 Mon Sep 17 00:00:00 2001 From: Lio Novelli Date: Sat, 2 Dec 2023 21:57:40 +0100 Subject: [PATCH] DIY: Add template method to parse request data related concepts. --- .../src/Plugin/rest/resource/AddConcept.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/web/modules/custom/yufu_concept/src/Plugin/rest/resource/AddConcept.php b/web/modules/custom/yufu_concept/src/Plugin/rest/resource/AddConcept.php index 258d09d..9fd11f6 100644 --- a/web/modules/custom/yufu_concept/src/Plugin/rest/resource/AddConcept.php +++ b/web/modules/custom/yufu_concept/src/Plugin/rest/resource/AddConcept.php @@ -118,6 +118,7 @@ class AddConcept extends ResourceBase { throw new MissingDataException('Title, uuid or text missing.'); } $uid = $this->getUserIdByEmail($data['email'] ?? null); + $relatedConcepts = $this->parseReleatedConcepts($data['related_concepts'] ?? []); if ($concept = $this->getConceptFromUuid($data['uuid'])) { // Concept exists - create a new revision. $concept->setNewRevision(TRUE); @@ -127,6 +128,7 @@ class AddConcept extends ResourceBase { $concept->isDefaultRevision(FALSE); $concept->setRevisionLogMessage('New revision by concept endpoint.'); $concept->moderation_state->target_id = 'draft'; + $concept->set('field_related_concept', $relatedConcepts); $concept->save(); $this->logger->notice('New concept @title revision @revid created by uid @uid.', [ '@title' => $concept->getTitle(), @@ -148,6 +150,7 @@ class AddConcept extends ResourceBase { // @TODO Check language and add set it on concept if exists. /** @var \Drupal\node\Entity\NodeInterface $concept */ $concept = $this->entityTypeManager->getStorage('node')->create($concept); + $concept->set('field_related_concept', $relatedConcepts); $concept->save(); $this->logger->log(LogLevel::NOTICE, $this->t('Creating concept: @title', [ '@title' => $concept->getTitle(), @@ -218,4 +221,28 @@ class AddConcept extends ResourceBase { return $new_user->id(); } + /** + * Parses the data from post request. + * + * @param array|null $parseReleatedConcepts + * Data provided by post request. + * + * @return array + * Array of concept nodes in format suitable to set on node. + */ + protected function parseReleatedConcepts(?array $relatedConcepts): array { + // @TODO Specify how array of related concepts comes from the fe request. + // We can do some quick check if related concepts exists. + $result = []; + if ($relatedConcepts) { + foreach ($relatedConcepts as $concept) { + // @TODO Validate concept and get its id. + $result[] = [ + 'target_id' => $conceptId, + ]; + } + } + return $result; + } + }