View source
<?php
namespace Drupal\rest\Plugin\views\display;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\State\StateInterface;
use Drupal\views\Plugin\views\display\ResponseDisplayPluginInterface;
use Drupal\views\Render\ViewsRenderPipelineMarkup;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\PathPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouteCollection;
class RestExport extends PathPluginBase implements ResponseDisplayPluginInterface {
protected $usesAJAX = FALSE;
protected $usesPager = FALSE;
protected $usesMore = FALSE;
protected $usesAreas = FALSE;
protected $usesOptions = FALSE;
protected $contentType = 'json';
protected $mimeType;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, StateInterface $state, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider, $state);
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('router.route_provider'), $container
->get('state'), $container
->get('renderer'));
}
public function initDisplay(ViewExecutable $view, array &$display, array &$options = NULL) {
parent::initDisplay($view, $display, $options);
$request_content_type = $this->view
->getRequest()
->getRequestFormat();
if ($request_content_type != 'html') {
$this
->setContentType($request_content_type);
}
elseif (!empty($options['style']['options']['formats']) && !isset($options['style']['options']['formats'][$this
->getContentType()])) {
$this
->setContentType(reset($options['style']['options']['formats']));
}
$this
->setMimeType($this->view
->getRequest()
->getMimeType($this->contentType));
}
public function getType() {
return 'data';
}
public function usesExposed() {
return TRUE;
}
public function displaysExposed() {
return FALSE;
}
public function setMimeType($mime_type) {
$this->mimeType = $mime_type;
}
public function getMimeType() {
return $this->mimeType;
}
public function setContentType($content_type) {
$this->contentType = $content_type;
}
public function getContentType() {
return $this->contentType;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['style']['contains']['type']['default'] = 'serializer';
$options['row']['contains']['type']['default'] = 'data_entity';
$options['defaults']['default']['style'] = FALSE;
$options['defaults']['default']['row'] = FALSE;
unset($options['exposed_form']);
unset($options['exposed_block']);
unset($options['css_class']);
return $options;
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
unset($categories['page'], $categories['exposed']);
unset($options['show_admin_links'], $options['analyze-theme']);
$categories['path'] = array(
'title' => $this
->t('Path settings'),
'column' => 'second',
'build' => array(
'#weight' => -10,
),
);
$options['path']['category'] = 'path';
$options['path']['title'] = $this
->t('Path');
unset($options['exposed_form']);
unset($options['exposed_block']);
unset($options['css_class']);
}
public function collectRoutes(RouteCollection $collection) {
parent::collectRoutes($collection);
$view_id = $this->view->storage
->id();
$display_id = $this->display['id'];
if ($route = $collection
->get("view.{$view_id}.{$display_id}")) {
$style_plugin = $this
->getPlugin('style');
$route
->setMethods([
'GET',
]);
if ($formats = $style_plugin
->getFormats()) {
$route
->setRequirement('_format', implode('|', $formats + [
'html',
]));
}
}
}
public static function buildResponse($view_id, $display_id, array $args = []) {
$build = static::buildBasicRenderable($view_id, $display_id, $args);
$renderer = \Drupal::service('renderer');
$output = $renderer
->renderRoot($build);
$response = new CacheableResponse($output, 200);
$cache_metadata = CacheableMetadata::createFromRenderArray($build);
$response
->addCacheableDependency($cache_metadata);
$response->headers
->set('Content-type', $build['#content_type']);
return $response;
}
public function execute() {
parent::execute();
return $this->view
->render();
}
public function render() {
$build = array();
$build['#markup'] = $this->renderer
->executeInRenderContext(new RenderContext(), function () {
return $this->view->style_plugin
->render();
});
$this->view->element['#content_type'] = $this
->getMimeType();
$this->view->element['#cache_properties'][] = '#content_type';
if (!empty($this->view->live_preview)) {
$build['#prefix'] = '<pre>';
$build['#plain_text'] = $build['#markup'];
$build['#suffix'] = '</pre>';
unset($build['#markup']);
}
elseif ($this->view
->getRequest()
->getFormat($this->view->element['#content_type']) !== 'html') {
$build['#markup'] = ViewsRenderPipelineMarkup::create($build['#markup']);
}
parent::applyDisplayCachablityMetadata($build);
return $build;
}
public function preview() {
return $this->view
->render();
}
}