better endpoints

This commit is contained in:
Ste Vaidis 2024-01-30 11:07:41 +02:00
parent 72c7d14df8
commit a9af7c00be

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\bundle_article\Controller;
namespace Drupal\endpoint_get_AAAAA\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;
@ -22,7 +22,6 @@ class NodesController extends ControllerBase {
* Url : optional URL argument - single value
* Example : /api/en/articles/1234
*/
// private const FIELD_CATEGORY = 'field_AAAAA_category'; // Editable
/* Used for : Taxonomy reference field
@ -30,7 +29,6 @@ class NodesController extends ControllerBase {
* Url : optional URL query params - accept multiple values
* Example : /api/en/articles/1234?subcategory=55,66,77
*/
// private const FIELD_SUBCATEGORY = 'field_AAAAA_subcategory'; // Editable
private const HTTP_BAD_REQUEST = 400;
@ -64,30 +62,60 @@ class NodesController extends ControllerBase {
$node = $node->getTranslation($lang);
$published = $node->get('status')->value;
/* get user name */
/* USER - get 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
);
/* BODY Field - 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
// );
/* LINK Field */
// $field_link = [];
// if ($node->hasField('field_link') && !$node->get('field_link')->isEmpty()) {
// $link_field = $node->get('field_link')->first();
// $url = $link_field->getUrl();
// $title = $link_field->getTitle();
// if ($url) {
// $field_link['url'] = $url->toString();
// }
// if ($title) {
// $field_link['title'] = $link_field->getTitle();
// } else {
// $field_link['title'] = $url->toString();;
// }
// }
/* GEOLOCATION Field */
// $field_geolocation = [];
// if ($node->hasField('field_geolocation') && !$node->get('field_geolocation')->isEmpty()) {
// $geolocation_field = $node->get('field_geolocation')->first();
// $latitude = $geolocation_field->lat;
// $longitude = $geolocation_field->lng;
// if ($latitude && $longitude) {
// $field_geolocation['lat'] = $latitude;
// $field_geolocation['lng'] = $longitude;
// }
// }
if ($published == 1) {
$nodes_response[] = [
'id' => $node->id(),
'title' => $node->getTitle(),
'body' => $body,
'lang' => $node->get('langcode')->value,
'created' => $node->get('created')->value,
'alias' => $node->get('path')->alias,
'author' => $name,
// 'field_geolocation' => $field_geolocation,
// 'field_link' => $field_link,
// 'body' => $body,
// 'file' => $this->getFiles($node, 'field_file'),
// 'image' => $this->getImages($node, 'field_image'),
// 'category_id' => (