DIY: Add template method to parse request data related concepts.

pull/34/head
Lio Novelli 2023-12-02 21:57:40 +01:00
parent 44fba60a76
commit 6f67a4e198
1 changed files with 27 additions and 0 deletions

View File

@ -118,6 +118,7 @@ class AddConcept extends ResourceBase {
throw new MissingDataException('Title, uuid or text missing.'); throw new MissingDataException('Title, uuid or text missing.');
} }
$uid = $this->getUserIdByEmail($data['email'] ?? null); $uid = $this->getUserIdByEmail($data['email'] ?? null);
$relatedConcepts = $this->parseReleatedConcepts($data['related_concepts'] ?? []);
if ($concept = $this->getConceptFromUuid($data['uuid'])) { if ($concept = $this->getConceptFromUuid($data['uuid'])) {
// Concept exists - create a new revision. // Concept exists - create a new revision.
$concept->setNewRevision(TRUE); $concept->setNewRevision(TRUE);
@ -127,6 +128,7 @@ class AddConcept extends ResourceBase {
$concept->isDefaultRevision(FALSE); $concept->isDefaultRevision(FALSE);
$concept->setRevisionLogMessage('New revision by concept endpoint.'); $concept->setRevisionLogMessage('New revision by concept endpoint.');
$concept->moderation_state->target_id = 'draft'; $concept->moderation_state->target_id = 'draft';
$concept->set('field_related_concept', $relatedConcepts);
$concept->save(); $concept->save();
$this->logger->notice('New concept @title revision @revid created by uid @uid.', [ $this->logger->notice('New concept @title revision @revid created by uid @uid.', [
'@title' => $concept->getTitle(), '@title' => $concept->getTitle(),
@ -148,6 +150,7 @@ class AddConcept extends ResourceBase {
// @TODO Check language and add set it on concept if exists. // @TODO Check language and add set it on concept if exists.
/** @var \Drupal\node\Entity\NodeInterface $concept */ /** @var \Drupal\node\Entity\NodeInterface $concept */
$concept = $this->entityTypeManager->getStorage('node')->create($concept); $concept = $this->entityTypeManager->getStorage('node')->create($concept);
$concept->set('field_related_concept', $relatedConcepts);
$concept->save(); $concept->save();
$this->logger->log(LogLevel::NOTICE, $this->t('Creating concept: @title', [ $this->logger->log(LogLevel::NOTICE, $this->t('Creating concept: @title', [
'@title' => $concept->getTitle(), '@title' => $concept->getTitle(),
@ -218,4 +221,28 @@ class AddConcept extends ResourceBase {
return $new_user->id(); 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;
}
} }