DIY: Add template method to parse request data related concepts. #34
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue