1. 8.5.x vendor/zendframework/zend-diactoros/src/Request/Serializer.php
  2. 8.5.x vendor/symfony/serializer/Serializer.php
  3. 8.5.x vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php
  4. 8.5.x vendor/zendframework/zend-diactoros/src/Response/Serializer.php
  5. 8.5.x core/modules/rest/src/Plugin/views/style/Serializer.php
  6. 8.0.x vendor/symfony/serializer/Serializer.php
  7. 8.0.x core/modules/rest/src/Plugin/views/style/Serializer.php
  8. 8.0.x vendor/zendframework/zend-diactoros/src/Response/Serializer.php
  9. 8.0.x vendor/zendframework/zend-diactoros/src/Request/Serializer.php
  10. 8.0.x vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php
  11. 8.1.x vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php
  12. 8.1.x core/modules/rest/src/Plugin/views/style/Serializer.php
  13. 8.1.x vendor/zendframework/zend-diactoros/src/Request/Serializer.php
  14. 8.1.x vendor/zendframework/zend-diactoros/src/Response/Serializer.php
  15. 8.1.x vendor/symfony/serializer/Serializer.php
  16. 8.2.x core/modules/rest/src/Plugin/views/style/Serializer.php
  17. 8.2.x vendor/zendframework/zend-diactoros/src/Request/Serializer.php
  18. 8.2.x vendor/zendframework/zend-diactoros/src/Response/Serializer.php
  19. 8.2.x vendor/symfony/serializer/Serializer.php
  20. 8.2.x vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php
  21. 8.3.x vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php
  22. 8.3.x core/modules/rest/src/Plugin/views/style/Serializer.php
  23. 8.3.x vendor/zendframework/zend-diactoros/src/Request/Serializer.php
  24. 8.3.x vendor/symfony/serializer/Serializer.php
  25. 8.3.x vendor/zendframework/zend-diactoros/src/Response/Serializer.php
  26. 8.4.x vendor/symfony/serializer/Serializer.php
  27. 8.4.x vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php
  28. 8.4.x vendor/zendframework/zend-diactoros/src/Request/Serializer.php
  29. 8.4.x vendor/zendframework/zend-diactoros/src/Response/Serializer.php
  30. 8.4.x core/modules/rest/src/Plugin/views/style/Serializer.php
  31. 8.6.x vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php
  32. 8.6.x core/modules/rest/src/Plugin/views/style/Serializer.php
  33. 8.6.x vendor/symfony/serializer/Serializer.php
  34. 8.6.x vendor/zendframework/zend-diactoros/src/Request/Serializer.php
  35. 8.6.x vendor/zendframework/zend-diactoros/src/Response/Serializer.php

Namespace

Drupal\rest\Plugin\views\style

File

core/modules/rest/src/Plugin/views/style/Serializer.php
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;

/**
 * The style plugin for serialized output formats.
 *
 * @ingroup views_style_plugins
 *
 * @ViewsStyle(
 *   id = "serializer",
 *   title = @Translation("Serializer"),
 *   help = @Translation("Serializes views row data using the Serializer component."),
 *   display_types = {"data"}
 * )
 */
class Serializer extends StylePluginBase implements CacheableDependencyInterface {

  /**
   * {@inheritdoc}
   */
  protected $usesRowPlugin = TRUE;

  /**
   * {@inheritdoc}
   */
  protected $usesGrouping = FALSE;

  /**
   * The serializer which serializes the views result.
   *
   * @var \Symfony\Component\Serializer\Serializer
   */
  protected $serializer;

  /**
   * The available serialization formats.
   *
   * @var array
   */
  protected $formats = array();

  /**
   * The serialization format providers, keyed by format.
   *
   * @var string[]
   */
  protected $formatProviders;

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * Constructs a Plugin object.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['formats'] = array(
      'default' => array(),
    );
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['formats'] = array(
      '#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'],
    );
  }

  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {
    parent::submitOptionsForm($form, $form_state);
    $formats = $form_state
      ->getValue(array(
      'style_options',
      'formats',
    ));
    $form_state
      ->setValue(array(
      'style_options',
      'formats',
    ), array_filter($formats));
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $rows = array();

    // If the Data Entity row plugin is used, this will be an array of entities
    // which will pass through Serializer to one of the registered Normalizers,
    // which will transform it to arrays/scalars. If the Data field row plugin
    // is used, $rows will not contain objects and will pass directly to the
    // Encoder.
    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);

    // Get the content type configured in the display or fallback to the
    // default.
    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,
    ]);
  }

  /**
   * Gets a list of all available formats that can be requested.
   *
   * This will return the configured formats, or all formats if none have been
   * selected.
   *
   * @return array
   *   An array of formats.
   */
  public function getFormats() {
    return $this->options['formats'];
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return [
      'request_format',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    $formats = $this
      ->getFormats();
    $providers = array_intersect_key($this->formatProviders, array_flip($formats));

    // The plugin always uses services from the serialization module.
    $providers[] = 'serialization';
    $dependencies += [
      'module' => [],
    ];
    $dependencies['module'] = array_merge($dependencies['module'], $providers);
    return $dependencies;
  }

  /**
   * Returns an array of format options
   *
   * @return string[]
   *   An array of format options. Both key and value are the same.
   */
  protected function getFormatOptions() {
    $formats = array_keys($this->formatProviders);
    return array_combine($formats, $formats);
  }

}

Classes

Namesort descending Description
Serializer The style plugin for serialized output formats.