How do I execute a view in a custom module and print the output (as HTML markup)? I want to print the output from a simple view in my custom module. But is doesnt work. I have tried many options from forums and stackoverflow. All of them print "array" instead of html-markup.

My controller:

class DefaultController extends ControllerBase {


    public function myfunc1() {

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

        $view-&gt;setDisplay('page_1');
        $view-&gt;preExecute();
        $view-&gt;execute();

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


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


How can I print the result/output of a view programmatically?

I dont want to make it without "embed view" , because I want to set some exposed filters later.
Views implements its own render element type (Drupal\views\Element\View), which you can use like so:

return [
  '#title' =&gt; 'Hello world',
  'view' =&gt; [
    '#type' =&gt; 'view',
    '#name' =&gt; 'myfirstview',
    '#display_id' =&gt; 'page_1',
    '#arguments' =&gt; ['foo', 'bar'],
    '#embed' =&gt; TRUE,
  ],
];


If you need to manipulate the View before rendering it, (you mentioned exposed filters), you can do that first and pass along the object:

$view = Views::getView('myfirstview');
$view-&gt;setFoo('bar');

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