これは私の見ている設定です。
私が持っているデータは現在この形式です:
[{"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"}]
これでこのフォーマットを変更して、すべてのコメントを削除したいと思います。 :
[[1551709414,43],
[1551709404,98],
[1551709392,58],
[1551709382,4],
[1551709373,65],
[1551709356,23]]
どうすればこれを達成できますか?
Serializer
出力の構造を変更するには、カスタムSerializer
を作成する必要があります。使用例の手順は次のとおりです。
CustomSerializer
を作成してDrupal\rest\Plugin\views\style\Serializer
を拡張し、render
メソッドを必要に応じて更新します。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]);
}
}