buildBasicRenderable example

$args[] = $arg;
      }
    }

    $class = $route->getOption('_view_display_plugin_class');
    if ($route->getOption('returns_response')) {
      /** @var \Drupal\views\Plugin\views\display\ResponseDisplayPluginInterface $class */
      return $class::buildResponse($view_id$display_id$args);
    }
    else {
      /** @var \Drupal\views\Plugin\views\display\Page $class */
      $build = $class::buildBasicRenderable($view_id$display_id$args$route);
      Page::setPageRenderArray($build);

      views_add_contextual_links($build, 'page', $display_id$build);

      return $build;
    }
  }

  /** * Gets the title of the given view's display. * * @param string $view_id * The id of the view. * @param string $display_id * The id of the display from the view. * * @return string|\Drupal\Component\Render\MarkupInterface * The title of the display of the view. */
$display['display_options']['access']['options']['role'] = [
      $this->normalRole => $this->normalRole,
    ];
    $view->save();

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
    $account_switcher = \Drupal::service('account_switcher');

    // First access as user with access.     $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
    $account_switcher->switchTo($this->normalUser);
    $result = $renderer->renderPlain($build);
    $this->assertContains('user.roles', $build['#cache']['contexts']);
    $this->assertEquals(['config:views.view.test_access_role']$build['#cache']['tags']);
    $this->assertEquals(Cache::PERMANENT, $build['#cache']['max-age']);
    $this->assertNotSame('', $result);

    // Then without access.     $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
    $account_switcher->switchTo($this->webUser);
    $result = $renderer->renderPlain($build);
    
/** * {@inheritdoc} */
  public function getType() {
    return 'feed';
  }

  /** * {@inheritdoc} */
  public static function buildResponse($view_id$display_id, array $args = []) {
    $build = static::buildBasicRenderable($view_id$display_id$args);

    // Set up an empty response, so for example RSS can set the proper     // Content-Type header.     $response = new CacheableResponse('', 200);
    $build['#response'] = $response;

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $output = (string) $renderer->renderRoot($build);

    

  protected function assertViewsCacheTagsFromStaticRenderArray(ViewExecutable $view, array $expected_render_array_cache_tags$views_caching_is_enabled) {
    $original = $build = DisplayPluginBase::buildBasicRenderable($view->id()$view->current_display ?: 'default', $view->args);

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');

    // Ensure the current request is a GET request so that render caching is     // active for direct rendering of views, just like for actual requests.     /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
    $request_stack = \Drupal::service('request_stack');
    $request = new Request();
    
$display['display_options']['cache'] = [
      'type' => 'tag',
    ];

    $view_random->storage->save();

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');

    $original = $build = DisplayPluginBase::buildBasicRenderable($view_random->id(), 'default');
    $result = $renderer->renderPlain($build);

    $original['#cache'] += ['contexts' => []];
    $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts']$this->container->getParameter('renderer.config')['required_cache_contexts']);

    $this->assertFalse($render_cache->get($original), 'Ensure there is no render cache entry.');

    $build = DisplayPluginBase::buildBasicRenderable($view_random->id(), 'default');
    $result2 = $renderer->renderPlain($build);

    // Ensure that the random ordering works and don't produce the same result.
'type' => 'time',
      'options' => [
        'results_lifespan' => '3600',
        'output_lifespan' => '3600',
      ],
    ]);
    $view->save();
    $this->container->get('router.builder')->rebuildIfNeeded();

    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');
    $cache_element = DisplayPluginBase::buildBasicRenderable('test_display', 'page_1');
    $cache_element['#cache'] += ['contexts' => $this->container->getParameter('renderer.config')['required_cache_contexts']];
    $this->assertFalse($render_cache->get($cache_element));

    $this->drupalGet('test-display');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertNotEmpty($render_cache->get($cache_element));
    $cache_tags = [
      'config:user.role.anonymous',
      'config:views.view.test_display',
      'node_list',
      'rendered',
    ];
