2024-04-12 17:07:04 +03:00

151 lines
4.3 KiB
PHP

<?php
namespace Drupal\endpoint_get_AAAAA\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\TempStore;
use Drupal\Core\Url;
use Drupal\media\Plugin\media\Source;
use Drupal\media\Entity;
use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeController extends ControllerBase implements ContainerInjectionInterface {
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('file_url_generator')
);
}
protected $entityTypeManager;
protected $fileUrlGenerator;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
FileUrlGeneratorInterface $fileUrlGenerator
) {
$this->entityTypeManager = $entityTypeManager;
$this->fileUrlGenerator = $fileUrlGenerator;
}
private const FIELD_CATEGORY = 'field_AAAAA_category'; // Editable
private const FIELD_SUBCATEGORY = 'field_AAAAA_subcategory'; // Editable
private const HTTP_BAD_REQUEST = 400;
private const HTTP_NOT_FOUND = 404;
private const HTTP_OK = 200;
private array $response = [];
private int $statusCode = self::HTTP_OK;
/*
* Main
*/
public function getNode(Request $request, $lang, $alias) {
/* Quit if alias not exist in path */
if (empty($alias)) {
$this->errorAliasMissing();
return new JsonResponse($this->response, $this->statusCode);
}
/* Get unaliased path */
$path_alias = '/AAAAA/'.$alias;
$path = \Drupal::service('path_alias.repository')->lookupByAlias($path_alias, $lang);
/* Quit if path does not exist in drupal */
if (is_null($path)) {
$this->errorNodeNotExist($alias);
return new JsonResponse($this->response, $this->statusCode);
}
/* Get the nid from unaliased path */
$path = $path['path'];
$nid = explode('/',$path)[2];
/* Get the node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
/* Quit if node has no tranlation */
if (! $node->hasTranslation($lang)) {
$this->errorTranslationNotExist($lang, $alias);
return new JsonResponse($this->response, $this->statusCode);
}
/* Build the response */
$this->buildNodeResponse($lang, $alias, $nid);
return new JsonResponse($this->response, $this->statusCode);
}
/*
* Node Response
*/
private function buildNodeResponse($lang, $alias, $nid) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid)->getTranslation($lang);
/* user name */
$uid = $node->getOwnerId();
$user = \Drupal\user\Entity\User::load($uid);
$name = $user->getDisplayName();
// AUTO_ADD_CODE_BELLOW_extractor
$this->response = [
'code' => $this->statusCode,
'alias' => '/AAAAA/'.$alias,
'nid' => intval($nid),
'author' => $name,
'title' => $node->get('title')->value,
// AUTO_ADD_CODE_BELLOW_response
'created' => $node->get('created')->value
];
}
/*
*
* Multiple value fields
*
*/
// AUTO_ADD_CODE_BELLOW_splitter
/*
*
* Error functions
*
*/
private function errorNodeNotExist($alias) {
$this->statusCode = self::HTTP_NOT_FOUND;
$this->response = [
'code' => $this->statusCode,
"message" => "Node with alias " . $alias . " does not exist."
];
}
private function errorTranslationNotExist($lang, $alias) {
$this->statusCode = self::HTTP_NOT_FOUND;
$this->response = [
'code' => $this->statusCode,
"message" => "Node with alias " . $alias . " does not have a translation for language code " . $lang
];
}
private function errorAliasMissing() {
$this->statusCode = self::HTTP_BAD_REQUEST;
$this->response = [
'code' => $this->statusCode,
'message' => 'Query parameter "alias" is mandatory and is missing.'
];
}
// \Drupal::logger('hello')->notice(''.$nid);
}