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, ...);