Problem/Motivation

@see d7 issue https://www.drupal.org/node/1440016

Proposed resolution

An optional setting to render an "All"-link per facet.
It should apply for the default widgets "List of checkboxes", "Dropdown" and "List of links".
The default-text "All" should be editable and translatable.

Current status

Widget/Options  | Undo/Reset   | All
------------------------------------------------
Dropdown Widget | -            | Implemented
Links Widget    | Implemented  | Not Implemented
Checkbox Widget | Implemented  | -

Remaining tasks

Discuss if it makes more sense to add a hook (hook_facets_items_alter()) and implement that after sorting? Or keep to the current way of working.

Build.

  1. Add sorting to make sure that the ALL link is always the first result from the processor (implement sortResults?)
  2. Write a unit test that makes sure this works
  3. Add an integration test so that this always the first result. Keep in mind settings like sort a-z|z-a, sort by value, hard limit, ...
  4. Discuss if we need to set this all link at the end of the results as well? (in that case, we should add an option to this processor (all=first || all=last))
CommentFileSizeAuthor
#99 facet_reset_link-checkbox-widget-compatibility-2692027-99.patch716 bytesniko-
#10 all_link_in_facets_-2692027-9.patch7.36 KBborisson_
#11 all_link_in_facets_-2692027-11.patch7.89 KBjohnwebdev
#16 all_links_in_facets-2692027-16.patch7.37 KBjohnwebdev
#16 interdiff-2692027-9-16.txt640 bytesjohnwebdev
#17 all_links_in_facets-2692027-16.patch7.37 KBjohnwebdev
#18 all_links_in_facets_-2692027-16.patch7.37 KBjohnwebdev
#25 all_links_in_facets-2692027-25.patch7.33 KBCountPacMan
#26 all_link_in_facets_-2692027-26.patch3.17 KBborisson_
#34 facets_all-links-in-facets_2692027_34.patch2.16 KBicurk
#37 facets_all-links-in-facets_2692027_37.patch2.18 KBmErilainen
#38 facets_all-links-in-facets_2692027_38.patch2.47 KBmErilainen
#39 interdiff-2692027.txt6.89 KBborisson_
#39 all_link_in_facets_-2692027-39.patch8.36 KBborisson_
#41 interdiff-2692027.txt2.42 KBborisson_
#41 all_link_in_facets_-2692027-41.patch10.78 KBborisson_
#44 all_link_in_facets_-2692027-44.patch10.78 KBborisson_
#44 interdif-2692027.txt439 bytesborisson_
#48 all_link_in_facets_-2692027-47.patch11.51 KBicurk
#50 intedriff-2692027-44-48.txt1.58 KBicurk
#52 all_link_in_facets_-2692027-52.patch10.8 KBborisson_
#55 all_link_in_facets_count-2692027-55.patch11.08 KBtomhollevoet
#57 all_link_in_facets_count-2692027-56.patch11.17 KBtomhollevoet
#60 all_link_in_facets_count-2692027-60.patch8.02 KBabu-zakham
#63 interdiff-2692027.txt4.07 KBborisson_
#63 all_link_in_facets_-2692027-63.patch11.58 KBborisson_
#66 2692027.patch14.12 KBborisson_
#68 interdiff-2692027.txt1.38 KBborisson_
#68 2692027.patch14.59 KBborisson_
#70 2692027_70.patch15.08 KBsocketwench
#70 interdiff_2692027_0-70.txt866 bytessocketwench
#75 facets-reset_link-2692027-75.patch15.09 KBstBorchert
#78 2692027-78.patch14.65 KBStryKaizer
#78 interdiff.txt3 KBStryKaizer
#81 Selection_673.png14.13 KBMiroslavBanov
#82 2692027-interdiff-78-82.patch2.34 KBMiroslavBanov
#82 2692027-82-facet_reset_link.patch15.13 KBMiroslavBanov
#85 2692027-85-facet_reset_link--8.x-1.0-beta2.patch15.11 KBgrndlvl
#90 facet_reset_link-2692027-90.patch15.34 KBdagomar
#90 interdiff_82-90.txt762 bytesdagomar
#91 facet_reset_link-2692027-91.patch15.33 KBdagomar
#91 interdiff_82-91.txt750 bytesdagomar
#92 innerdiff_91-92.txt5.61 KBlapek
#92 facet_reset_link-2692027-92.patch17.4 KBlapek
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ndf created an issue. See original summary.

