When adding a facet based on an aggregated field the option "Transform entity id into label" doen't work properly.

The processor "Transform entity ID to label" (translate_entity) works as designed, to finish this issue we should:

  1. Add a note in the description of the processor ("Transform entity ID to label" (translate_entity)) that it doesn't support aggregated fields This was committed in #23.
  2. Add a new processor of transforming the entity ID into label in aggregated fields.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jeroen_005 created an issue. See original summary.

Jeroen_005’s picture

Added a possible approach in this patch file.

Jeroen_005’s picture

Status: Active » Needs review
Jeroen_005’s picture

Jeroen_005’s picture

Fixed some patch issues.

Jeroen_005’s picture

Added field state to facet_setting.

Status: Needs review » Needs work

The last submitted patch, 6: translate_entity_processor_field_aggregation-2844895-6.patch, failed testing.

borisson_’s picture

Issue tags: +Needs reroll

This patch no longer applies and needs a reroll

Jeroen_005’s picture

Rerolled patch.

Jeroen_005’s picture

Status: Needs work » Needs review

Rerolled patch.

Status: Needs review » Needs work

The last submitted patch, 9: translate_entity_processor_field_aggregation-2844895-9.patch, failed testing.

Jeroen_005’s picture

Looks like there is an dependency on issue #2777217.

Status: Needs review » Needs work
borisson_’s picture

Status: Needs work » Postponed

Ok, let's postpone this one.

borisson_’s picture

Status: Postponed » Needs review

The issue this depended on is committed.

Status: Needs review » Needs work
borisson_’s picture

Issue tags: +Needs reroll
gaurav.kapoor’s picture

Status: Needs work » Needs review
FileSize
4.78 KB

Status: Needs review » Needs work

The last submitted patch, 18: facets-2844895-18.patch, failed testing.

borisson_’s picture

Issue summary: View changes
Issue tags: -Needs reroll

We discussed this in today's hangout and decided that the current approach makes this way too complicated.

Instead, we'd like to keep this processor as-is and add documentation in the UI that a new custom processor should be created to do this.

borisson_’s picture

Attached is what #20 suggested.

borisson_’s picture

Status: Needs work » Needs review

NR for #21

  • borisson_ committed 99d3f0f on 8.x-1.x
    Issue #2844895 by Jeroen_005, borisson_, gaurav.kapoor: Transform entity...
borisson_’s picture

Status: Needs review » Fixed
joachim’s picture

> We discussed this in today's hangout and decided that the current approach makes this way too complicated.
> Instead, we'd like to keep this processor as-is and add documentation in the UI that a new custom processor should be created to do this.

Are you really sure this is the way to go?

Doing this means duplicating code in a new processor that handles entity labels on aggregated fields.

It also means the the complication that is being saved in code is moved onto to the site builder, because there will be two 'Transform entity ID to label' processors, and the site builder will need to choose one and know what sort of Search API search index they are using.

Furthermore, I am guessing that the 'List item label' processor will be affected in the same way -- that too might need special treatment to work with aggregated fields.

joachim’s picture

Status: Fixed » Active

Also, reopening because if this *is* the right way to go, there needs to be form validation to prevent this being added to aggregated fields -- views with this processor on an aggregated field crash with:

> Call to undefined method Drupal\search_api\Plugin\search_api\processor\Property\AggregatedFieldProperty::getPropertyDefinition

joachim’s picture

