buildRenderable example


  public function testCssClassCleaning() {
    \Drupal::service('theme_installer')->install(['test_theme']);
    $this->config('system.theme')->set('default', 'test_theme')->save();

    $entity = EntityTest::create();
    $entity->save();
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $view = Views::getview('test_preprocess');
    $build = $view->buildRenderable();
    $renderer->renderRoot($build);
    $this->assertStringContainsString('class="entity-test--default entity-test__default', (string) $build['#markup']);
    $view->destroy();

    $view->setDisplay('display_2');
    $build = $view->buildRenderable();
    $renderer->renderRoot($build);
    $markup = (string) $build['#markup'];
    $this->assertStringContainsString('css_class: entity-test--default and-another-class entity-test__default', $markup);
    $this->assertStringContainsString('attributes: class="entity-test--default and-another-class entity-test__default', $markup);
  }

}


  /** * Ensure that the view renderable contains the cache contexts. */
  public function testBuildRenderableWithCacheContexts() {
    $view = View::load('test_view');
    $display =& $view->getDisplay('default');
    $display['cache_metadata']['contexts'] = ['views_test_cache_context'];
    $executable = $view->getExecutable();

    $build = $executable->buildRenderable();
    $this->assertEquals(['views_test_cache_context']$build['#cache']['contexts']);
  }

  /** * Ensures that saving a view calculates the cache contexts. */
  public function testViewAddCacheMetadata() {
    $view = View::load('test_display');
    $view->save();

    $this->assertEqualsCanonicalizing(['languages:' . LanguageInterface::TYPE_CONTENT, 'languages:' . LanguageInterface::TYPE_INTERFACE, 'url.query_args', 'user.node_grants:view', 'user.permissions']$view->getDisplay('default')['cache_metadata']['contexts']);
  }

  public function buildRenderable($display_id = NULL, $args = []$cache = TRUE) {
    // @todo Extract that into a generic method.     if (empty($this->current_display) || $this->current_display != $this->chooseDisplay($display_id)) {
      if (!$this->setDisplay($display_id)) {
        return NULL;
      }
    }

    return $this->display_handler->buildRenderable($args$cache);
  }

  /** * Executes the given display, with the given arguments. * * To be called externally by whatever mechanism invokes the view, * such as a page callback, hook_block, etc. * * This function should NOT be used by anything external as this * returns data in the format specified by the display. It can also * have other side effects that are only intended for the 'proper' * use of the display, such as setting page titles. * * If you simply want to view the display, use View::preview() instead. * * @param string $display_id * The display ID of the view to be executed. * @param string[] $args * The arguments to be passed to the view. * * @return array|null * A renderable array containing the view output or NULL if the display ID * of the view to be executed doesn't exist. */

  protected function assertViewsCacheTags(ViewExecutable $view$expected_results_cache$views_caching_is_enabled, array $expected_render_array_cache_tags) {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');

    $build = $view->buildRenderable();
    $original = $build;

    // 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 = Request::createFromGlobals();
    $request->server->set('REQUEST_TIME', REQUEST_TIME);
    $view->setRequest($request);
    $request_stack->push($request);
    $renderer->renderRoot($build);

    
$this->assertSession()->statusCodeEquals(200);
    $this->assertNotEmpty($render_cache->get($cache_element));
    $this->assertCacheTags($cache_tags);
  }

  /** * Tests that a display without caching still contains the cache metadata. */
  public function testDisplayWithoutCacheStillBubblesMetadata() {
    $view = Views::getView('test_display');

    $uncached_block = $view->buildRenderable('block_1', [], FALSE);
    $cached_block = $view->buildRenderable('block_1', [], TRUE);
    $this->assertEquals($uncached_block['#cache']['contexts']$cached_block['#cache']['contexts'], 'Cache contexts are the same when you render the view cached and uncached.');
  }

}
class Embed extends DisplayPluginBase {

  /** * {@inheritdoc} */
  protected $usesAttachments = TRUE;

