web-dev-qa-db-fra.com

Comment changer la vue REST format d'exportation

Ce sont mes paramètres en vue.

enter image description here

Les données que j'ai sont maintenant dans ce format:

[{"created":"1551709414","field_iot_integer":"43"},
{"created":"1551709404","field_iot_integer":"98"},
{"created":"1551709392","field_iot_integer":"58"},
{"created":"1551709382","field_iot_integer":"4"},
{"created":"1551709373","field_iot_integer":"65"},
{"created":"1551709356","field_iot_integer":"23"}]

Je voudrais changer ce format en cela, débarrassé de tous les commentaires. :

[[1551709414,43],
[1551709404,98],
[1551709392,58],
[1551709382,4],
[1551709373,65],
[1551709356,23]]

Comment puis-je y parvenir?

3
SakaSerbia

Pour modifier la structure de la sortie Serializer, vous devez créer un Serializer personnalisé, voici les étapes de votre cas d'utilisation:

  1. Créer CustomSerializer étend le Drupal\rest\Plugin\views\style\Serializer et mettez à jour la méthode render selon vos besoins.
    • Créer MY_MODULE/Plugin/views/style/CustomSerializer.php

namespace Drupal\MY_MODULE\Plugin\views\style;

use Drupal\rest\Plugin\views\style\Serializer;

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

  /**
   * {@inheritdoc}
   */
  public function render() {
    $rows = [];
    // 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;
      $row_render = $this->view->rowPlugin->render($row);
      $iot_integer = 0;
      if (isset($row_render["field_iot_integer"]) && $row_render["field_iot_integer"] instanceof \Drupal\Core\Render\Markup){
        $iot_integer = (int) $row_render["field_iot_integer"]->__toString();
      }
     $rows[] = [
        $row_render["created"]->__toString(),
       $iot_integer
      ];
    }
    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]);
  }
}
  1. Vider le cache:
  2. Accédez à votre vue et changez le format de Serializer en Custom serializer comme enter image description here

  3. Le résultat:

enter image description here

5
berramou