0

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:

  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.

3
  • Not sure what's wrong there, but did you try defining it as a configurable field?
    – Berdir
    Sep 2, 2016 at 16:12
  • I'm not sure what you mean by that, do you mean have I tried adding the field through config rather than through hook_entity_base_field_info? I did try that, but when doing that, the field wasn't appearing on the add poll form, and when I ran drush entity-updates I would get output saying that the field I had added needed to be uninstalled. Sep 2, 2016 at 18:09
  • Yes, fields currently don't show up. That's a known issue, check the issue queue. but if you have that, then you're already further than you are now. how did you add it? just do it in the UI, that shouldn't report as to be uninstalled.
    – Berdir
    Sep 2, 2016 at 21:27

1 Answer 1

0

This is the best solution I have been able to come up with so far. Answers are still welcome, because I still don't know:

  1. Why this is happening, is it a views bug?
  2. Could I have created the field in a way that using a hook to alter the query is not necessary?

What I ended up doing was using hook_views_pre_execute(Drupal\views\ViewExecutable $view) to get the query out of $view->build_info['query']->getTables() and then manually change the condition on my join table to use the correct column, which was not poll__cat.cat but rather poll__cat.cat_target_id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.