ndf’s picture

Issue summary: View changes
borisson_’s picture

Issue summary: View changes

This sounds like a great idea.

joachim’s picture

The dropdown widget seems to already have this, though:

- when first set up, the dropdown option for this has no text
- the text can set with the 'Default option label' option, which seems confusing wording to me

borisson_’s picture

I think we should postpone this on #2725453: Refactor widget plugins by adding interface, base class, schema to make this easier to implement.

borisson_’s picture

So, the issue we postponed this work on is now in. The slider widget that @mollux is building will also need this.

We should add a "reset_link_active" boolean and "reset_link_label" string in the schema.yml and the widget base class.

The reset link active should be a checkbox to enable the link and the label should be a textbox to add the label for the link.

johnwebdev’s picture

I believe we also want to make sure we can add the total count of the search on the All (reset link)

johnwebdev’s picture

To have a reset link on the Widget, I kinda did a "hack"-solution for the moment, since we needed it asap. It might come in handy for someone.

This code is placed in LinksWidget.php

    $totalCount = array_map(function (Result $result) {
      return $result->getCount();
    }, $facet->getResults());

    $request = \Drupal::request();
    $url = Url::createFromRequest($request);

    $params = $request->query->all();
    unset($params['f']); // This removes all Facets filters, beaware if you have multiple.

    $url->setOption('query', $params);

    $item = [
        '#theme' => 'facets_result_item',
        '#is_active' => FALSE,
        '#value' => 'All',
        '#show_count' => TRUE,
        '#count' => array_sum($totalCount),
    ];
    $item = (new Link($item, $url))->toRenderable();
    array_unshift($build['#items'], $item);

borisson_’s picture

Status: Active » Needs review

This patch is based on @johndevman's "hack", not sure if this is the best solution but it actually works in my limited testing, this needs validation if this is a good way to do this and if it's validated, we should add tests, both an integration test and a unit test adaption for the QueryStringTest unit test.

borisson_’s picture

Actually attaching the patch now.

johnwebdev’s picture

array_map

returns an array. So we need to use array_sum to calculate the count of the All link. Attached patch:

Status: Needs review » Needs work

The last submitted patch, 11: all_link_in_facets_-2692027-11.patch, failed testing.

The last submitted patch, 11: all_link_in_facets_-2692027-11.patch, failed testing.

johnwebdev’s picture

I think I submitted the patch wrong?

borisson_’s picture

You need to create the patch from the facets directory, looks like you created it from the root-drupal directory. Can you also provide an interdiff?

johnwebdev’s picture

Here we go again:

johnwebdev’s picture

johnwebdev’s picture

Ugh... @borisson_ could you help me out here? Not sure why it's not prompted for testing. Sorry for the spam.

borisson_’s picture

Status: Needs work » Needs review

The issue needs to have "Needs review" status for the tesbot to pick up the patch. Changed status

johnwebdev’s picture

It seems to work as expected. Do we have any other input on this? I could provide the integration and unit tests required otherwise.

borisson_’s picture

The more I look at this, the more I'm unsure about the direction we took here, by adding the config directly into the facet entity and not providing a processor that does this. I think that adding a processor would create more work for the sort processors as they shouldn't just sort this link with the other items but I'm not sure about the architecture at this point.

I wanted to discuss that at the last facets hangout, but we cancelled that because of time restrictions. I'll try to ping @StryKaizer to see if he has an opinion, once we've validated that this is the correct way to do this we can write unit + integration tests.

But we first need to validate that this is a good solution.

borisson_’s picture

Status: Needs review » Active

We had a short discussion about this in irc, StryKaizer agreed that this should not live on the facet config.

