【问题标题】:How to add a root name and child name to drupal view REST?如何将根名称和子名称添加到 drupal 视图 REST?
【发布时间】:2021-06-01 16:07:16
【问题描述】:

我有一个疑问,我已经创建了一个带有 View 的 REST,并且我的格式类似于 JSON,但是输出是这样的:

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

我需要这样的东西:

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

如何添加根名称和子名称? 我可以对视图进行一些配置吗?

我已经尝试过像这样使用 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;
    }
  }

但我收到此错误:

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).

你能帮帮我吗? 问候 马里奥

【问题讨论】:

    标签: php view drupal hook drupal-views


    【解决方案1】:

    您可以使用自定义Serializer 来更改输出:

    1. 在您的自定义模块中创建一个新的自定义序列化程序类,路径为:.../your_module/src/Plugin/views/style/CustomSerializer.php
    2. 自定义序列化程序类应从核心Serializer扩展类:Drupal\rest\Plugin\views\style\Serializer.php
    3. 重写render()方法(你可以和核心Serializer类比较看看我修改了什么)
    <?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. 转到您的视图 > REST Export > FORMAT > Format > 选择新创建的序列化程序
    2. 结果:

    【讨论】:

    • 您好,感谢您的回答,我已经使用这些文件创建了一个模块,但是在安装此模块后,我看不到新选项自定义序列化程序。我创建了 2 个文件:custom_serializer.info.yml 和 CustonSerializer.php
    • 您应该在您的自定义模块中创建新目录:.../your_module/src/Plugin/views/style/ 并将CustomSerializer.php 移动到其中。记得在那之后刷新缓存。
    • 谢谢你,我没有创建 src 文件夹,但现在它可以工作了
    • Hi 是否可以使用此自定义序列化程序制作 3 个不同的选项?或者我必须创建 3 个具有不同选项的模块?问候马里奥
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2015-07-20
    • 2023-01-11
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多