2024-02-20 10:30:02 +02:00

203 lines
5.9 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;
class NodeController extends ControllerBase {
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 = '/'.$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();
/* absolute urls for body inline images */
// $base_url = Url::fromRoute('<current>')->setAbsolute()->toString();
// $url_components = parse_url($base_url);
// $domain = $url_components['scheme'].'://'.$url_components['host'].':'.$url_components['port'];
// $body = str_replace(
// '/sites/default/',
// $domain.'/sites/default/',
// $node->get('body')->value
// );
// $manager = '';
// if ($node->hasField('manager') && !$node->get('manager')->isEmpty()) {
// $manager = $node->get('manager')->value;
// }
$this->response = [
'code' => $this->statusCode,
'alias' => $alias,
// 'path' => $path,
'nid' => $nid,
'author' => $name,
'title' => $node->get('title')->value,
// 'manager' => $manager,
// 'body' => $node->get('body')->value,
// 'body' => $body,
// 'lang' => $node->get('langcode')->value,
// 'alias' => $node->get('path')->alias,
// 'files' => $this->getFiles($node, 'field_file'),
// 'images' => $this->getImages($node, 'field_image'),
// 'category' => (
// $node
// ->get(self::FIELD_CATEGORY)
// ->entity)?\Drupal::service('entity.repository')
// ->getTranslationFromContext(
// $node->get(self::FIELD_CATEGORY)->entity,
// $node->currentTranslation
// )->getName():'',
// 'subcategory' => $this->getTerms($node, self::FIELD_SUBCATEGORY)
'created' => $node->get('created')->value
];
}
/*
*
* Multiple value fields
*
*/
// private function getTerms(Node $node, string $field): array {
// $terms = $node->get($field)->referencedEntities();
// $response = [];
// foreach ($terms as $term) {
// $name = $term->getName();
// $tid = $term->id();
// $response[] = array(
// 'name' => $name,
// 'id' => $tid
// );
// }
// return $response;
// }
// private function getFiles(Node $node, string $field): array {
// $uris = [];
// foreach ($node->get($field) as $value) {
// $file = \Drupal::entityTypeManager()
// ->getStorage('file')
// ->load($value->getValue()['target_id']);
// $url = \Drupal::service('file_url_generator')
// ->generateAbsoluteString($file->getFileUri());
// $uris[] = array("url" => $url);
// }
// return $uris;
// }
// private function getImages(Node $node, string $field): array {
// $uris = [];
// foreach ($node->get($field)->getValue() as $value) {
// $file = \Drupal::entityTypeManager()
// ->getStorage('file')
// ->load($value['target_id']);
// $url = \Drupal::service('file_url_generator')
// ->generateAbsoluteString($file->getFileUri());
// $uris[] = array("url" => $url, "alt" => $value['alt']);
// }
// return $uris;
// }
/*
*
* 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);
}