3 namespace Drupal\rest\Plugin\views\style;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\views\Plugin\views\style\StylePluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\Serializer\SerializerInterface;
13 * The style plugin for serialized output formats.
15 * @ingroup views_style_plugins
19 * title = @Translation("Serializer"),
20 * help = @Translation("Serializes views row data using the Serializer component."),
21 * display_types = {"data"}
24 class Serializer extends StylePluginBase implements CacheableDependencyInterface {
29 protected $usesRowPlugin = TRUE;
34 protected $usesGrouping = FALSE;
37 * The serializer which serializes the views result.
39 * @var \Symfony\Component\Serializer\Serializer
41 protected $serializer;
44 * The available serialization formats.
48 protected $formats = [];
51 * The serialization format providers, keyed by format.
55 protected $formatProviders;
60 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
65 $container->get('serializer'),
66 $container->getParameter('serializer.formats'),
67 $container->getParameter('serializer.format_providers')
72 * Constructs a Plugin object.
74 public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, array $serializer_formats, array $serializer_format_providers) {
75 parent::__construct($configuration, $plugin_id, $plugin_definition);
77 $this->definition = $plugin_definition + $configuration;
78 $this->serializer = $serializer;
79 $this->formats = $serializer_formats;
80 $this->formatProviders = $serializer_format_providers;
86 protected function defineOptions() {
87 $options = parent::defineOptions();
88 $options['formats'] = ['default' => []];
96 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
97 parent::buildOptionsForm($form, $form_state);
100 '#type' => 'checkboxes',
101 '#title' => $this->t('Accepted request formats'),
102 '#description' => $this->t('Request formats that will be allowed in responses. If none are selected all formats will be allowed.'),
103 '#options' => $this->getFormatOptions(),
104 '#default_value' => $this->options['formats'],
111 public function submitOptionsForm(&$form, FormStateInterface $form_state) {
112 parent::submitOptionsForm($form, $form_state);
114 $formats = $form_state->getValue(['style_options', 'formats']);
115 $form_state->setValue(['style_options', 'formats'], array_filter($formats));
121 public function render() {
123 // If the Data Entity row plugin is used, this will be an array of entities
124 // which will pass through Serializer to one of the registered Normalizers,
125 // which will transform it to arrays/scalars. If the Data field row plugin
126 // is used, $rows will not contain objects and will pass directly to the
128 foreach ($this->view->result as $row_index => $row) {
129 $this->view->row_index = $row_index;
130 $rows[] = $this->view->rowPlugin->render($row);
132 unset($this->view->row_index);
134 // Get the content type configured in the display or fallback to the
136 if ((empty($this->view->live_preview))) {
137 $content_type = $this->displayHandler->getContentType();
140 $content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
142 return $this->serializer->serialize($rows, $content_type, ['views_style_plugin' => $this]);
146 * Gets a list of all available formats that can be requested.
148 * This will return the configured formats, or all formats if none have been
152 * An array of formats.
154 public function getFormats() {
155 return $this->options['formats'];
161 public function getCacheMaxAge() {
162 return Cache::PERMANENT;
168 public function getCacheContexts() {
169 return ['request_format'];
175 public function getCacheTags() {
182 public function calculateDependencies() {
183 $dependencies = parent::calculateDependencies();
184 $formats = $this->getFormats();
185 $providers = array_intersect_key($this->formatProviders, array_flip($formats));
186 // The plugin always uses services from the serialization module.
187 $providers[] = 'serialization';
189 $dependencies += ['module' => []];
190 $dependencies['module'] = array_merge($dependencies['module'], $providers);
191 return $dependencies;
195 * Returns an array of format options
198 * An array of format options. Both key and value are the same.
200 protected function getFormatOptions() {
201 $formats = array_keys($this->formatProviders);
202 return array_combine($formats, $formats);