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

 

Tags :

Comments

Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

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

Web Posts

Search

Page Facebook