Problem/Motivation

If you add Product Type as a field on Search API Product Index, and then try to filter a Product Datasource view (catalog page) by Product Type, it doesn't use the search api views handler you would expect (options list). Instead, it is treated as an integer that can only be limited by value.

The same issue existed for node types, here is the issue: https://www.drupal.org/project/search_api/issues/2683259

Proposed resolution

As a workaround, we can implement hook_search_api_views_handler_mapping_alter to let Search API know what views handler to use. However, the best solution would be to solve this on the Search API side (see related issue).

CommentFileSizeAuthor
#4 3016759-4.patch1.54 KBericchew
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ericchew created an issue. See original summary.

ericchew’s picture

Issue summary: View changes
ericchew’s picture

If possible, the best course of action would be to fix this on the Search API side. In the meantime, the workaround is pretty simple. I will convert the workaround to a patch instead of pasted code on the issue when I get a chance.

Adding related issue from Search API.

ericchew’s picture

Issue summary: View changes
FileSize
1.54 KB
mglaman’s picture

Status: Active » Needs review
AndyD328’s picture

+1 from me, it's working perfectly. Thanks for the patch!

bojanz’s picture

Status: Needs review » Needs work

I hoped Search API would fix this quickly, cause it's super weird for them to not support bundle fields, but oh well.
Let's proceed with the patch.

+use Drupal\commerce\EntityHelper;
 use Drupal\entity\BundleFieldDefinition;
 use Drupal\commerce_product\Entity\ProductTypeInterface;

Use statements are no longer alphabetical.

+/**
+ * Returns a list of available product type names.
+ *
+ * @return string[]
+ *   An array of product type labels, keyed by the product type name.
+ */
+function commerce_product_type_get_names() {
+  $product_types = \Drupal::entityTypeManager()->getStorage('commerce_product_type')->loadMultiple();
+  return EntityHelper::extractLabels($product_types);
+}

Returns should be Gets.
There is no such thing as a product type name, so the function should be called commerce_product_type_labels() and document that it returns a list of labels keyed by ID.
We should get the storage on its own line, to shorten the mega-line.

  • bojanz committed a0a9215 on 8.x-2.x authored by ericchew
    Issue #3016759 by ericchew, bojanz: Search API Catalog Filter By Product...
bojanz’s picture

Status: Needs work » Fixed

Addressed #7 and committed. Thanks!

Status: Fixed » Closed (fixed)

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