How do I programmatically render a view with contextual filters?

Solutions

/**
 * Return the rendered view with contextual filter.
 * @param string $viewId - The view machine name.
 * @param string $displayId - The display machine name.
 * @param array $arguments - The arguments to pass.
 * 
 * @return object $result
 */
function getView($viewId, $displayId, array $arguments)
{
    $result = false;
    $view = Views::getView($viewId);

    if (is_object($view)) {
        $view->setDisplay($displayId);
        $view->setArguments($arguments);
        $view->execute();

        // Render the view
        $result = \Drupal::service('renderer')->render($view->render());
    }

    return $result;
}

/**
 * Get json output.
 * @return array $data
 */
function jsonData() {
    $data = [];
    $viewId = 'rest_comments';
    $displayId = 'rest_export_1';
    $arguments = [$nid];

    // Get the view
    $result = getView($viewId, $displayId, $arguments);

    if(is_object($result)) {
        $json = $result->jsonSerialize();
        $data = json_decode($json);
    }

    return $data;
}


/**
 * Get the view object.
 * @return object $view
 */
function view() {
    $viewId = 'rest_comments';
    $displayId = 'rest_export_1';
    $arguments = [$nid];

    // Get the view
    $view = getView($viewId, $displayId, $arguments);

    return $view;
}

Basing on the code used by views_get_view_result(), I would use the following code, as code equivalent to the one you show in the question.

  $args = [$tid];
  $view = Views::getView('test_view');
  if (is_object($view)) {
    $view->setArguments($args);
    $view->setDisplay('block');
    $view->preExecute();
    $view->execute();
    $content = $view->buildRenderable('block', $args);
  }

To get the actual JSON output of the REST Export display you can do something like this:

$view = \Drupal\views\Views::getView($view_id); 
$render_array = $view->buildRenderable('rest_export_display_name', $args);
$rendered = \Drupal::service('renderer')->renderRoot($render_array);
$json_string = $rendered->jsonSerialize();
$json_object = json_decode($rendered);
Tags: Views / Drupal 8

Similar questions

How render view with programmatically passed parameters of contextual filter
I am using Drupal 8. Added new view My Vocabulary and created a contextual filter with paragraph ID. Now I need to render the view with passed parameters to contextual filter. What hook to use to solve my problem. Thanks.
How do I render contextual links for blocks I render in my code?
I loaded a block subject and content with module_invoke() and used render() for rendering them. I want to use contextual links for this block. How can I render them too? This is the code I am using insde the page--front.tpl.php template. What code should I instead use?
How to set programmatically multiple arguments for views contextual filters
How to set programmatically multiple arguments for views contextual filters? I can do it with one argument, it works great, but not with multiple arguments. I want to select all nodes created by a list of people, but views only takes first in array into consideration: Am I doing something wrong?
Views contextual filters programmatically alter add to or
I have 2 fields in my contextual filters. Obviously, the default join is AND but I'd like to filter on one or the other field depending on which one of two arguments is provided. I tried the following approach: However, using the devel module to inspect the Views data, the type field in the where section of my View still outputs AND instead of OR. ...
How do I programmatically change contextual filters?
Basing on a blog post, I wrote the following code. I cleared the cache, but the code I wrote doesn't have any effect. The SQL query is not altered; only single taxonomy ID is returned. I have selected Allow multiple values for the filter. What is wrong with the code? How can I fix it?
How do I programmatically set contextual filters?
I need to set a contextual filter programmatically and almost every view I have will have this filter. However, some wont and will have other contextual filters instead. I have tried following the examples in here but this will just set the first argument to be my filter value regardless of what field is it for. In views_pre_build I get the keyed a...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.