Commit fbfa0ca0 authored by Thomas Seiber's avatar Thomas Seiber
Browse files

Issue #2763161 by drunken monkey, borisson_: Fixed cache issues with Views search display plugins.

parent 7640c7b8
Loading
Loading
Loading
Loading
+2 −0
Changes for CHANGELOG.txt: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
Search API 1.x, dev (xxxx-xx-xx):
---------------------------------
- #2763161 by drunken monkey, borisson_: Fixed cache issues with Views search
  display plugins.
- #2800011 by drunken monkey, borisson_: Fixed display of hidden properties
  when adding fields.
- #2794093 by drunken monkey, borisson_, kamalrajsahu21: Fixed the processor
+40 −0
Changes for search_api.module: 40 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -15,9 +15,11 @@ use Drupal\search_api\Entity\Index;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\search_api\datasource\ContentEntityTaskManager;
use Drupal\search_api\Plugin\search_api\datasource\EntityDatasourceInterface;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\search_api\SearchApiException;
use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;
use Drupal\search_api\Task\IndexTaskManager;
use Drupal\views\ViewEntityInterface;

/**
 * Implements hook_help().
@@ -449,6 +451,44 @@ function search_api_search_api_index_update(IndexInterface $index) {
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert() for type "view".
 */
function search_api_view_insert(ViewEntityInterface $view) {
  _search_api_view_crud_event($view);

}

/**
 * Implements hook_ENTITY_TYPE_update() for type "view".
 */
function search_api_view_update(ViewEntityInterface $view) {
  _search_api_view_crud_event($view);
}

/**
 * Implements hook_ENTITY_TYPE_delete() for type "view".
 */
function search_api_view_delete(ViewEntityInterface $view) {
  _search_api_view_crud_event($view);
}

/**
 * Reacts to a view CRUD event.
 *
 * @param \Drupal\views\ViewEntityInterface $view
 *   The view that was created, changed or deleted.
 */
function _search_api_view_crud_event(ViewEntityInterface $view) {
  // Whenever a view is created, updated (displays might have been added or
  // removed) or deleted, we need to clear our cached display definitions.
  if (SearchApiQuery::getIndexFromTable($view->get('base_table'))) {
    \Drupal::getContainer()
      ->get('plugin.manager.search_api.display')
      ->clearCachedDefinitions();
  }
}

/**
 * Returns a warning message if the Core Search module is enabled.
 *
+9 −0
Changes for src/Display/DisplayPluginManager.php: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -54,4 +54,13 @@ class DisplayPluginManager extends DefaultPluginManager {
    return $this->displays;
  }

  /**
   * {@inheritdoc}
   */
  public function clearCachedDefinitions() {
    parent::clearCachedDefinitions();

    $this->discovery = NULL;
  }

}
+79 −0
Changes for tests/src/Kernel/ViewsDisplayTest.php: 79 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\search_api\Kernel;

use Drupal\KernelTests\KernelTestBase;

/**
 * Tests whether Views pages correctly create search display plugins.
 *
 * @group search_api
 */
class ViewsDisplayTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = array(
    'field',
    'search_api',
    'search_api_db',
    'search_api_test_db',
    'search_api_test_example_content',
    'search_api_test_views',
    'search_api_test',
    'user',
    'system',
    'entity_test',
    'text',
    'views',
  );

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installEntitySchema('entity_test_mulrev_changed');
    $this->installEntitySchema('search_api_task');

    // Do not use a batch for tracking the initial items after creating an
    // index when running the tests via the GUI. Otherwise, it seems Drupal's
    // Batch API gets confused and the test fails.
    if (php_sapi_name() != 'cli') {
      \Drupal::state()->set('search_api_use_tracking_batch', FALSE);
    }

    // Set tracking page size so tracking will work properly.
    \Drupal::configFactory()
      ->getEditable('search_api.settings')
      ->set('tracking_page_size', 100)
      ->save();

    $this->installConfig(array(
      'search_api_test_example_content',
      'search_api_test_db',
    ));
  }

  /**
   * Tests whether the search display plugin for a new view is available.
   */
  public function testViewsPageDisplayPluginAvailable() {
    // Retrieve the display plugins once first, to fill the cache.
    $displays = $this->container
      ->get('plugin.manager.search_api.display')
      ->getDefinitions();
    $this->assertArrayNotHasKey('views_page:search_api_test_view__page_1', $displays);

    // Then, install our test view and see whether its search display becomes
    // available right away, without manually clearing the cache first.
    $this->installConfig('search_api_test_views');
    $displays = $this->container
      ->get('plugin.manager.search_api.display')
      ->getDefinitions();
    $this->assertArrayHasKey('views_page:search_api_test_view__page_1', $displays);
  }

}