Commit 84f61a7c authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2794745 follow-up by borisson_, mkalkbrenner, mpp, Nick_vh, drunken...

Issue #2794745 follow-up by borisson_, mkalkbrenner, mpp, Nick_vh, drunken monkey, sam0971: Use Search API's display plugin to fix facets
parent 425d17d3
......@@ -13,6 +13,7 @@ use Drupal\search_api\Query\QueryInterface;
use Drupal\views\Entity\View;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Drupal\Core\Logger\RfcLogLevel;
/**
* Implements hook_help().
......@@ -82,13 +83,13 @@ function facets_entity_presave(EntityInterface $entity) {
// Check if the current display is also a facet source plugin and that
// is removed from the view. We use the double underscore here to make
// sure that we use core convention of "plugin:derived_plugin".
$test = 'views_page:' . $entity->id() . '__' . $display['id'];
if (array_key_exists($test, $definitions) && !array_key_exists($k, $entity->get('display'))) {
$entity_id = str_replace(':', '__', $test);
$facets_source_plugin_id = 'views_page:' . $entity->id() . '__' . $display['id'];
if (array_key_exists($facets_source_plugin_id, $definitions) && !array_key_exists($k, $entity->get('display'))) {
$entity_id = str_replace(':', '__', $facets_source_plugin_id);
$source_entity = FacetSource::load($entity_id);
$sources[] = $facets_source_plugin_id;
if (!is_null($source_entity)) {
$source_entity->delete();
$sources[] = $test;
}
}
}
......@@ -132,23 +133,26 @@ function facets_preprocess_block(&$variables) {
* when a view is deleted. It also deletes facets that are created on those
* plugins.
*/
function facets_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
function facets_entity_predelete(EntityInterface $entity) {
if ($entity instanceof View) {
$facet_source_plugin_manager = \Drupal::getContainer()
->get('plugin.manager.facets.facet_source');
$definitions = $facet_source_plugin_manager->getDefinitions();
if (!is_array($definitions)) {
return;
}
foreach ($definitions as $plugin_id => $definition) {
if (strpos($plugin_id, 'views_page:' . $entity->id() . '__') !== FALSE) {
try {
$facetManager = \Drupal::getContainer()->get('facets.manager');
} catch (ServiceNotFoundException $e) {
\Drupal::logger('facets')->log(Drupal\Core\Logger\RfcLogLevel::DEBUG, 'Facet manager not found on trying to delete a view.');
\Drupal::logger('facets')->log(RfcLogLevel::DEBUG, 'Facet manager not found on trying to delete a view.');
return;
}
$facets = $facetManager->getFacetsByFacetSourceId($plugin_id);
foreach ($facets as $facet) {
$facet->delete();
......
......@@ -484,11 +484,14 @@ class Facet extends ConfigEntityBase implements FacetInterface {
* {@inheritdoc}
*/
public function getFacetSource() {
if (!$this->facet_source_instance && $this->facet_source_id) {
/* @var $facet_source_plugin_manager \Drupal\facets\FacetSource\FacetSourcePluginManager */
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
$this->facet_source_instance = $facet_source_plugin_manager->createInstance($this->facet_source_id, ['facet' => $this]);
if (!$facet_source_plugin_manager->hasDefinition($this->facet_source_id)) {
return;
}
$this->facet_source_instance = $facet_source_plugin_manager
->createInstance($this->facet_source_id, ['facet' => $this]);
}
return $this->facet_source_instance;
......@@ -540,13 +543,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
[
'id' => $source_id,
'name' => $this->facet_source_id,
'filter_key' => 'f',
'url_processor' => 'query_string',
],
'facets_facet_source'
);
$facet_source->save();
$this->facetSourceConfig = $facet_source;
return $this->facetSourceConfig;
return $facet_source;
}
/**
......
......@@ -228,7 +228,7 @@ interface FacetInterface extends ConfigEntityInterface {
/**
* Returns the plugin instance of a facet source.
*
* @return \Drupal\facets\FacetSource\FacetSourcePluginInterface
* @return \Drupal\facets\FacetSource\FacetSourcePluginInterface|null
* The plugin instance for the facet source.
*/
public function getFacetSource();
......
......@@ -268,7 +268,7 @@ class DefaultFacetManager {
// is not available on the page. Returning an empty array here is enough
// to halt all further processing.
$facet_source = $facet->getFacetSource();
if (!$facet_source->isRenderedInCurrentRequest()) {
if (is_null($facet_source) || !$facet_source->isRenderedInCurrentRequest()) {
return [];
}
}
......
......@@ -76,7 +76,7 @@ class UrlIntegrationTest extends WebTestBase {
$this->assertTrue($facet instanceof FacetInterface);
$config = $facet->getFacetSourceConfig();
$this->assertTrue($config instanceof FacetSourceInterface);
$this->assertEqual(NULL, $config->getFilterKey());
$this->assertEqual('f', $config->getFilterKey());
$facet = NULL;
$config = NULL;
......
......@@ -106,7 +106,7 @@ class UrlProcessorHandlerTest extends UnitTestCase {
$em = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeManagerInterface')
->disableOriginalConstructor()
->getMock();
$em->expects($this->exactly(2))
$em->expects($this->exactly(1))
->method('getStorage')
->willReturn($storage);
......
......@@ -249,6 +249,9 @@ class QueryStringTest extends UnitTestCase {
->getMock();
$manager->method('createInstance')
->willReturn($fsi);
$manager->method('hasDefinition')
->with('facet_source__dummy')
->willReturn(TRUE);
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
$em = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeManagerInterface')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment