Newer
Older
<?php
/**
* @file
* Contains \Drupal\rest\Plugin\views\display\RestExport.
*/
namespace Drupal\rest\Plugin\views\display;
Alex Pott
committed
Alex Pott
committed
use Drupal\Core\State\StateInterface;
Angie Byron
committed
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\ContentNegotiation;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\PathPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Alex Pott
committed
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouteCollection;
/**
* The plugin that handles Data response callbacks for REST resources.
*
* @ingroup views_display_plugins
*
Alex Pott
committed
* @ViewsDisplay(
* id = "rest_export",
* title = @Translation("REST export"),
* help = @Translation("Create a REST export resource."),
Alex Pott
committed
* uses_route = TRUE,
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
* admin = @Translation("REST export")
* )
*/
class RestExport extends PathPluginBase {
/**
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAJAX.
*/
protected $usesAJAX = FALSE;
/**
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesPager.
*/
protected $usesPager = FALSE;
/**
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesMore.
*/
protected $usesMore = FALSE;
/**
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAreas.
*/
protected $usesAreas = FALSE;
/**
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAreas.
*/
protected $usesOptions = FALSE;
/**
* Overrides the content type of the data response, if needed.
*
* @var string
*/
Alex Pott
committed
protected $contentType = 'json';
/**
* The mime type for the response.
*
* @var string
*/
protected $mimeType;
/**
* The content negotiation library.
*
* @var \Drupal\Core\ContentNegotiation
*/
protected $contentNegotiation;
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs a Drupal\rest\Plugin\ResourceBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
Angie Byron
committed
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider
Alex Pott
committed
* @param \Drupal\Core\State\StateInterface $state
Angie Byron
committed
* The state key value store.
* @param \Drupal\Core\ContentNegotiation $content_negotiation
* The content negotiation library.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, StateInterface $state, ContentNegotiation $content_negotiation, Request $request) {
Angie Byron
committed
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider, $state);
$this->contentNegotiation = $content_negotiation;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
Angie Byron
committed
$container->get('router.route_provider'),
$container->get('state'),
$container->get('content_negotiation'),
$container->get('request')
);
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function initDisplay(ViewExecutable $view, array &$display, array &$options = NULL) {
parent::initDisplay($view, $display, $options);
$request_content_type = $this->contentNegotiation->getContentType($this->request);
Alex Pott
committed
// Only use the requested content type if it's not 'html'. If it is then
// default to 'json' to aid debugging.
Alex Pott
committed
// @todo Remove the need for this when we have better content negotiation.
if ($request_content_type != 'html') {
Alex Pott
committed
$this->setContentType($request_content_type);
}
$this->setMimeType($this->request->getMimeType($this->contentType));
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
protected function getType() {
return 'data';
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function usesExposed() {
return FALSE;
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function displaysExposed() {
return FALSE;
}
/**
* Sets the request content type.
*
* @param string $mime_type
* The response mime type. E.g. 'application/json'.
*/
public function setMimeType($mime_type) {
$this->mimeType = $mime_type;
}
/**
* Gets the mime type.
*
Alex Pott
committed
* This will return any overridden mime type, otherwise returns the mime type
* from the request.
*
* @return string
* The response mime type. E.g. 'application/json'.
*/
public function getMimeType() {
return $this->mimeType;
}
/**
* Sets the content type.
*
* @param string $content_type
* The content type machine name. E.g. 'json'.
*/
public function setContentType($content_type) {
$this->contentType = $content_type;
}
/**
* Gets the content type.
*
* @return string
* The content type machine name. E.g. 'json'.
*/
public function getContentType() {
return $this->contentType;
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
// Set the default style plugin to 'json'.
$options['style']['contains']['type']['default'] = 'serializer';
$options['row']['contains']['type']['default'] = 'data_entity';
$options['defaults']['default']['style'] = FALSE;
$options['defaults']['default']['row'] = FALSE;
// Remove css/exposed form settings, as they are not used for the data display.
unset($options['exposed_form']);
unset($options['exposed_block']);
unset($options['css_class']);
return $options;
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
unset($categories['page'], $categories['exposed']);
// Hide some settings, as they aren't useful for pure data output.
Angie Byron
committed
unset($options['show_admin_links'], $options['analyze-theme']);
$categories['path'] = array(
'title' => t('Path settings'),
'column' => 'second',
'build' => array(
'#weight' => -10,
),
);
$options['path']['category'] = 'path';
$options['path']['title'] = t('Path');
// Remove css/exposed form settings, as they are not used for the data
// display.
unset($options['exposed_form']);
unset($options['exposed_block']);
unset($options['css_class']);
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function collectRoutes(RouteCollection $collection) {
parent::collectRoutes($collection);
$view_id = $this->view->storage->id();
$display_id = $this->display['id'];
Alex Pott
committed
if ($route = $collection->get("view.$view_id.$display_id")) {
$style_plugin = $this->getPlugin('style');
// REST exports should only respond to get methods.
$requirements = array('_method' => 'GET');
Alex Pott
committed
// Format as a string using pipes as a delimeter.
$requirements['_format'] = implode('|', $style_plugin->getFormats());
// Add the new requirements to the route.
Alex Pott
committed
$route->addRequirements($requirements);
}
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function execute() {
parent::execute();
Angie Byron
committed
$output = $this->view->render();
return new Response(drupal_render($output), 200, array('Content-type' => $this->getMimeType()));
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function render() {
Angie Byron
committed
$build = array();
$build['#markup'] = $this->view->style_plugin->render();
Angie Byron
committed
// Wrap the output in a pre tag if this is for a live preview.
if (!empty($this->view->live_preview)) {
Angie Byron
committed
$build['#prefix'] = '<pre>';
Angie Byron
committed
$build['#markup'] = check_plain($build['#markup']);
Angie Byron
committed
$build['#suffix'] = '</pre>';
}
Angie Byron
committed
return $build;
}
/**
Alex Pott
committed
* {@inheritdoc}
*
* The DisplayPluginBase preview method assumes we will be returning a render
* array. The data plugin will already return the serialized string.
*/
public function preview() {
return $this->view->render();
}
}