Skip to main content
Category:

Code snippet that can be used to Embed view in custom module in Drupal 8.

using views_embed_view() you can render any view:

<?php
namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\Renderer;

/**
 * @Block(
 *   id = "people_block",
 *   admin_label = @Translation("Custom Newsletter block"),
 *   category = @Translation("Custom Newsletter block")
 * )
 */
class PeopleBlock extends BlockBase
{
  
  /**
   * {@inheritdoc}
   */
  public function build()
  {
    $content = [];
    // get renderable array for view
    $view = views_embed_view('user_admin_people', 'page_1');
    // render view
    $content['#markup'] = render($view);

    return $view;
  }
}

We can pass parameters using views_embed_view as well, like following : 

$view = views_embed_view('user_admin_people', 'page_1', $param1, $param2, ...);

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook