n How to use views_embed_view() in drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

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

using views_embed_view() you can render any view:

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


Page Facebook