TransWikia.com

How do I programmatically render a view with contextual filters?

Drupal Answers Asked on December 14, 2021

How do I programmatically render a view with contextual filters in Drupal 8?

In Drupal 7, I generally use the following code.

$view = views_get_view('test_view'); //test_view is the view machine name
$view->set_display("block"); //block is the view display name
$view->set_arguments($tid); //$tid is the argument to pass
$view->pre_execute();
$view->execute();
$content = $view->render();

Is there any alternative way to achieve this in Drupal 8?

5 Answers

This works almost everywhere I use it:

use DrupalviewsViews; // above

$activeContestId = 100; 
$view = Views::getView('mymodule_contestants');
$view->setDisplay('block_1');
// contextual relationship filter
$view->setArguments([$activeContestId]);
$view->execute();
$rendered = $view->render();
$output = Drupal::service('renderer')->render($rendered);

Source: Render a view with contextual filters programmatically in Drupal 9

Answered by Stef Van Looveren on December 14, 2021

The easiest way to render a view in Drupal 8 is using the render element provided by core: https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Element%21View.php/class/View/8.9.x

  $view = [
    '#type' => 'view',
    '#name' => 'test_view',
    '#display_id' => 'block',
    '#arguments' => [$tid],
    '#embed' => TRUE,
  ],

Answered by Eric C on December 14, 2021

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

$view = DrupalviewsViews::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);

Answered by imclean on December 14, 2021

/**
 * 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;
}

Answered by Santo Boldizar on December 14, 2021

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);
  }

Answered by apaderno on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP