backdrop-contrib / reference

Defines a field to allow Backdrop entities to reference each other.
GNU General Public License v2.0
4 stars 6 forks source link

AJAX error using autocomplete for entities when bundles selected #2

Closed docwilmot closed 7 years ago

docwilmot commented 8 years ago

See image; happens as soon as you start typing in the autocomplete.

capture

mikemccaffrey commented 8 years ago

Weird, I wonder why it is not using clean urls when making the ajax request.

mikemccaffrey commented 8 years ago

Hey @docwilmot, I am unable to reproduce your error. I'm able to use the autocomplete on all entity types. Can you please test it out again and see if you are still having the issue, and if so, actually use the devel tools in your browser to get the http response to the request.

docwilmot commented 8 years ago

@mikemccaffrey when creating a new field (adding to a content type), check the "Allowed bundles" on the final step: allowed bundles

That seems to cause the error to show. Seems because in reference.module line 303:

  foreach ($field_instance_info['settings']['bundles'] as $bundle => $active) {
    if ($active) {
      $target_entity_bundles[] = $bundle;
    }
  }

Once $target_entity_bundles is not empty then line 322:

  if ($target_entity_bundles) {
    $query->condition($keys['bundle'], $target_entity_bundles,'IN');
  }

But $keys['bundle'] is node_type which doesnt exist in the comment table. Thus error.

The full error in the error log is:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node_type' in 'where clause': SELECT bt.cid AS cid, bt.subject AS subject FROM {comment} bt WHERE (subject LIKE :db_condition_placeholder_0 ESCAPE '\\') AND (node_type IN (:db_condition_placeholder_1, :db_condition_placeholder_2)) ORDER BY subject ASC LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] => %b% [:db_condition_placeholder_1] => comment_node_page [:db_condition_placeholder_2] => comment_node_post ) in reference_autocomplete_results() (line 333 of C:\wamp64\www\backdrop\modules\reference\reference.module).
olafgrabienski commented 7 years ago

I can reproduce the AJAX error for comments. As @docwilmot mentioned, the error appears only, if you check a content type under "Allowed bundles".

My environment:

PS: Maybe the issue title should be updated to reflect that the error happens also with comments.

mikemccaffrey commented 7 years ago

Alright, I've done a bunch of refactoring of the reference_field_instance_settings_form to account for entity types that are not properly defining the key for their bundles or if the key doesn't match a column in its base table, and disabling the bundle selection accordingly.

  // Allow the reference options to be filtered by entity bundle.
  $bundles_options = array();
  foreach ($entity_type_info['bundles'] as $bundle => $bundle_info) {
    $bundles_options[$bundle] = $bundle_info['label'];
  }
  $form['bundles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Allowed bundles'),
    '#options' => $bundles_options,
    '#multiple' => TRUE,
    '#default_value' =>  $instance['settings']['bundles'] ? $instance['settings']['bundles'] : array(),
    '#description' => t('Leave unchecked to allow all bundles to be referenced.'),
  );
  // Ensure that the entity type has registered its bundle key in entity info.
  if(!isset($entity_type_info['entity keys']['bundle']) || empty($entity_type_info['entity keys']['bundle'])) {
    $form['bundles']['#options'] = array();
    $form['bundles']['#description'] = t('Sorry, this entity type does not support limiting reference selection by bundle. <em>(no key for bundle found in entity info)</em>');
  }
  // Ensure that the entity type's bundle key matches a column in its base table.
  if(!db_field_exists($entity_type_info['base table'], $entity_type_info['entity keys']['bundle'])) {
    $form['bundles']['#options'] = array();
    $form['bundles']['#description'] = t('Sorry, this entity type does not support limiting reference selection by bundle. <em>(bundle key in entity info not found in database table)</em>');
  }

While, disabling the bundle selection solves the problems of the errors, it does mean that the feature is not available for entities like comments which have a mismatch between their bundle key and the bundle database columns. The questions is whether this is really needed by many people. Are people likely going to want to limit comment references based on the type of node they are on? Are there many other contrib entities that defining bundles weirdly or have a mismatch between their bundle array key and column name?

In any case, I think that this issue can be closed.