I have Views with Facets, that placed on page as block via Layouts Builder. And I place block, contains Facet with "List of links" widget, displayed on all site pages, not only in my views page - with unchecked "Hide facet when facet source is not rendered" I can normally display this block on any page.

But the problem is that widget links of this block targets to current page url, instead of my views page url. So, if I visit/about url, facet block contains links like /about?f[0]=param:value (on /about page) instead of /my-views-page?f[0]=param:value.

In Drupal 7.x, regarding to https://www.drupal.org/docs/7/modules/search-api/advanced-site-building-... , I can add "Facets block" Views display, where can I set "Search page path", but this feature is missing in Drupal 8.x branch.

So, how can I set needed URL for widget items links in Facets block?

Issue fork facets-3057943

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Murz created an issue. See original summary.

murz’s picture

Facet Views Display mode is removed from D8 version #2604906: Remove Facet Views Display mode so is there any replacement for implement same feature?

murz’s picture

Issue summary: View changes
lamp5’s picture

I think that you can try something like this:

/**
 * Implements hook_preprocess_HOOK().
 */
function yourmodule_preprocess_facets_item_list(&$variables) {
 
}
lamp5’s picture

Status: Active » Needs review
StatusFileSize
new7.11 KB

Hmm now I think that we need patch for this....

Status: Needs review » Needs work

The last submitted patch, 5: facets-set_base_path-3057943-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

siavash’s picture

This is a great functionality!

After enabling and saving facets I get this when clearing cache:
Error: Uncaught Error: Call to a member function getBasePath() on null in /web/modules/contrib/facets/src/Plugin/facets/facet_source/SearchApiDisplay.php:146

Also base_path field is not set with my value - but field validation was working in terms of verifying valid URL.

Have not done a ton of testing will probably handle it using preprocess but thanks for working on this! Hope to see it get fixed and committed.

lamp5’s picture

Hi @Siavash. Thanks for your tests. Could you give me steps to reproduce this error? For me, this solution works fine.

ludo.r’s picture

Hello,

Thanks for providing this functionnality, it's really helpful!

However I applied the patch, I can set the base path, I don't have any errors, but the link is still based on the current page and not the base path.

I'm setting it to a specific node: /node/123

lamp5’s picture

HI @dolu. Please check why $this->facet->getBasePath() does not return your path in /src/Plugin/facets/facet_source/SearchApiDisplay.php, then we can improve my solution and find way to fix tests.

ludo.r’s picture

Hi @lamp5,

It IS returning my configured base path /node/123 (I cleared the cache prior testing using drush cr).

But the link still uses the current page path.

lamp5’s picture

@dolu your view with search api results is page or block?

ludo.r’s picture

It is a block.

Here's my configuration with 2 pages:

- Page 1 (/node/456)
-- my_view-block1
-- facet

- Page 2 (/node/123)
-- my_view-block2
-- facet

the facet is linked to my_view-block2

So when being on Page 1, I'd like the facetlinks to point to Page 2.

I can send you the config if needed.

Thanks!

rutiolma’s picture

StatusFileSize
new7.56 KB
new519 bytes

I'm proposing a new patch since I'm getting the same issue as @dolu.
Could this be a solution?

This is a patch for Facets 1.4 and doesn't apply on dev brach.

efpapado’s picture

StatusFileSize
new7.38 KB

This should apply to the dev.

jwwj’s picture

I tried applying the patch in #15, and I'm getting the same symptoms as in #7. Did nothing other than add the patch to my composer.json, run composer update, then "drupal cache:rebuild all". And the exception I get is

