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

31-related-concepts
Lio Novelli 2023-12-02 21:57:40 +01:00 committed by Jurij Podgoršek
parent 1baea2c7d3
commit 6c93b8ac93
1 changed files with 27 additions and 0 deletions

View File

@ -117,6 +117,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);
@ -128,6 +129,7 @@ class AddConcept extends ResourceBase {
$concept->moderation_state->target_id = 'draft'; $concept->moderation_state->target_id = 'draft';
$concept->set('status', 0); $concept->set('status', 0);
$concept->setUnpublished(); $concept->setUnpublished();
$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(),
@ -149,6 +151,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(),
@ -219,4 +222,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;
}
} }