Commit 3ca4f4bb authored by StryKaizer's avatar StryKaizer Committed by StryKaizer

Issue #2626458 by borisson_, StryKaizer: Refactor getPath to use facet source directly

parent 68d5a944
......@@ -128,6 +128,15 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $facet_source_id;
/**
* The facet source belonging to this facet.
*
* @var \Drupal\facetapi\FacetSourceInterface
*
* @see getFacetSource()
*/
protected $facet_source_instance;
/**
* The path all the links should point to.
*
......@@ -345,10 +354,14 @@ class Facet extends ConfigEntityBase implements FacetInterface {
* {@inheritdoc}
*/
public function getFacetSource() {
/** @var $facet_source_plugin_manager \Drupal\facetapi\FacetSource\FacetSourcePluginManager */
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facetapi.facet_source');
return $facet_source_plugin_manager->createInstance($this->facet_source_id);
if (!$this->facet_source_instance && $this->facet_source_id) {
/** @var $facet_source_plugin_manager \Drupal\facetapi\FacetSource\FacetSourcePluginManager */
$facet_source_plugin_manager = \Drupal::service('plugin.manager.facetapi.facet_source');
$this->facet_source_instance = $facet_source_plugin_manager->createInstance($this->facet_source_id);
}
return $this->facet_source_instance;
}
/**
......@@ -476,20 +489,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return array_intersect_key($this->facetSourcePlugins, array_flip($this->facetSourcePlugins));
}
/**
* {@inheritdoc}
*/
public function setPath($path) {
$this->path = $path;
}
/**
* {@inheritdoc}
*/
public function getPath() {
return $this->path;
}
/**
* {@inheritdoc}
*/
......
......@@ -197,20 +197,6 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function getFacetSources($only_enabled = TRUE);
/**
* Get the path to which the facet should link.
*
* @param string $path
*/
public function setPath($path);
/**
* Get the path to which the facet should link.
*
* @return NULL|string
*/
public function getPath();
/**
* Returns an array of processors with their configuration.
*
......
......@@ -300,10 +300,6 @@ class DefaultFacetManager {
$facet_source_plugin = $this->facet_source_manager->createInstance($this->facetsource_id);
$facet_source_plugin->fillFacetsWithResults($this->facets);
foreach ($this->facets as $facet) {
$facet->setPath($facet_source_plugin->getPath());
}
}
}
......@@ -79,8 +79,8 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase {
$result_get_params->set($this->filter_key, $filter_params);
$request = $this->request;
if ($facet->getPath()) {
$request = Request::create('/' . $facet->getPath());
if ($facet->getFacetSource()->getPath()) {
$request = Request::create('/' . $facet->getFacetSource()->getPath());
}
$url = Url::createFromRequest($request);
$url->setOption('query', $result_get_params->all());
......
......@@ -79,6 +79,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
public function testEmptyBuild() {
$facet = new Facet([], 'facet');
$facet->setFacetSourceId('facet_source__dummy');
$request = new Request;
$request->query->set('f', []);
......@@ -91,11 +92,12 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
public function testBuild() {
$facet = new Facet([], 'facet');
$facet->setFieldIdentifier('test');
$facet->setFacetSourceId('facet_source__dummy');
$request = new Request;
$request->query->set('f', []);
$this->setRouter();
$this->setContainer();
$this->processor = new QueryStringUrlProcessor([], 'query_string', [], $request);
$results = $this->processor->build($facet, $this->original_results);
......@@ -110,6 +112,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
public function testBuildWithActiveItem() {
$facet = new Facet([], 'facet');
$facet->setFieldIdentifier('test');
$facet->setFacetSourceId('facet_source__dummy');
$original_results = $this->original_results;
$original_results[2]->setActiveState(TRUE);
......@@ -117,7 +120,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
$request = new Request;
$request->query->set('f', ['king:kong']);
$this->setRouter();
$this->setContainer();
$this->processor = new QueryStringUrlProcessor([], 'query_string', [], $request);
$results = $this->processor->build($facet, $original_results);
......@@ -134,7 +137,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
}
}
protected function setRouter() {
protected function setContainer() {
$router = $this->getMockBuilder('Drupal\Tests\Core\Routing\TestRouterInterface')
->disableOriginalConstructor()
->getMock();
......@@ -147,8 +150,21 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
]
);
$fsi = $this->getMockBuilder('\Drupal\facetapi\FacetSource\FacetSourceInterface')
->disableOriginalConstructor()
->getMock();
$fsi->method('getPath')
->willReturn('search/test');
$manager = $this->getMockBuilder('Drupal\facetapi\FacetSource\FacetSourcePluginManager')
->disableOriginalConstructor()
->getMock();
$manager->method('createInstance')
->willReturn($fsi);
$container = new ContainerBuilder();
$container->set('router.no_access_checks', $router);
$container->set('plugin.manager.facetapi.facet_source', $manager);
\Drupal::setContainer($container);
}
......
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