  /** * {@inheritdoc} */
  public function buildRenderable(array $args = []$cache = TRUE) {
    $build = parent::buildRenderable($args$cache);
    $build['#embed'] = TRUE;
    return $build;
  }

}
/** * Tests the rendered output and form output of a view element. */
  public function testViewElement() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $view = Views::getView('test_view_embed');

    // Get the render array, #embed must be FALSE since this is the default     // display.     $render = $view->buildRenderable();
    $this->assertFalse($render['#embed']);
    $this->setRawContent($renderer->renderRoot($render));

    $xpath = $this->xpath('//div[@class="views-element-container"]');
    $this->assertNotEmpty($xpath, 'The view container has been found in the rendered output.');

    // There should be 5 rows in the results.     $xpath = $this->xpath('//div[@class="views-row"]');
    $this->assertCount(5, $xpath);

    // Add an argument and save the view.
          if ($value instanceof EntityInterface) {
            $value = $value->id();
          }
          $args[$argument_name] = $value;
        }
      }
    }

    // We ask ViewExecutable::buildRenderable() to avoid creating a render cache     // entry for the view output by passing FALSE, because we're going to cache     // the whole block instead.     if ($output = $this->view->buildRenderable($this->displayID, array_values($args), FALSE)) {
      // Before returning the block output, convert it to a renderable array       // with contextual links.       $this->addContextualLinks($output);

      // Block module expects to get a final render array, without another       // top-level #pre_render callback. So, here we make sure that Views'       // #pre_render callback has already been applied.       $output = View::preRenderViewElement($output);

      // Override the label to the dynamic title configured in the view.       if (empty($this->configuration['views_label']) && $this->view->getTitle()) {
        
    // pass the parameters along in the pager, filters etc.     $view_request = $view_executable->getRequest();
    $view_request->query->add($state->all());
    $view_executable->setRequest($view_request);

    $args = [$state->getSelectedTypeId()];

    $view_executable->setDisplay($display_id);
    $view_executable->preExecute($args);
    $view_executable->execute($display_id);

    return $view_executable->buildRenderable($display_id$args, FALSE);
  }

}
    // so they should be added to the css/js storage.     $view = Views::getView('test_view');
    $view->setDisplay();
    $view->storage->set('id', 'test_cache_header_storage');
    $view->display_handler->overrideOption('cache', [
      'type' => 'time',
      'options' => [
        'output_lifespan' => '3600',
      ],
    ]);

    $output = $view->buildRenderable();
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $renderer->executeInRenderContext(new RenderContext()function D) use (&$output$renderer) {
      return $renderer->render($output);
    });

    unset($view->pre_render_called);
    $view->destroy();

    $view->setDisplay();
    $output = $view->buildRenderable();
    
'weight' => 2,
    ]);

    parent::setUpFixtures();
  }

  /** * Tests the order of the handlers. */
  public function testAreaOrder() {
    $view = Views::getView('test_area_order');
    $renderable = $view->buildRenderable();
    $output = $this->render($renderable);

    $position_powered = strpos($output, 'block-id-olivero-powered');
    $position_branding = strpos($output, 'block-id-olivero-branding');

    $this->assertNotEquals(0, $position_powered, 'ID olivero-powered found.');
    $this->assertNotEquals(0, $position_branding, 'ID olivero-branding found');

    // Make sure "powered" is before "branding", so it reflects the position     // in the configuration, and not the weight of the blocks.     $this->assertLessThan($position_branding$position_powered);
  }
return;
    }

    $args = $this->getOption('inherit_arguments') ? $this->view->args : [];
    $view->setArguments($args);
    $view->setDisplay($this->display['id']);
    if ($this->getOption('inherit_pager')) {
      $view->display_handler->usesPager = $this->view->displayHandlers->get($display_id)->usesPager();
      $view->display_handler->setOption('pager', $this->view->displayHandlers->get($display_id)->getOption('pager'));
    }

    $attachment = $view->buildRenderable($this->display['id']$args);

    switch ($this->getOption('attachment_position')) {
      case 'before':
        $this->view->attachment_before[] = $attachment;
        break;

      case 'after':
        $this->view->attachment_after[] = $attachment;
        break;

      case 'both':
        

  protected function getRenderCache(ViewExecutable $view) {
    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');
    $view->element = ['#cache' => []];
    $build = $view->buildRenderable();
    $build['#cache']['contexts'] = Cache::mergeContexts($build['#cache']['contexts']$this->container->getParameter('renderer.config')['required_cache_contexts']);

    return $render_cache->get($build);
  }

  /** * Tests the tag cache plugin. */
  public function testTagCaching() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    

  public function double(string $view_id, string $display_id, string $args_1, string $args_2) {
    $build = [];
    $view = Views::getView($view_id);
    $build[] = $view->buildRenderable($display_idexplode(",", $args_1));
    $view = Views::getView($view_id);
    $build[] = $view->buildRenderable($display_idexplode(",", $args_2));
    return $build;
  }

}
Home | Imprint | This part of the site doesn't use cookies.