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

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon