dev/endpoints/bundle_AAAAA/src/Controller/NodeController.php
2024-01-16 09:54:33 +02:00

164 lines
5.1 KiB
PHP

<?php
namespace Drupal\bundle_article\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;
use Drupal\media\Plugin\media\Source;
use Drupal\media\Entity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TempStore;
class NodeController extends ControllerBase {
private const FIELD_CATEGORY = 'field_AAAAA_category'; // 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) {
// check if alias exist in path
if (empty($alias)) {
$this->errorAliasMissing();
return new JsonResponse($this->response, $this->statusCode);
}
// check if alias exist in drupal
$path_alias = '/'.$alias;
$path = \Drupal::service('path_alias.manager')->getPathByAlias($path_alias);
if ( $path_alias == $path) {
$this->errorNodeNotExist($alias, $path);
return new JsonResponse($this->response, $this->statusCode);
}
// check if node has translation
$nid = Url::fromUri('internal:' . $path_alias)->getRouteParameters()['node'];
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
if (! $node->hasTranslation($lang)) {
$this->errorTranslationNotExist($lang, $alias);
return new JsonResponse($this->response, $this->statusCode);
}
// build response
$this->buildNodeResponse($lang, $alias);
return new JsonResponse($this->response, $this->statusCode);
}
// Response
private function buildNodeResponse($lang, $alias) {
$path = \Drupal::service('path_alias.manager')->getPathByAlias($alias);
$nid = Url::fromUri('internal:/' . $path)->getRouteParameters()['node'];
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid)->getTranslation($lang);
$uid = $node->getOwnerId();
$user = \Drupal\user\Entity\User::load($uid);
$name = $user->getDisplayName();
$this->response = [
'code' => $this->statusCode,
'alias' => $alias,
'path' => $path,
'nid' => $nid,
'title' => $node->get('title')->value,
'body' => $node->get('body')->value,
'lang' => $node->get('langcode')->value,
'alias' => $node->get('path')->alias,
'created' => $node->get('created')->value,
'author' => $name,
// 'files' => $this->getFiles($node, 'field_file'),
// 'images' => $this->getImages($node, 'field_image'),
// 'category' => $this->getTerms($node, self::FIELD_CATEGORY)
// // single value taxonomy field
// 'tag' => (
// $node
// ->get('field_tags')
// ->entity)?\Drupal::service('entity.repository')
// ->getTranslationFromContext(
// $node->get('field_tags')->entity,
// $node->currentTranslation
// )->getName():''
];
}
// Multiple vallue 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, $path) {
$this->statusCode = self::HTTP_NOT_FOUND;
$this->response = [
'code' => $this->statusCode,
"message" => "Node with alias " . $alias . " does not exist." . $path
];
}
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);
}