Unpublished terms appears in facets

Created on 5 May 2022, about 3 years ago
Updated 15 February 2023, over 2 years ago

Problem/Motivation

If a taxonomy term is unpublished, but a node is tagged with it, the term still appears in the facets as a filter option, also to anonymous users.

Steps to reproduce

Create a term in a taxonomy vocabulary that is used in a facet;
Tag a node with that term;
Unpublish the term;
The term does not appear on the node page but still appears in the facet as a filter option.

Proposed resolution

Unpublished terms should not appear in facets.

🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

🇪🇸Spain mtoscano

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    @mkalkbrenner I tried to use a Search API field processor.

    But there was a problem, if the referenced entity is published or unpublished, then, until the entity is indexed again, the reference entity on the search index will be inconsistent, so maybe it can be done using a Search API processor, but altering the result after retrieving the items and then check the reference status, to avoid losing facet items.

    The solution here is not bad, either, but it should take into account the translations.

  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    I created a Search API processor that should do the job https://git.drupalcode.org/project/search_api/-/merge_requests/79/diffs

  • I agree with @mkalkberenner that the solution proposed in the linked issue is better.
    Is there any reason to keep this issue open, or could we fall back to the other one and close this?

  • Status changed to Closed: won't fix about 1 year ago
  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    This is not the way, the way is in search api

  • 🇮🇳India kalpanajaiswal

    We encountered a similar requirement, and the following solutions worked effectively in our case:

    Including unpublished nodes in the Search API index
    Solution: Disable the Entity status processor in the index configuration. This ensures that unpublished nodes are not excluded during indexing.

    Excluding unpublished taxonomy terms from facet filters
    Solution: Implement a custom Facets Processor plugin that filters out unpublished taxonomy terms during the facet build process. This allows only published terms to be displayed in the facet options, ensuring a clean and relevant user interface.

    <?php
    
    namespace Drupal\facets\Plugin\facets\processor;
    
    use Drupal\facets\FacetInterface;
    use Drupal\facets\Processor\ProcessorPluginBase;
    use Drupal\facets\Processor\BuildProcessorInterface;
    use Drupal\taxonomy\Entity\Term;
    
    /**
     * @FacetsProcessor(
     *   id = "exclude_unpublished_terms",
     *   label = @Translation("Exclude unpublished terms"),
     *   description = @Translation("Removes unpublished taxonomy terms from facet options."),
     *   stages = {
     *     "build" = 50
     *   }
     * )
     */
    class ExcludeUnpublishedTerms extends ProcessorPluginBase implements BuildProcessorInterface {
    
      /**
       * {@inheritdoc}
       */
      public function build(FacetInterface $facet, array $results): array {
        $filtered = [];
        foreach ($results as $result) {
          $tid = $result->getRawValue();
          $term = Term::load($tid);
          if ($term && $term->isPublished()) {
            $filtered[] = $result;
          }
        }
        return $filtered;
      }
    
    }
    
    ?>
Production build 0.71.5 2024