TypeError with "show count" facet summary

Created on 10 October 2024, about 2 months ago

Problem/Motivation

Updating to facets 2.0.8 broke our facet summary, which uses the processor "Show a summary of how many results were found".

Whenever a user visits our search view without providing a search query, the view shows a maintenance page with this in the logs:

TypeError: Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay::getCount(): Return value must be of type string, none returned in Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay->getCount() (line 460 of /var/www/web/modules/contrib/facets/src/Plugin/facets/facet_source/SearchApiDisplay.php).

Proposed resolution

The problem was introduced in πŸ“Œ Automated Drupal 11 compatibility fixes for facets Fixed , specifically by this change from 74103165:

diff --git a/src/Plugin/facets/facet_source/SearchApiDisplay.php b/src/Plugin/facets/facet_source/SearchApiDisplay.php
index d5b7042..7cb65eb 100644
--- a/src/Plugin/facets/facet_source/SearchApiDisplay.php
+++ b/src/Plugin/facets/facet_source/SearchApiDisplay.php
@@ -449,7 +449,7 @@ class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSo
   /**
    * {@inheritdoc}
    */
-  public function getCount() {
+  public function getCount(): string {
     $search_id = $this->getDisplay()->getPluginId();
     if ($search_id && !empty($search_id)) {
       if ($this->searchApiQueryHelper->getResults($search_id) !== NULL) {

ShowCountProcessor expects that getCount() may return NULL:

// modules/facets_summary/src/Plugin/facets_summary/processor/ShowCountProcessor.php
...
class ShowCountProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
  ...
  public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
    ...
    $count = $facets_summary->getFacetSource()->getCount();
    $build_count = [
      '#theme' => 'facets_summary_count',
      '#count' => $count === NULL ? 0 : $count,
    ];
    ...
  }
}

And as a result fails with a TypeError when that is the case.

Reverting the change on src/Plugin/facets/facet_source/SearchApiDisplay.php above fixes the issue, but my understanding is that providing a type declaration for the function's return value is required for Drupal 11.

Attached is a suggested patch that turns the return type from string to ?string and also fixes the issue.

πŸ› Bug report
Status

Active

Version

2.0

Component

Facet summary

Created by

πŸ‡¨πŸ‡¦Canada fengtan Montreal, Canada

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

Comments & Activities

Production build 0.71.5 2024