MonitoringMultigraphResource.php in Monitoring 8
File
modules/multigraph/src/Plugin/rest/resource/MonitoringMultigraphResource.php
View source
<?php
namespace Drupal\monitoring_multigraph\Plugin\rest\resource;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Route;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
class MonitoringMultigraphResource extends ResourceBase {
public function routes() {
$path_prefix = strtr($this->pluginId, ':', '/');
$route_name = strtr($this->pluginId, ':', '.');
$collection = parent::routes();
$route = new Route("/{$path_prefix}", array(
'_controller' => 'Drupal\\rest\\RequestHandler::handle',
'_plugin' => $this->pluginId,
), array(
'_permission' => "restful get {$this->pluginId}",
));
$route
->setMethods([
'GET',
]);
foreach ($this->serializerFormats as $format_name) {
$format_route = clone $route;
$format_route
->addRequirements(array(
'_format' => $format_name,
));
$collection
->add("{$route_name}.list.{$format_name}", $format_route);
}
return $collection;
}
public function get($id = NULL) {
$request = \Drupal::request();
$format = $request
->getRequestFormat('ĵson');
if ($id) {
$multigraph = \Drupal::entityTypeManager()
->getStorage('monitoring_multigraph')
->load($id);
if ($multigraph == NULL) {
throw new NotFoundHttpException('No multigraph with name "' . $id . '"');
}
$response = $multigraph
->getDefinition();
$url = Url::fromRoute('rest.monitoring-multigraph.GET', [
'id' => $id,
'_format' => $format,
])
->setAbsolute()
->toString(TRUE);
$response['uri'] = $url
->getGeneratedUrl();
$response = new ResourceResponse($response);
$response
->addCacheableDependency($multigraph);
$response
->addCacheableDependency($url);
return $response;
}
$list = array();
$multigraphs = \Drupal::entityTypeManager()
->getStorage('monitoring_multigraph')
->loadMultiple();
$cacheable_metadata = new CacheableMetadata();
foreach ($multigraphs as $name => $multigraph) {
$list[$name] = $multigraph
->getDefinition();
$url = Url::fromRoute('rest.monitoring-multigraph.GET', [
'id' => $name,
'_format' => $format,
])
->setAbsolute()
->toString(TRUE);
$list[$name]['uri'] = $url
->getGeneratedUrl();
$cacheable_metadata = $cacheable_metadata
->merge($url);
$cacheable_metadata = $cacheable_metadata
->merge(CacheableMetadata::createFromObject($multigraph));
}
$response = new ResourceResponse($list);
return $response
->addCacheableDependency($cacheable_metadata);
}
}