public function testRenderCaching() {
    $view = Views::getView('test_access_perm');
    $display = &$view->storage->getDisplay('default');
    $display['display_options']['cache'] = [
      'type' => 'tag',
    ];

    $renderer = $this->container->get('renderer');
    $account_switcher = $this->container->get('account_switcher');

    // First access as user without access.     $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
    $account_switcher->switchTo($this->webUser);
    $this->assertEmpty($renderer->renderPlain($build));

    // Then with access.     $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
    $account_switcher->switchTo($this->normalUser);
    $this->assertNotEmpty($renderer->renderPlain($build));
  }

}
'weight' => ['default' => 0],
      ],
    ];

    return $options;
  }

  /** * {@inheritdoc} */
  public static function buildBasicRenderable($view_id$display_id, array $args = [], Route $route = NULL) {
    $build = parent::buildBasicRenderable($view_id$display_id$args);

    if ($route) {
      $build['#view_id'] = $route->getDefault('view_id');
      $build['#view_display_plugin_id'] = $route->getOption('_view_display_plugin_id');
      $build['#view_display_show_admin_links'] = $route->getOption('_view_display_show_admin_links');
    }
    else {
      throw new \BadFunctionCallException('Missing route parameters.');
    }

    return $build;
  }
$render_cache = \Drupal::service('render_cache');

    // Enable render caching for the views.     /** @var \Drupal\views\ViewEntityInterface $storage */
    $storage = View::load('test_serializer_display_entity');
    $options = &$storage->getDisplay('default');
    $options['display_options']['cache'] = [
      'type' => 'tag',
    ];
    $storage->save();

    $original = DisplayPluginBase::buildBasicRenderable('test_serializer_display_entity', 'rest_export_1');

    // Ensure that there is no corresponding render cache item yet.     $original['#cache'] += ['contexts' => []];
    $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts']$this->container->getParameter('renderer.config')['required_cache_contexts']);

    $cache_tags = [
      'config:views.view.test_serializer_display_entity',
      'entity_test:1',
      'entity_test:10',
      'entity_test:2',
      'entity_test:3',
      
$route_has_format = $route->hasRequirement('_format');
    $route_formats = $route_has_format ? explode('|', $route->getRequirement('_format')) : [];
    $view_route_formats = $view_route->hasRequirement('_format') ? explode('|', $view_route->getRequirement('_format')) : [];
    return $this->overrideAppliesPathAndMethod($view_path$view_route$route)
      && (!$route_has_format || array_intersect($route_formats$view_route_formats) != []);
  }

  /** * {@inheritdoc} */
  public static function buildResponse($view_id$display_id, array $args = []) {
    $build = static::buildBasicRenderable($view_id$display_id$args);

    // Setup an empty response so headers can be added as needed during views     // rendering and processing.     $response = new CacheableResponse('', 200);
    $build['#response'] = $response;

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $output = (string) $renderer->renderRoot($build);

    
class PageTest extends UnitTestCase {

  /** * @covers ::buildBasicRenderable */
  public function testBuildBasicRenderable() {
    $route = new Route('/test-view');
    $route->setDefault('view_id', 'test_view');
    $route->setOption('_view_display_plugin_id', 'page');
    $route->setOption('_view_display_show_admin_links', TRUE);

    $result = Page::buildBasicRenderable('test_view', 'page_1', []$route);

    $this->assertEquals('test_view', $result['#view_id']);
    $this->assertEquals('page', $result['#view_display_plugin_id']);
    $this->assertEquals(TRUE, $result['#view_display_show_admin_links']);
  }

  /** * @covers ::buildBasicRenderable */
  public function testBuildBasicRenderableWithMissingRoute() {
    $this->expectException(\BadFunctionCallException::class);
    
Home | Imprint | This part of the site doesn't use cookies.