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 'poll__cat.cat' in 'on clause': SELECT poll_field_data.id 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_field_data.id = 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 poll__cat.cat = 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 poll__cat.cat
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:
How do I debug it? Where does views set the column name it's looking for?
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.