Fatal error: Uncaught Error: Call to a member function getBasePath() on null in /var/www/html/modules/contrib/facets/src/Plugin/facets/facet_source/SearchApiDisplay.php:146
Stack trace:
#0 /var/www/html/modules/contrib/facets_pretty_paths/src/RouteSubscriber.php(39): Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay->getPath()
#1 /var/www/html/core/lib/Drupal/Core/Routing/RouteSubscriberBase.php(37): Drupal\facets_pretty_paths\RouteSubscriber->alterRoutes(Object(Symfony\Component\Routing\RouteCollection))
#2 [internal function]: Drupal\Core\Routing\RouteSubscriberBase->onAlterRoutes(Object(Drupal\Core\Routing\RouteBuildEvent), 'routing.route_a...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#3 /var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Drupal\Core\Routing\RouteBuildEvent), 'routing.route_a...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#4 /var/www/html/core/lib/Drupal/Core/Ro in /var/www/html/modules/contrib/facets/src/Plugin/facets/facet_source/SearchApiDisplay.php on line 146

could be because of the facets_pretty_paths module I also have installed, which maybe clashes with this patch. I tried to uninstall facets_pretty_paths. The cache:rebuild worked after that, but apparently it does not uninstall cleanly as I started to get errors in other parts instead... still trying to debug that part.

jwwj’s picture

Ok, tested again without facets_pretty_paths installed, and in this case the patch in #15 worked without errors being thrown. So it seems facets_pretty_paths causes the problem. Considering how common it is afaik to use that module, I suggest trying to get this patch to work both with and without facets_pretty_paths installed. I'll put some time into this today to see if I can get it working.

One thing I also noticed, if the base path I want to use in itself is dynamic, then there doesn't seem to be a way to define that with this patch? E.g. if the path to the base path I want to target would be /node/{nid}, there is no way to define that {nid} should be resolved dynamically, and not just used as an url-encoded string.

mariacha1 made their first commit to this issue’s fork.

mariacha1’s picture

theodorosploumis’s picture

StatusFileSize
new8.31 KB

Patch for #20 attached.

shkiper’s picture

Hi guys,

You may try to use hook_search_api_displays_alter to add the path

/**
 * Implements hook_search_api_displays_alter().
 */
function mymodule_search_api_displays_alter(array &$displays) {
  $displays['views_block:my_display_id']['path'] = '/path-to-the-page-with-block';
}
amoebanath’s picture

StatusFileSize
new8.34 KB

Reroll onto 1.8

cluke009’s picture

StatusFileSize
new6.68 KB

Rerolled this as it reverted this issue https://www.drupal.org/project/facets/issues/3040947

atoll’s picture

Issue summary: View changes
StatusFileSize
new6.57 KB

Rerolled this for the Facets 2.0.0 module version.

mkalkbrenner’s picture

Version: 8.x-1.4 » 2.0.x-dev
mkalkbrenner’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 25: facets-set_base_path-3057943-25.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

atoll’s picture

Issue summary: View changes
proweb.ua’s picture

#24

Fatal error: Uncaught Error: Call to a member function getBasePath() on null in /home/***/modules/contrib/facets/src/Plugin/facets/facet_source/SearchApiDisplay.php:170

ggamba’s picture

Hello,

should this work with dev version updated 16 Dec 2022? How? Is it included in dev or there is patch to apply?

Thanks a lot!

jonathan_hunt’s picture

Patch #25 applies to 3.0.0-beta1 and works. However, I have another facet (a) on the same page as facet (b) with base path. facet (a) does not have base path set, and if facet (a) is submitted first, the base path on facet (b) is ignored when the facets are re-rendered.

mistrae’s picture

StatusFileSize
new6.49 KB

Had to switch isRenderedInCurrentRequest after getBasePath or it was always trigerred by isRenderedInCurrentRequest.

damienmo’s picture

StatusFileSize
new6.79 KB

Adde missing "use Drupal\Core\Path\PathValidatorInterface;" in FacetForm.php Causing an error on facet edition page.

The error was:

TypeError: Drupal\facets\Form\FacetForm::__construct(): Argument #6 ($pathValidator) must be of type Drupal\facets\Form\PathValidatorInterface, Drupal\Core\Path\PathValidator given

damienmo’s picture

StatusFileSize
new6.96 KB

Added config_export in Facet.php