borisson_: StryKaizer: if you have some time later: https://www.drupal.org/node/2692027 needs validation if the patch is the correct way to resolve the issue.
StryKaizer: borisson_ : gave it a quick look just now, and I share your opinion at first sight, this should not live on the facet config indeed
StryKaizer: It prolly needs to be a widget setting
borisson_: StryKaizer: oh, yeah. That makes a ton more sense
StryKaizer: We could make an interface for this, of many widgets will use it
StryKaizer: Not sure though

This means that both the initial assesment in #6 and the patch I created in #10 are not a good solution.

Setting this issue back to active. The first thing we should do is write and decided a good way to do resolve this before diving into the code again.

Sorry for the work you already put into this @johndevman.

johnwebdev’s picture

It's better to make a good solution, rather than a temporary one. So no worries. I'll see If I can contribute some once some initial work been done on the issue.

So If I got it right, the idea is to move these configuration up a level to the Widget, which decides if it should render an Reset link or not, rather then on the Facet which enforce a Widget to handle a reset button.
Is that correct?

borisson_’s picture

Yep, that sounds like a better solution.

CountPacMan’s picture

I understand this is being rewritten, but I can confirm that the patch gets the job done. For those that need the functionality right away, I've re-rolled the patch to apply cleanly on the latest dev build:

Last updated: 20 Dec 2016 at 11:38 PST
Last packaged version: 8.x-1.0-alpha6+45-dev
Git Hash: 445efcac0e76cb870e2f22a7c12bf6542a862c6a

borisson_’s picture

FileSize
3.17 KB

This is the approach with the processor. Not fully tests yet but this is a good start I think, trying to confirm that later.

borisson_’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 26: all_link_in_facets_-2692027-26.patch, failed testing.

johnwebdev’s picture

Isn't this approach similar to the first one? It enforces an Add link, but not actually anything confirming that the widget will handle it, for example the Dropdown widget already have an All link, with this one enabled it would show 2 all options. Per se it's not that much of issue, but imo confusing.

borisson_’s picture

In functionality this doesn't really change a lot, that's true. However this is a processor so we're not baking it into the facet object and thus making it easier to swap out with a custom implementation. While it is true that the current dropdown widget already has an all link, this can be replaced with this approach. Since we're adding #2611198: Give widgets the ability to require settings we can make the dropdown widget require this processor.

johnwebdev’s picture

But doesn't that mean we actually enforce an All link to the widget? Maybe that's okay, but I believe it makes more sense for it to be an option, not an requirement.

jummonk’s picture

This processor results in an error on my site.
Error: Call to undefined method Drupal\facets\Plugin\facets\processor\AddAllLink::sortResults() in Drupal\facets\FacetManager\DefaultFacetManager->Drupal\facets\FacetManager\{closure}() (line 334 of modules/contrib/facets/src/FacetManager/DefaultFacetManager.php).

In my facet config, this processor seems to be added automatically to the 'sort' stage so DefaultFacetManager calls this method sortResults on it.

borisson_’s picture

Issue summary: View changes

Yeah, this is because the processor also defines that stage and doesn't add the method. I think that's also the reason why there's so many failures on that patch. I figured we'd also need to include a implementation to sort because I don't think we want to have the 'all link' sorted alphabetically. I was half-way trough trying to figure that out when I uploaded this patch. This is currently very low on the priority-list for me but if you (or anyone really) would want to work on this I've updated the IS.

icurk’s picture

I created a patch for 'List of links' widget, which adds an option to add "Show all" link in the facet widget. I also made text on link configurable on widget configuration form.

borisson_’s picture

Status: Needs work » Needs review

Let's see what the test thinks

borisson_’s picture

Status: Needs review » Needs work

Needs work for the test

mErilainen’s picture

For me the patch was not working as expected, seems that whatever the first element in the results array is, the reset link will activate the same facet. Clearing the query value on that url object should take user to the path where all facets are cleared. Patch provided. Tests still needed.

mErilainen’s picture

