View source
<?php
namespace Drupal\rest\Plugin\views\style;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
class Serializer extends StylePluginBase implements CacheableDependencyInterface {
protected $usesRowPlugin = TRUE;
protected $usesGrouping = FALSE;
protected $serializer;
protected $formats = [];
protected $formatProviders;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('serializer'), $container
->getParameter('serializer.formats'), $container
->getParameter('serializer.format_providers'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, array $serializer_formats, array $serializer_format_providers) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->definition = $plugin_definition + $configuration;
$this->serializer = $serializer;
$this->formats = $serializer_formats;
$this->formatProviders = $serializer_format_providers;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['formats'] = [
'default' => [],
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['formats'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Accepted request formats'),
'#description' => $this
->t('Request formats that will be allowed in responses. If none are selected all formats will be allowed.'),
'#options' => $this
->getFormatOptions(),
'#default_value' => $this->options['formats'],
];
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
$formats = $form_state
->getValue([
'style_options',
'formats',
]);
$form_state
->setValue([
'style_options',
'formats',
], array_filter($formats));
}
public function render() {
$rows = [];
foreach ($this->view->result as $row_index => $row) {
$this->view->row_index = $row_index;
$rows[] = $this->view->rowPlugin
->render($row);
}
unset($this->view->row_index);
if (empty($this->view->live_preview)) {
$content_type = $this->displayHandler
->getContentType();
}
else {
$content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
}
return $this->serializer
->serialize($rows, $content_type, [
'views_style_plugin' => $this,
]);
}
public function getFormats() {
return $this->options['formats'];
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [
'request_format',
];
}
public function getCacheTags() {
return [];
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
$formats = $this
->getFormats();
$providers = array_intersect_key($this->formatProviders, array_flip($formats));
$providers[] = 'serialization';
$dependencies += [
'module' => [],
];
$dependencies['module'] = array_merge($dependencies['module'], $providers);
return $dependencies;
}
protected function getFormatOptions() {
$formats = array_keys($this->formatProviders);
return array_combine($formats, $formats);
}
}