Add cardinality_unlimited taxonomy reference to custom entity

Okay, so I’m getting an error that I think may be a Views bug, but I don’t have enough knowledge about Views & SQL right now to be sure about that.

Here’s the hook I’m using to install the new field into the poll entity, using drush entity-updates:

/**  * Implements hook_entity_base_field_info().  */ function mymodule_entity_base_field_info(EntityTypeInterface $entity_type) {   if ($entity_type->id() === 'poll') {     $fields['cat'] = BaseFieldDefinition::create('entity_reference')       ->setLabel(t('Category'))       ->setDescription(t('Category of poll'))       ->setSetting('target_type', 'taxonomy_term')       ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)       ->setDisplayOptions('form', array(         'type' => 'entity_reference_autocomplete',         'weight' => -10,         'settings' => array(           'match_operator' => 'CONTAINS',           'size' => '60',           'autocomplete_type' => 'tags',           'placeholder' => '',         ),       ))       ->setTranslatable(TRUE);     return $fields;   } } 

Then, I make a view that would like to use this taxonomy term as a Contextual Filter. I want to use the taxonomy term name rather than the ID, so I need to add a relationship. I add relationship in the view and look for the Taxonomy Term relationship that has my description, “Category of poll” and select it. The view complains about a SQL error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'on clause': SELECT AS id, taxonomy_term_field_data_poll__cat.tid AS taxonomy_term_field_data_poll__cat_tid FROM {poll_field_data} poll_field_data LEFT JOIN {poll__cat} poll__cat ON = poll__cat.entity_id AND poll__cat.deleted = :views_join_condition_0 LEFT JOIN {taxonomy_term_field_data} taxonomy_term_field_data_poll__cat ON = taxonomy_term_field_data_poll__cat.tid LIMIT 5 OFFSET 0; Array ( [:views_join_condition_0] => 0 )

The reason for the error in the above query is that the column does not exist, but it rather SHOULD be poll__cat.cat_target_id.

My question is: is this a views bug or a bug with the way I’m adding the field to the poll entity, and if it is a views bug:

  1. How do I debug it? Where does views set the column name it’s looking for?

  2. Is there a hook I can use to change it, and/or should I submit this odd case to the views project?

EDIT: I’m trying to use hook_views_query_alter and hook_views_pre_execute to try and modify the broken query to do what I want it to do, but it is incredibly frustrating, because all of the things are protected, and it is very inconvenient to access anything, considering the purpose of these hooks is to modify data. I’m trying to look through the functions for ViewExecutable and the views Sql class, but I don’t see anything that will help me change the column to what it should be! Anyone who can help in any way, I will be so grateful.

Edit 2: It seems like this guy: Views doesn't like multiple entity references in my entity has had a similar problem. I feel like this is a bug somewhere, or at the very least an issue with CARDINALITY_UNLIMITED being confusing to implement with entity references.

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Add cardinality_unlimited taxonomy reference to custom entity

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.