简体   繁体   English

如何将根名称和子名称添加到 drupal 查看 REST?

[英]How to add a root name and child name to drupal view REST?

I have a doubt, i've created a REST with View and i formated like JSON, but the output is like this:我有一个疑问,我已经创建了一个带有 View 的 REST 并且我的格式类似于 JSON,但是 output 是这样的:

[{"item1":"123","item2":"123","item3":"123","item4":"","item5":"123","item5":"123"},    {"item1":"345","item2":"345","item3":"345","item4":"","item5":"345","item5":"345"}]

And i need something like this:我需要这样的东西:

{"elements":
 [
  {"element":
   {"item1":"123","item2":"123","item3":"123","item4":"123","item5":"123"}
  },
  {"element":    {"item1":"345","item2":"345","item3":"345","item4":"345","item5":"345"}
  }
 ]
}

How can i add a root name and child name?如何添加根名称和子名称? is some configuration on views that i can do?我可以对视图进行一些配置吗?

I've tried with views_post_execute hook like this:我试过像这样使用 views_post_execute 钩子:

function mymodule_views_post_execute(ViewExecutable $view) {
    if (isset($view) && ($view->storage->id() == 'myrestjson')) {
        $result = ['elements' => array_map(
            function ($subarray) {
                return ['element' => $subarray];
            },$view->result
        )];
        $view->result = $result;
    }
  }

but i'm getting this error:但我收到此错误:

TypeError: Argument 1 passed to Drupal\views\Plugin\views\field\FieldPluginBase::advancedRender() must be an instance of Drupal\views\ResultRow,array given, called in mysite\core\modules\rest\src\Plugin\views\row\DataFieldRow.php on line 147 in Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender() (line 1142 of core\modules\views\src\Plugin\views\field\FieldPluginBase.php).

Could you please help me?请你帮助我好吗? Regards Mario问候马里奥

You can use a custom Serializer to alter the output:您可以使用自定义Serializer程序来更改 output:

  1. Create a new custom serializer class in your custom module with the path: .../your_module/src/Plugin/views/style/CustomSerializer.php .在您的自定义模块中创建一个新的自定义序列化程序 class ,路径为: .../your_module/src/Plugin/views/style/CustomSerializer.php
  2. Custom serializer class should be extended from the core Serializer class: Drupal\rest\Plugin\views\style\Serializer.php自定义序列化器 class 应从核心Serializer器 class 扩展: Drupal\rest\Plugin\views\style\Serializer.php
  3. Override the render() method (you can compare with core Serializer class to see what I modified)覆盖render()方法(可以和core Serializer class比较看看我修改了什么)
<?php
namespace Drupal\your_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 = [];
    foreach ($this->view->result as $row_index => $row) {
      $this->view->row_index = $row_index;
      $rows[] = ['element' => $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(['elements' => $rows], $content_type, ['views_style_plugin' => $this]);
  }
}
  1. Go to your view > REST Export > FORMAT > Format > Select newly created serializer Go 到您的视图 > REST Export > FORMAT > Format > Select 新创建的序列化程序在此处输入图像描述
  2. Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM