Skip to main content

Drupal 8 Views: Render a View programmatically

If you want to render a views view to your page content is now pretty straightforward:

$content['content_prefooter']['view'] = [
  '#type' => 'view',
  '#name' => 'articles',
  '#display_id' => 'embed',
  '#arguments' => [

And that's it! $content is my render array which I'll return and let Drupal render. I suspect most of the bits there are self-explanatory, but in case they aren't:

  • '#type' => 'view' is the magic that lets Drupal's rendering system know that this array represents a view to render.
  • '#name' is the machine name of the view you want to render.
  • '#display_id' is the display ID within the view to render.
  • '#arguments' is an optionally array of arguments to pass to the view.

If you want it even easier than that, then using the views_embed_view() function will return you the render array and complete access checks etc:

$content['editor_tools']['view'] = views_embed_view('editor_tools', 'embed', 123);


Now if you want to embed a view in custom module. This is how should do it:

Drupal views are really powerful and you get a lot of functionality out of the box.

Normally you can assign a URL path in page display of the view and then access this view using that URL path. But sometimes we might want to use our own module to display or embed views through code.

To achieve this, Drupal provides us views_embed_view() helper function.

As the documentation suggests, it accepts following parameters

  • $name - Machine name of the view
  • $display_id - display id of the view. This depends on which type of display we are using. If the display is set to page, normally its display id is page_1.

This function returns a renderable array that can be rendered using Drupal\Core\Render\Renderer

Suppose you want to use your custom controller action to render a view. To achieve this, first, we need to inject Drupal\Core\Render\Renderer in our controller when creating its new instance.

namespace Drupal\music\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\Renderer;

class AlbumController extends ControllerBase {

   * @var Renderer
  protected $renderer;

  public function __construct(Renderer $renderer) {
    $this->renderer = $renderer;

  public static function create(ContainerInterface $container) {
    return new static(


And then in any action of this controller we can render a view like following:

public function albumSongList() {
  $content = [];
  // get renderable array for view
  $view = views_embed_view('album_song_list', 'page_1');
  // render view
  $content['#markup'] = $this->renderer->render($view);

  return $view;

View parameters

Sometimes our view relies on some parameters. For example, our view might display results based on some node id contextual filter. We can pass these parameters using views_embed_view as well, like following

$view = views_embed_view('album_song_list', 'page_1', $param1, $param2, ...);
public function albumSongList($node) {
  $content = [];
  // get renderable array for view
  $view = views_embed_view('album_song_list', 'page_1', $node);
  // render view
  $content['#markup'] = $this->renderer->render($view);
  // return rendered view
  return $view;


There is also The buildRenderable method

use Drupal\views\Views;
$view = Views::getView('user_list');

// ... some form
$form['user_list'] = $view->buildRenderable('block_1', [$user_id]);

The first option you have is to get a view using the Views class and then call the buildRenderable() method. Let's add a form element that is printing a view.

In this example, we are rendering the view called User list (machine name is user_list). The first param in the buildRenderable method is the display ID and the second param is an array of arguments passed along to the view (in other words that's our contextual filter).

If you pass an invalid display ID the method will return a NULL value, and if everything is okay, you'll get a renderable array with #type that is equal to view.