Can we support other entities than nodes? I would like to show similar commerce products.

How difficult would it be to implement this?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

luksak’s picture

Could the maintainer please make a statement here?

quicksketch’s picture

I don't think it would be too difficult, but I don't plan on implementing it because it's unneeded for the situations in which we've used this module. I'd be happy to look at patches to add this functionality.

luksak’s picture

Assigned: Unassigned » luksak

Ok, I guess I will give it a shot in a few weeks.

luksak’s picture

Assigned: luksak » Unassigned

I was not able to try this. Maybe I will get a budget for this in the next few months...

sill’s picture

I'd love to see this feature implemented. For my situation it would be helpful in the case of tagging images.

playfulwolf’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Priority: Normal » Major
Issue summary: View changes
Related issues: +#2278057: Count total weights of similar terms and order by them

I am upping this to major, and changing the version.
Right now working on the "quick" views based solution for this.

luksak’s picture

Looking at this once again, I realized that the module uses the taxonomy_index table, which is an index of the references between nodes and taxonomy terms. Either we need to introduce a table that holds data for all entity types or we need to run much more complex queries...

Has anyone made progress on this?

luksak’s picture

I just realized that there is this module that provides an index for any entity type: https://www.drupal.org/project/taxonomy_entity_index

Here is the core issue regarding this: https://www.drupal.org/project/drupal/issues/1040786

luksak’s picture

Status: Active » Needs work
StatusFileSize
new5.52 KB

I worked on this for a bit. I managed to get the dynamic queries right, except for the one in SimilarTermsArgument.

Changing this

$this->query->addTable('taxonomy_index', NULL, NULL, 'similarterms_taxonomy_index');

to this

$this->query->addTable('taxonomy_entity_index', NULL, NULL, 'similarterms_taxonomy_index');

Causes the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'similarterms_taxonomy_index.tid' in 'where clause': SELECT "commerce_product_field_data"."langcode" AS "commerce_product_field_data_langcode", COUNT(commerce_product.product_id) AS "commerce_product_product_id", MIN(commerce_product_field_data.product_id) AS "product_id" FROM {commerce_product_field_data} "commerce_product_field_data" INNER JOIN {commerce_product} "commerce_product" ON commerce_product_field_data.product_id = commerce_product.product_id WHERE (("similarterms_taxonomy_index"."tid" IN (:db_condition_placeholder_0, :db_condition_placeholder_1, :db_condition_placeholder_2)) AND ("similarterms_taxonomy_index"."entity_type" IN (:db_condition_placeholder_3)) AND ("commerce_product"."product_id" NOT IN (:db_condition_placeholder_4))) AND ("commerce_product_field_data"."status" = :db_condition_placeholder_5) GROUP BY commerce_product_field_data.product_id, commerce_product_field_data_langcode ORDER BY "commerce_product_product_id" DESC LIMIT 11 OFFSET 0; Array ( [:db_condition_placeholder_0] => 22 [:db_condition_placeholder_1] => 28 [:db_condition_placeholder_2] => 29 [:db_condition_placeholder_3] => commerce_product [:db_condition_placeholder_4] => 2716 [:db_condition_placeholder_5] => 1 )

There seems t be a missing relationship between the sql tables and therefore a join is missing from the query.

I also tried to use a left join, but somehow without success:

$this->query->leftJoin('taxonomy_entity_index', 'ti', 'ti.entity_id = :entity_id AND ti.entity_type = :entity_type AND ti.tid = :tid', [
  ':entity_id' => $this->value,
  ':entity_type' => $entity_type,
  ':tid' => $this->tids,
]);

Does anyone know how to solve this?

luksak’s picture

Status: Needs work » Needs review

Ok, i got this working with my patch in #3222950: Provide views join on BASE_TABLE_field_data!

Cleaned up my patch a bit in the merge request.

calbasi’s picture

It seems your patch can NOT be applied on last module versions 1.8.0:

- Installing drupal/similarterms (1.8.0): Extracting archive
- Applying patches for drupal/similarterms
https://www.drupal.org/files/issues/2021-06-30/1762448-other-entity-type... (Support other entities than nodes)
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2021-06-30/1762448-other-entity-type...

In Patches.php line 326:

Cannot apply patch Support other entities than nodes (https://www.drupal.org/files/issues/2021-06-30/1762448-other-entity-type...)!

luksak’s picture

StatusFileSize
new5.54 KB

Ok, here is a re-roll.

loze’s picture

Status: Needs review » Needs work

Thanks, this got it working for me with commerce_product entities, and think this is on the right track.

This also needs to do the following.

1. Check if the taxonomy_entity_index module exists first and only perform these changes if it does.
2. Instead of just supporting commerce_product entities it should loop through all available content entities and provide the joins for any content entity.

I need this for my project so I will try to provide a patch.

loze’s picture

Priority: Major » Normal
Status: Needs work » Needs review
StatusFileSize
new14.11 KB

I've made some updates to the MR.

1. It checks if taxonomy_entity_index is enabled, and defaults back to using the taxonomy_index table if it is not.

2. the views fields/args now supports all content entities.

I have also updated the MR for taxonomy_entity_index on #3222950: Provide views join on BASE_TABLE_field_data which is required for this to work.

Everything appears to be working as expected on my local site and I can now use this module with products and other entities.

attached is a patch for composer

shelane’s picture

Status: Needs review » Needs work
loze’s picture

Status: Needs work » Needs review

Rebased onto the current 8.x-1.x. The module has changed a lot since this MR was written, so the changes were redone on top of the current code rather than merged.

The behavior is the same as before: with taxonomy_entity_index installed, the similarity argument, field, and sort work for all content entity types, not just nodes. Without taxonomy_entity_index installed, nothing changes.

One difference: the current code builds its own join to the index table, so this no longer depends on #3222950: Provide views join on BASE_TABLE_field_data.

Added tests for non-node entities. Pipeline is green.

  • shelane committed 4b238cf4 on 8.x-1.x authored by luksak
    feat: #1762448 Support other entities than nodes
    
shelane’s picture

Thank you for updating the patch. I merged it and then realized that there was nothing added to the README about the new functionality. Can you submit that information?

shelane’s picture

Status: Needs review » Needs work

loze’s picture

Status: Needs work » Needs review

Updated Readme in !14

  • shelane committed 5630b961 on 8.x-1.x authored by loze
    Updated README with new features from #1762448
    
shelane’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • shelane committed 5630b961 on 2.x authored by loze
    Updated README with new features from #1762448
    

Status: Fixed » Closed (fixed)

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