Also, I think patch #18 can be improved, with something like this

  // Support multiple entity types when using Search API.
  if ($source instanceof \Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay) {
    $field = $source->getIndex()
      ->getField($facet->getFieldIdentifier());
    
    $configuration = $field->getConfiguration();
    
    foreach ($configuration['fields'] as $field) {
      list($entity_type, $property_name) = \Drupal\search_api\Utility\Utility::splitCombinedId($field);
      
      // TODO:
      // 1. Split $entity_type from the form 'entity:TYPE'
      // 2. see if there is a field storage $property_name on $entity_type
      // 3. if it's an entity ref field, get its target
      // 4. check that all the aggregated fields have the same target.
    }
    

That's not far off from being able to figure out the target entity type of an aggregated field.

Rather than load fields on every page view, this could be figured out in the facet form, and saved into the processor configuration?

borisson_’s picture

Doing this will introduce a big difference in the processor between core search and search api. Aggregated fields are only working for search api so that is somewhat expected. However this might make things very complex.

However, I think that implementing #2851851: Facet source plugins should provide metadata for facet fields would be a good start to simplify the code to decide what field it is, see also the code already added to TranslateEntityProcessor in that patch.

joachim’s picture

(BTW, for people who are finding this breaks their facets from older versions, a quick and dirty workaround is to copy the taxonomy term label processor plugin from an old alpha (3 in my case) into a custom module.)

mkalkbrenner’s picture

I ran into this issue today. It leads to a fatal error:
The aggregated_field_type field has no datasource, there is no valid use for the list_item processor with this facet in ...

The aggregated field contains the types (bundles) of two different content entities. If I remove the "List Item label" option, it works.

mpp’s picture

I ran into this issue today. Not on an aggregated field but on a custom field I created that contains a bundle value.

ListItemProcessor contains fallback code to handle the bundle label but it doesn't reach that part of the code since my custom field doesn't have a datasource.

tamarpe’s picture

I ran it today as well with aggregated fields that contain a bundle value.
Would be a nice feature to add- a processor of transforming aggregated field entity IDs to labels

tamarpe’s picture

Ended up with a new processor to aggregated fields

tamarpe’s picture

Status: Active » Needs review
Aron Novak’s picture

Status: Needs review » Needs work

Just tested the patch in #33, works well not only for taxonomy terms, but for custom entities too.

Two minor code review comments:

1.
"An array of values that contain possible replacements for the orignal"

Should be:
"An array of values that contain possible replacements for the original"

2.
The processor should hide itself from the list already if not an aggregated field, so

+      if ($field->getPropertyPath() !== 'aggregated_field') {
+        throw new InvalidProcessorException("This field is not aggregated field.");
+      }

Could be removed and added as a pre-build check, not?

tamarpe’s picture

Good point, I fixed them both

tamarpe’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 36: transform_entity_id-2844895-36.patch, failed testing. View results

tamarpe’s picture

Status: Needs work » Needs review
FileSize
9.31 KB

Fix tests

tamarpe’s picture

Issue summary: View changes
Jax’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Ok, works.

Jax’s picture

Status: Reviewed & tested by the community » Needs work

I rescind the RTBC. Will fix a couple of small things:

  1. InvalidArgumentException description is incorrect.
  2. It gives issues with computed fields
Jax’s picture

Status: Needs work » Needs review
FileSize
9.33 KB
1.49 KB

Patch with very small changes.

         $field_storage = $definition_update_manager->getFieldStorageDefinition($field, $entity_type_id);
-        if ($field_storage->getType() === 'entity_reference') {
+        if ($field_storage && $field_storage->getType() === 'entity_reference') {

This was changed because computed fields don't have storage.

Jax’s picture

I actually used the wrong previous patch. I used the one from #36 instead of #38 so my patch is worthless...

Jax’s picture

Correct versions of the files.

niles38’s picture

Hey @Jax. Your patch in #45 is good! Just tested it.

Thanks so much! You saved me a lot of time :)

lamp5’s picture

Works well also with entity type bundles!

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, we don't have specific coverage for this, but that is going to be a lot of specific setup. I like that the supportsFacet is very specific here. +1

Jax’s picture

+    if ($field->getPropertyPath() === 'aggregated_field') {
+      return TRUE;
+    }
+
+    return FALSE;

should be converted to

return $field->getPropertyPath() === 'aggregated_field';

  • Nick_vh committed 4ca0a83 on 8.x-1.x authored by Jax
    Issue #2844895 by Jeroen_005, tamarpe, Jax, borisson_, gaurav.kapoor:...
Nick_vh’s picture

Status: Reviewed & tested by the community » Fixed

Committed and fixed

phenaproxima’s picture

Status: Fixed » Needs work
    /** @var \Drupal\search_api\Entity\Index $index */
    $index = $facet->getFacetSource()->getIndex();

I think this line is a problem. It assumes that the facet source is a Search API source, which is not always true (see the core_views_facets module, for example). We should probably return early if the facet source is not Search API. Patch incoming.

phenaproxima’s picture

Status: Needs work » Needs review
FileSize
1.72 KB

Here's a patch for this.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Looks like a simple change, LGTM.

borisson_’s picture

Status: Reviewed & tested by the community » Fixed

Pushed the followup.

Status: Fixed » Closed (fixed)

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