I'm creating a Facet based in a taxonomy term,

My settings for the facet are:
* Widget: dropdown
* Hide facet when facet source is not rendered: on
* Ensure that only one result can be displayed: on
* Operator: OR
* Enable parent when child gets disabled: on
* Minimum count: 1
* Sort by taxonomy term weight: on and Ascending sort order

Once I index content, and manually sort the Taxonomy term weight as desired, I go to the page, I can see my facet but it never sort by weight, already cleared cache, reindexed, moved the list multiple times, reindexed, nothing changes.

Comments

cristian100 created an issue. See original summary.

cristian100’s picture

I'm seeing a problem in the code:

facets/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php

/**
   * {@inheritdoc}
   */
  public function sortResults(Result $a, Result $b) {
    kint($a);
    $ids = [$a->getRawValue(), $b->getRawValue()];

    $entities = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadMultiple($ids);

    // Bail out if we could not load the term (eg. if the result is not
    // referring to a term).
    if (empty($entities[$a->getRawValue()]) || empty($entities[$b->getRawValue()])) {
      return 0;
    }

    /** @var \Drupal\taxonomy\Entity\Term $term_a */
    $term_a = $entities[$a->getRawValue()];
    /** @var \Drupal\taxonomy\Entity\Term $term_b */
    $term_b = $entities[$b->getRawValue()];

    if ($term_a->getWeight() === $term_b->getWeight()) {
      return 0;
    }
    return ($term_a->getWeight() < $term_b->getWeight()) ? -1 : 1;
  }

The index field is based in taxonomy term name (field_category:entity:name), and the code above is assuming the field will be an id, which is not always the case, therefore, it can't find the right entities by name. I wish there could be a way to handle this better.

borisson_’s picture

Issue tags: -taxonomy, -sort, -weight

I think doing #2851851: Facet source plugins should provide metadata for facet fields first would make this a lot easier.

PS: It seems you (like many others – it's really easy to misinterpret) are confused by the "Issue tags" field. As the guidelines state, they aren't meant for free text tags related to the issue, but only for specific categorization purposes, usually by module maintainers.
So, if you aren't sure your current usage is correct, please just leave the field empty.

borisson_’s picture

Status: Active » Needs review
StatusFileSize
new1.98 KB

This should only work for actual entities, so let's filter that out.

borisson_’s picture

We should probably also extend the function test coverage for this.

borisson_’s picture

StatusFileSize
new1.97 KB
new3.7 KB

Adds test coverage.

borisson_’s picture

borisson_’s picture

StatusFileSize
new588 bytes
new3.77 KB

I forgot to add a docblock.

Status: Needs review » Needs work

The last submitted patch, 8: sorting_taxonomy_term-2909262-8.patch, failed testing. View results

borisson_’s picture

Status: Needs work » Needs review
StatusFileSize
new1.95 KB
new4.56 KB
ac’s picture

Status: Needs review » Reviewed & tested by the community

Works

borisson_’s picture

Status: Reviewed & tested by the community » Fixed

  • borisson_ committed e1108f0 on 8.x-1.x
    Issue #2909262 by borisson_: Limit taxonomy weight processor to entity...

Status: Fixed » Closed (fixed)

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