#23: Ogrodje za endpoint za dodajanje konceptov.
parent
62385a8622
commit
da9ac824c9
|
@ -0,0 +1,163 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\yufu_concept\src\Plugin\rest\resource;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\TypedData\Exception\MissingDataException;
|
||||
use Drupal\rest\Plugin\ResourceBase;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Mail\MailManagerInterface;
|
||||
use Drupal\rest\ResourceResponse;
|
||||
use Drupal\Core\Session\AccountProxyInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates post endpoint to create new concept.
|
||||
*
|
||||
* @RestResource(
|
||||
* id = "add_concept_rest_resource",
|
||||
* label = @Translation("Create or edit a concept"),
|
||||
* uri_paths = {
|
||||
* "canonical" = "/api/pojem/dodaj"
|
||||
* }
|
||||
*/
|
||||
class AddConcept extends ResourceBase {
|
||||
|
||||
/**
|
||||
* A current user instance.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountProxyInterface
|
||||
*/
|
||||
protected $currentUser;
|
||||
|
||||
/**
|
||||
* Mail manager service.
|
||||
*
|
||||
* @var \Drupal\Core\Mail\MailManagerInterface
|
||||
*/
|
||||
protected $mailManager;
|
||||
|
||||
/**
|
||||
* Site configs.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ImmutableConfig
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Entity type manager service.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* Constructs a Drupal\rest\Plugin\ResourceBase object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param array $serializer_formats
|
||||
* The available serialization formats.
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
* A logger instance.
|
||||
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
|
||||
* A current user instance.
|
||||
* @param \Drupal\Core\Mail\MailManagerInterface $mailManager
|
||||
* Mail manager service.
|
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
|
||||
* Config factory service.
|
||||
*/
|
||||
public function __construct(
|
||||
array $configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
array $serializer_formats,
|
||||
LoggerInterface $logger,
|
||||
AccountProxyInterface $current_user,
|
||||
MailManagerInterface $mailManager,
|
||||
ConfigFactoryInterface $configFactory,
|
||||
EntityTypeManagerInterface $entity_type_manager) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
|
||||
$this->currentUser = $current_user;
|
||||
$this->mailManager = $mailManager;
|
||||
$this->config = $configFactory->get('system.site');
|
||||
$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.factory')->get('custom_rest'),
|
||||
$container->get('current_user'),
|
||||
$container->get('plugin.manager.mail'),
|
||||
$container->get('config.factory'),
|
||||
$container->get('entity_type.manager')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a test email.
|
||||
*
|
||||
* @param $data
|
||||
* Post date.
|
||||
*
|
||||
* @return \Drupal\rest\ResourceResponse
|
||||
* Returns rest resource.
|
||||
*/
|
||||
public function post($data) {
|
||||
$response_status['status'] = FALSE;
|
||||
// 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'])) {
|
||||
throw new MissingDataException('Title or text missing.');
|
||||
}
|
||||
$concept = [
|
||||
'type' => 'concept',
|
||||
'title' => $data['title'],
|
||||
'body' => $data['text'],
|
||||
];
|
||||
// @TODO Check if related concepts are set and add them to the concept.
|
||||
// @TODO Check language and add set it on concept if exists.
|
||||
$concept = $this->entityTypeManger->getStorage('node')->create($concept);
|
||||
$concept->save();
|
||||
|
||||
if (!empty($data['email'])) {
|
||||
// @TODO Poslji mail uporabniku, da se lahko registrira.
|
||||
$site_email = $this->config->get('mail');
|
||||
$module = 'yufu_concept';
|
||||
$key = 'notice';
|
||||
$to = $site_email;
|
||||
// Send email to user to make him register to the website.
|
||||
$params['message'] = $data['message']['value'];
|
||||
$params['title'] = $data['subject']['value'];
|
||||
$params['from'] = $data['email']['value'];
|
||||
$langcode = $data['lang']['value'];
|
||||
$send = TRUE;
|
||||
|
||||
$result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
|
||||
$response_status['status'] = $result['result'];
|
||||
}
|
||||
$response = new ResourceResponse($response_status);
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
# _*_ restclient _*_
|
||||
|
||||
# Makes post http request to a resource from yufu_concept module.
|
||||
|
||||
POST https://yufu-manifest.ddev.site/api/pojem/dodaj
|
||||
User-Agent: Emacs Restclient
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"title": "Emacs restclient test",
|
||||
"body": "O wau. Emacs restclient. How nice."
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
name: Yufu Concept
|
||||
type: module
|
||||
description: Provides concept content type related functionality.
|
||||
package: Yufu
|
||||
core_version_requirement: ^9 || ^10
|
||||
dependencies:
|
||||
- drupal:rest
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Primary module hooks for Yufu Concept module.
|
||||
*/
|
Loading…
Reference in New Issue