currentUser = $current_user; $this->entityTypeManager = $entity_type_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->getParameter('serializer.formats'), $container->get('logger.channel.yufu_concept'), $container->get('current_user'), $container->get('entity_type.manager') ); } /** * Ustvari nov koncept. * * Logika: * 1. Zloadamo uporabnika preko emaila, ce ne obstaja, ga ustvarimo. * 2. Zloadamo koncept prek uuid, ce ne obstaja, da ustvarimo. * - avtor je zloadan prek maila * * @param \Symfony\Component\HttpFoundation\Request; * Post request. * * @return \Drupal\rest\ResourceResponse * Returns rest resource. */ public function post(Request $request) { $response_status['status'] = FALSE; $data = json_decode($request->getContent(), TRUE); // You must to implement the logic of your REST Resource here. // Use current user after pass authentication to validate access. if (!$this->currentUser->hasPermission('access content')) { throw new AccessDeniedHttpException(); } // Fields: naslov, text, povezani pojmi, email // Optional fields: language, related concept. if (empty($data['title']) || empty($data['text']) || empty($data['uuid'])) { throw new MissingDataException('Title, uuid or text missing.'); } $uid = $this->getUserIdByEmail($data['email'] ?? null); if ($concept = $this->getConceptFromUuid($data['uuid'])) { // Concept exists - create a new revision. $concept->setNewRevision(TRUE); $concept->setRevisionUserId($uid); $concept->set('title', $data['title']); $concept->set('body', $data['text'] ?? $concept->body->value); $concept->isDefaultRevision(FALSE); $concept->setRevisionLogMessage('New revision by concept endpoint.'); $concept->moderation_state->target_id = 'draft'; $concept->set('status', 0); $concept->setUnpublished(); $concept->save(); $this->logger->notice('New concept @title revision @revid created by uid @uid.', [ '@title' => $concept->getTitle(), '@revid' => $concept->getRevisionId(), '@uid' => $uid, ]); } else { // Concept does not exist - create a new node. $concept = [ 'type' => 'concept', 'title' => $data['title'], 'body' => $data['text'], 'uuid' => $data['uuid'], 'uid' => $uid, 'moderation_state' => 'draft', ]; // @TODO Check if related concepts are set and add them to the concept. // @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->save(); $this->logger->log(LogLevel::NOTICE, $this->t('Creating concept: @title', [ '@title' => $concept->getTitle(), ])); } $response_status['concept'] = [ 'title' => $concept->label(), 'id' => $concept->id(), 'uuid' => $concept->uuid(), 'revision_id' => $concept->getRevisionId(), 'revision_create_time' => $concept->getRevisionCreationTime(), 'revision_uid' => $concept->getRevisionUserId(), 'uid' => $concept->uid->target_id, ]; $response = new ResourceResponse($response_status); return $response; } /** * Get concept from uuid. * * If concept is new, return null. * * @param string|null $uuid * Uuid of the node. * * @return \Drupal\node\NodeInterface|null */ protected function getConceptFromUuid(?string $uuid): ?NodeInterface { if (!$uuid) { return NULL; } $node_storage = $this->entityTypeManager->getStorage('node'); if ($concept = end($node_storage->loadByProperties(['uuid' => $uuid]))) { return $concept; } return NULL; } /** * Loads user from email or creates a new blocked user. * * Side effect is to create a new user. * * @param string|null $email * Email provided by the fe call to endpoint. * * @return int * Id of the user. * */ protected function getUserIdByEmail(?string $email): int { if (!$email) { return 0; } // Load user by email. If it doesn't exist, create one. $user_storage = $this->entityTypeManager->getStorage('user'); if ($user = end($user_storage->loadByProperties(['mail' => $email]))) { return $user->id(); } $user = [ 'mail' => $email, 'name' => $email, 'status' => 0, ]; $new_user = $user_storage->create($user); $new_user->save(); return $new_user->id(); } }