Skip to content

Drupal 8 Views

andyceo edited this page Mar 14, 2017 · 2 revisions

Отобразить Views программно

// get the view machine id
$view = \Drupal\views\Views::getView('view_machine_id');
 
// set the display machine id
$view->setDisplay('view_display_machine_id');
 
// set arguments/filter values
$view->setArguments(array('filter_value_1', 'filter_value_2'));
 
$variables['theview'] = $view->render();

И потом в twig-шаблоне:

{{ theview }}

Ссылки:

Отобразить Views в контроллере (Drupal 8.2.6)

<?php

namespace Drupal\example\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Returns responses for Workbench routes.
 */
class ExampleController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a WorkbenchController object.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  public function __construct(RendererInterface $renderer) {
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('renderer')
    );
  }

  /**
   * Displays workbench area for all content.
   *
   * @return array
   *   A render array for a workbench area.
   */
  public function workbench() {
    $build = [
      '#type' => 'markup',
      '#markup' => $this->renderer->render(views_embed_view('example_views', 'page_1')),
    ];
    return $build;
  }

}

Sidebar is under construction

Clone this wiki locally