Darn, the $results[0] url object was being altered with that approach. Creating a new object seems to work better, patch attached.

borisson_’s picture

It looks like the reset link doesn't have the same classes as a normal facet item.

Further, this will reset all facets at once, not just this facet. I think that's a problem.

In any case, this adds test-coverage on both unit as integration level.

Status: Needs review » Needs work

The last submitted patch, 39: all_link_in_facets_-2692027-39.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

borisson_’s picture

FileSize
2.42 KB
10.78 KB

Looks like there was no config declaration. I fixed that, however tests don't pass yet locally. I don't have more time today so uploading current work.

borisson_’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 41: all_link_in_facets_-2692027-41.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

borisson_’s picture

Status: Needs work » Needs review
FileSize
10.78 KB
439 bytes

This should do it, at least for the kernel test.

borisson_’s picture

We have sufficient test-coverage now, I'd love if anyone here can manually test and rtbc this patch.

icurk’s picture

This patch removes all selected facets. Initially, I wrote a patch (#34), which removes just facet selection from one facet widget, not all (this was the use case we needed in our project). If this is the feature you want, then this patch works. Maybe we could add another setting, where users could choose if they want to reset all facets or just this one. What do you think?

borisson_’s picture

Status: Needs review » Needs work

#46, this is not what's intended, let's expand the testcoverage to make sure that we include that and fix it in the patch.

It should reset only one facet.

icurk’s picture

Here is a patch, which resets only one facet. Tests need to be updated.

borisson_’s picture

@icurk: do you happen to have an interdiff to #44?

icurk’s picture

FileSize
1.58 KB

Here is interdiff between #44 and #48.

mpp’s picture

Cfr An option to add a (block) link to reset all facets at once would be a valuable UX improvement.

borisson_’s picture

Rerolled.

borisson_’s picture

Status: Needs work » Needs review

Go testbot, go!

borisson_’s picture

Issue tags: +DrupalCampBE
tomhollevoet’s picture

Refactoring of fantastic patch 52 (https://www.drupal.org/files/issues/all_link_in_facets_-2692027-52.patch) from @borisson_
If the checkbox 'Show the amount of results' on the facet is checked, then show the total number of results between brackets.

Status: Needs review » Needs work

The last submitted patch, 55: all_link_in_facets_count-2692027-55.patch, failed testing. View results

tomhollevoet’s picture

FileSize
11.17 KB

Refactoring of fantastic patch 52 (https://www.drupal.org/files/issues/all_link_in_facets_-2692027-52.patch) from @borisson_
If the checkbox 'Show the amount of results' on the facet is checked, then show the total number of results between brackets.

tomhollevoet’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 57: all_link_in_facets_count-2692027-56.patch, failed testing. View results

abu-zakham’s picture

Hello, After apply the patch I noticed 'All' remove the query string from the URL, I've updated the latest patch to work latest dev version and fixed the query string issue.

abu-zakham’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 60: all_link_in_facets_count-2692027-60.patch, failed testing. View results

borisson_’s picture

Assigned: Unassigned » StryKaizer
FileSize
4.07 KB
11.58 KB

I think we should make use of the new url generator class to do this. This is also a reroll that actually applies and uses the new service.

It doesn't work yet - and doesn't passes tests either, I hope @StryKaizer can have a look at this.

borisson_’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 63: all_link_in_facets_-2692027-63.patch, failed testing. View results

borisson_’s picture

Assigned: StryKaizer » Unassigned
Status: Needs work » Needs review
FileSize
14.12 KB

No interdiff, because this didn't apply anymore. tests should be green.

Status: Needs review » Needs work

The last submitted patch, 66: 2692027.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

borisson_’s picture

Status: Needs work » Needs review
FileSize
1.38 KB
14.59 KB

I forgot to copy over the config.

socketwench’s picture

Status: Needs review » Needs work

This works, but the resulting link doesn't have any special class to identify the list item for the reset link, making it a pain to theme.

Also, unlike any of the other facet links, it's always set to being inactive, which works for a "reset" link, but not for designs that want to have an "All results" link and have that highlighted when no other facet is active.

Probably also needs tests?

socketwench’s picture

Status: Needs work » Needs review
FileSize
15.08 KB
866 bytes
  • Adds a 'facets-reset' class
  • Adds an is-active class when no other facets are active.

Status: Needs review » Needs work

The last submitted patch, 70: 2692027_70.patch, failed testing. View results

josephdpurcell’s picture

I did not review this, however I did a quick test. I am using Search API 8.x-1.7. I am using facets alpha11 for which #70 does not apply.

First, I updated facets to the latest 8.x-1.x branch, applied the patch (it applies successfully). I ran drush updb (there are no updates). Then, I ran cache flush. I confirmed search functionality still works as expected (i.e. confirmed that what used to work still works).

Next, I edited the facet and see the reset options show! My facet is set to "List of links". I checked "Show reset link" and noticed the "Reset text" input appears and is required; I set it to the text "All Results". I went to the search page and clicked a facet and confirmed the result set is limited to that facet item. Then, I clicked "All Results" and confirmed the result set cleared to showing all results.

This is exactly the functionality I was needing! Thank you!!! I hope this feedback is helpful / affirming of the behavior in #70.

borisson_’s picture

Status: Needs work » Needs review

Yes, thanks! This still needs tests as mentioned in #70, so tagging as such. Hopefully I can get to that soon.

ndf’s picture

Regarding #70
For consistency I prefer a slight change in the class-naming. The facet-reset link should also have the 'facet-item' class in my opinion.

This way we can expect that the link will be styled when enabling this new functionality. Styling can always be overridden with the additional facets-reset class.

Patch #70

// Reset link not active
<div class="facets-widget-links">
<ul class="item-list__links">
  <li class="facets-reset"><a href="#">Reset link in-active</a></li>
  <li class="facet-item"><a href="#" class="is-active">Facet-link active</a></li>
  <li class="facet-item"><a href="#">Facet link in-active</a></li>
</ul>
</div>

// Reset link active
<div class="facets-widget-links">
<ul class="item-list__links">
  <li class="facets-reset"><a href="#" class="is-active">Reset link in-active</a></li>
  <li class="facet-item"><a href="#">Facet-link in-active</a></li>
  <li class="facet-item"><a href="#">Facet link in-active</a></li>
</ul>
</div>

Expected

// Reset link not active
<div class="facets-widget-links">
<ul class="item-list__links">
  <li class="facet-item facets-reset"><a href="#">Reset link in-active</a></li>
  <li class="facet-item"><a href="#" class="is-active">Facet-link active</a></li>
  <li class="facet-item"><a href="#">Facet link in-active</a></li>
</ul>
</div>

// Reset link active
<div class="facets-widget-links">
<ul class="item-list__links">
  <li class="facet-item facets-reset"><a href="#" class="is-active">Reset link in-active</a></li>
  <li class="facet-item"><a href="#">Facet-link in-active</a></li>
  <li class="facet-item"><a href="#">Facet link in-active</a></li>
</ul>
</div>

Notice <li class="facet-item facets-reset"> instead of <li class="facets-reset">.

stBorchert’s picture

Updated the patch and added the class facet-item as suggested.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Looks super solid.

ndf’s picture

+1 RTBC nice work

StryKaizer’s picture

Status: Reviewed & tested by the community » Needs work
FileSize
14.65 KB
3 KB

Attached patch fixes following issues

- hide "show all" link when all items are already shown
- generate correct url in show all link, as currently the active-items array was incorrect, which caused pretty paths to show the wrong url.

I did not yet look into the tests, so they are probably broken now.

borisson_’s picture

Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Needs work

NW based on test in #78.

MiroslavBanov’s picture

FileSize
14.13 KB

#78

- hide "show all" link when all items are already shown

Actually there is genuine use case for showing the "show all" link when all items are already shown. See this image:
All results is show

MiroslavBanov’s picture

Status: Needs work » Needs review
FileSize
2.34 KB
15.13 KB

- hide "show all" link when all items are already shown

I exposed this one as an option. Patch test will fail for the same reason as the previous one.

The last submitted patch, 82: 2692027-interdiff-78-82.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 82: 2692027-82-facet_reset_link.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

grndlvl’s picture

ndf’s picture

grndlvl Can you add an interdiff with #75 ?
That was the last patch that was RTBC and had green tests.

grndlvl’s picture

@ndf,

Nothing to add. Just needed one rolled against a released version. Hid it from the list.

Thx

TuWebO’s picture

Hello,
Patch in #2692027-82: "All" link in facets? worked for me. I wonder if we could add an option in the facet configuration adding something like "Reset this facet" (maybe only if Show only one result is configured). In the code something like this (I don't know what could happen, I've just do a PoC and worked for my use case):

Drupal\facets\Plugin\facets\url_processor\QueryString()

        // Exclude currently active results from the filter params if we are in
        // the show_only_one_result mode.
        if ($facet->getShowOnlyOneResult()) {
          foreach ($results as $result2) {
            if ($result2->isActive()) {
              $active_filter_string = $this->urlAlias . $this->getSeparator() . $result2->getRawValue();
              foreach ($filter_params as $key2 => $filter_param2) {
                if ($filter_param2 == $active_filter_string) {
                  unset($filter_params[$key2]);
                }
              }
            }
          }
        }

        // For the example, something like:
        if ($facet->getResetMe()) {
          $filter_params = [];
          foreach ($results as $result2) {
            if ($result === $result2) {
              $filter_params[] = $this->urlAlias . $this->getSeparator() . $result2->getRawValue();
            }

Could this workaround work if we configure the "parent" facet (the one which dependent facets depend) to be reseted?

dagomar’s picture

This patch does not take into account when there are other - non-facets - query parameter values present in the url. This means that for instance a view with an exposed search filter will not work because it removes that from the query parameters as well. That means that the solution in #8 actually works as intended, only remove the current filter from the query string leaving all else intact.

dagomar’s picture

I've created a patch which takes into account existing query parameters.

dagomar’s picture

FileSize
15.33 KB
750 bytes

I noticed my previous patch didn't have correct indenting so I've updated the patch.

lapek’s picture

Rerolling for better testing (maybe?)

borisson_’s picture

Status: Needs work » Reviewed & tested by the community

This looks good now, thanks @lapek! I think this now has sufficient test-coverage as well. Setting to rtbc to commit the next time I get to committing things :)

  • borisson_ committed 21343a6 on 8.x-1.x
    Issue #2692027 by borisson_, johndevman, dagomar, icurk, MiroslavBanov,...
borisson_’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed, thanks!

ndf’s picture

Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

pfrenssen’s picture

There seem to be some unintended side effects from this feature addition. If I update to 8.x-1.4 suddenly this facet is popping up on pages where it was not visible when using 8.x-1.3, and it is making these pages uncacheable.

I had to lock on the previous version 8.x-1.3 for the time being.

edit: False alarm! After investigation this turned out to be caused by a theming issue in our custom facets widget, Facets is working fine!

niko-’s picture

Thanks All for great work.

Found one point - reset link in not compatible with checkbox widget.

Patch attached

recrit’s picture

@niko-, closed tickets do not get re-patched. I created a new ticket #3064344: "All" link in facets: Fix Checkbox Widget based on your report. The patch on 3064344 takes it one step further and uses the buildListItems() so that the reset link is process the same as the other links. Testers welcome.

idebr’s picture

+++ b/src/Plugin/facets/widget/LinksWidget.php
@@ -47,6 +52,64 @@ class LinksWidget extends WidgetPluginBase {
+      $max_items = array_sum(array_map(function ($item) {
+        return $item->getCount();
+      }, $results));

This amount is incorrect when:

  1. a result has multiple values for a field
  2. not every result has an associated facet item (eg. field is optional)

I have filed #3087076: Amount of results for 'reset link' is calculated incorrectly to fix the amount of results for the 'reset link'.