Created
February 28, 2022 07:12
-
-
Save vishnukumarpv/b676fc6e8244439ff18b723b30becdbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
codimth.json_api_articles: | |
path: '/api/articles' | |
defaults: | |
_controller: 'Drupal\mymodule\Controller\TestController::index' | |
_title: 'Codimth JSON api' | |
methods: [GET] | |
requirements: | |
_access: 'TRUE' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\mymodule\Controller; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
/** | |
* Class JsonApiArticlesController | |
* @package Drupal\mymodule\Controller | |
*/ | |
class JsonApiArticlesController { | |
/** | |
* @return JsonResponse | |
*/ | |
public function index() { | |
return new JsonResponse([ 'data' => $this->getData(), 'method' => 'GET', 'status'=> 200]); | |
} | |
/** | |
* @return array | |
*/ | |
public function getData() { | |
$result=[]; | |
$query = \Drupal::entityQuery('node') | |
->condition('type', 'article') | |
->sort('title', 'DESC'); | |
$nodes_ids = $query->execute(); | |
if ($nodes_ids) { | |
foreach ($nodes_ids as $node_id) { | |
$node = \Drupal\node\Entity\Node::load($node_id); | |
$result[] = [ | |
"id" => $node->id(), | |
"title" => $node->getTitle(), | |
]; | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment