From cc4b644a081fb7096bfcc54327a4f245cc488d6a Mon Sep 17 00:00:00 2001
From: "Frederic G. MARAND" <fgm@osinet.fr>
Date: Tue, 23 Jun 2015 15:45:20 +0200
Subject: [PATCH] Issue #2510672: implement ResponseDisplayPluginInterface

---
 .../views.view.autocompletion_callbacks_words.yml  |   37 +++++++++++++++++++-
 .../views/display/AutocompletionCallback.php       |   28 +++++++++++++--
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/config/optional/views.view.autocompletion_callbacks_words.yml b/config/optional/views.view.autocompletion_callbacks_words.yml
index a786bef..6de114e 100644
--- a/config/optional/views.view.autocompletion_callbacks_words.yml
+++ b/config/optional/views.view.autocompletion_callbacks_words.yml
@@ -509,7 +509,40 @@ display:
           entity_type: node
           entity_field: uid
           plugin_id: standard
-      arguments: {  }
+      arguments:
+        keys:
+          id: keys
+          table: node_search_index
+          field: keys
+          relationship: none
+          group_type: group
+          admin_label: ''
+          default_action: ignore
+          exception:
+            value: all
+            title_enable: false
+            title: All
+          title_enable: false
+          title: ''
+          default_argument_type: fixed
+          default_argument_options:
+            argument: ''
+          default_argument_skip_url: false
+          summary_options:
+            base_path: ''
+            count: true
+            items_per_page: 25
+            override: false
+          summary:
+            sort_order: asc
+            number_of_records: 0
+            format: default_summary
+          specify_validation: false
+          validate:
+            type: none
+            fail: 'not found'
+          validate_options: {  }
+          plugin_id: search
       display_extenders: {  }
     cache_metadata:
       contexts:
@@ -518,6 +551,7 @@ display:
         - url
         - 'url.query_args.pagers:0'
         - 'user.node_grants:view'
+        - user.permissions
       cacheable: false
   words_autocompletion_callback:
     display_plugin: autocompletion_callback
@@ -542,4 +576,5 @@ display:
         - 'languages:language_interface'
         - url
         - 'user.node_grants:view'
+        - user.permissions
       cacheable: false
diff --git a/src/Plugin/views/display/AutocompletionCallback.php b/src/Plugin/views/display/AutocompletionCallback.php
index 66c978a..6a0c2e3 100644
--- a/src/Plugin/views/display/AutocompletionCallback.php
+++ b/src/Plugin/views/display/AutocompletionCallback.php
@@ -8,9 +8,12 @@
 
 namespace Drupal\search_autocomplete\Plugin\views\display;
 
+use Drupal\Core\Cache\CacheableMetadata;
+use Drupal\Core\Cache\CacheableResponse;
 use Drupal\Core\State\StateInterface;
 use Drupal\Core\Routing\RouteProviderInterface;
 use Drupal\Core\ContentNegotiation;
+use Drupal\views\Plugin\views\display\ResponseDisplayPluginInterface;
 use Drupal\views\ViewExecutable;
 use Drupal\views\Plugin\views\display\PathPluginBase;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -32,7 +35,7 @@ use Symfony\Component\Routing\RouteCollection;
  *   autocompletion_callback_display = TRUE
  * )
  */
-class AutocompletionCallback extends PathPluginBase {
+class AutocompletionCallback extends PathPluginBase implements ResponseDisplayPluginInterface {
 
   /**
    * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAJAX.
@@ -95,6 +98,26 @@ class AutocompletionCallback extends PathPluginBase {
   /**
    * {@inheritdoc}
    */
+  public static function buildResponse($view_id, $display_id, array $args = []) {
+    $build = static::buildBasicRenderable($view_id, $display_id, $args);
+
+    /** @var \Drupal\Core\Render\RendererInterface $renderer */
+    $renderer = \Drupal::service('renderer');
+
+    $output = $renderer->renderRoot($build);
+
+    $response = new CacheableResponse($output, 200);
+    $cache_metadata = CacheableMetadata::createFromRenderArray($build);
+    $response->addCacheableDependency($cache_metadata);
+
+    $response->headers->set('Content-type', $build['#content_type']);
+
+    return $response;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
     return new static(
       $configuration,
@@ -227,8 +250,7 @@ class AutocompletionCallback extends PathPluginBase {
   public function execute() {
     parent::execute();
 
-    $output = $this->view->render();
-    return new Response(drupal_render_root($output), 200, array('Content-type' => $this->getMimeType()));
+    return $this->view->render();
   }
 
   /**
-- 
1.7.9.5

