【问题标题】:How to execute a Drupal-view in a custom module and print the output (as html-markup)?如何在自定义模块中执行 Drupal 视图并打印输出(作为 html 标记)?
【发布时间】:2018-01-14 23:15:45
【问题描述】:

我想在我的自定义模块中打印一个简单视图的输出。但是是行不通的。我从论坛和 stackoverflow 中尝试了许多选项。它们都打印“数组”而不是 html 标记。

我的控制器:

class DefaultController extends ControllerBase {


    public function myfunc1() {

        $view = Views::getView('myfirstview');

        $view->setDisplay('page_1');
        $view->preExecute();
        $view->execute();

        // $myresults = $view->preview();  = array
        // $myresults = $view->render();  = array
        $myresults = $view->result; // = array


        return array(
            '#title' => 'Hello World!',
            '#markup' => $myresults,
        );
    }
}

如何以编程方式打印视图的结果/输出?

我不想让它没有“嵌入视图”,因为我想稍后设置一些暴露的过滤器。

【问题讨论】:

    标签: drupal drupal-views drupal-8


    【解决方案1】:

    获取视图

    $corresponding_view = Views::getView('myfirstview');
    

    设置暴露的过滤器

    $arguments = your arguments (exposed filter value);
    $corresponding_view->setArguments($arguments);
    

    嵌入您的视图

    return [
      '#type' => 'view',
      '#name' => 'myfirstview',
      '#view' => $corresponding_view,
      '#display_id' => 'page_1',
      '#embed' => TRUE,
    ];
    

    【讨论】:

    • 否 - 这会导致一个数组被渲染。它不会产生标记
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    相关资源
    最近更新 更多