You are here

public function AutocompletionCallback::render in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x src/Plugin/views/display/AutocompletionCallback.php \Drupal\search_autocomplete\Plugin\views\display\AutocompletionCallback::render()

Renders this display.

Overrides DisplayPluginBase::render

File

src/Plugin/views/display/AutocompletionCallback.php, line 218

Class

AutocompletionCallback
The plugin that handles Data response callbacks for REST resources.

Namespace

Drupal\search_autocomplete\Plugin\views\display

Code

public function render() {
  $build = [];
  $build['#markup'] = $this->renderer
    ->executeInRenderContext(new RenderContext(), function () {
    return $this->view->style_plugin
      ->render();
  });
  $this->view->element['#content_type'] = $this
    ->getMimeType();
  $this->view->element['#cache_properties'][] = '#content_type';

  // Wrap the output in a pre tag if this is for a live preview.
  // Also little trick added to preview a formatted json for easier debug.
  if (!empty($this->view->live_preview)) {
    $dump = json_decode($build['#markup']);
    $build['#prefix'] = '<pre>';
    $build['#markup'] = json_encode($dump, JSON_PRETTY_PRINT);
    $build['#suffix'] = '</pre>';
  }
  elseif ($this->view
    ->getRequest()
    ->getFormat($this->view->element['#content_type']) !== 'html') {

    // This display plugin is primarily for returning non-HTML formats.
    // However, we still invoke the renderer to collect cacheability metadata.
    // Because the renderer is designed for HTML rendering, it filters
    // #markup for XSS unless it is already known to be safe, but that filter
    // only works for HTML. Therefore, we mark the contents as safe to bypass
    // the filter. So long as we are returning this in a non-HTML response
    // (checked above), this is safe, because an XSS attack only works when
    // executed by an HTML agent.
    // @todo Decide how to support non-HTML in the render API in
    //   https://www.drupal.org/node/2501313.
    $build['#markup'] = ViewsRenderPipelineMarkup::create($build['#markup']);
  }
  parent::applyDisplayCachablityMetadata($build);
  return $build;
}