diff --git a/entityreference.module b/entityreference.module index 9c78928..ac0502d 100644 --- a/entityreference.module +++ b/entityreference.module @@ -74,8 +74,13 @@ function entityreference_field_settings_form($field, $instance, $has_data) { foreach (entity_get_info() as $entity_type => $entity_info) { $entity_type_options[$entity_info['label']] = array(); $entity_type_options[$entity_info['label']][$entity_type . ':'] = t('@entity_type: all bundles', array('@entity_type' => $entity_info['label'])); - foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { - $entity_type_options[$entity_info['label']][$entity_type . ':' . $bundle_name] = t('@entity_type: @bundle_name', array('@entity_type' => $entity_info['label'], '@bundle_name' => $bundle_info['label'])); + // The comment entity type doesn't support EntityFieldQuery conditions + // on bundles, so there's no way to get only comments of a specific bundle + // without loading all of them. + if ($entity_type != 'comment') { + foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { + $entity_type_options[$entity_info['label']][$entity_type . ':' . $bundle_name] = t('@entity_type: @bundle_name', array('@entity_type' => $entity_info['label'], '@bundle_name' => $bundle_info['label'])); + } } } @@ -558,3 +563,35 @@ function entityreference_field_views_data($field) { return $data; } + + /** + * Implements hook_entity_query_alter(). + * + * Convert EntityFieldQuerys on taxonomy terms that have an entity condition + * on term bundles (vocabulary machine names). Since the vocabulary machine + * name is not present in the {taxonomy_term_data} table itself, we have to + * convert the bundle condition into a property condition of vocabulary IDs to + * match against {taxonomy_term_data}.vid. + */ +function entityreference_entity_query_alter($query) { + $conditions = &$query->entityConditions; + + // Alter only taxonomy term queries with bundle conditions. + if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'taxonomy_term' && isset($conditions['bundle'])) { + // Convert vocabulary machine names to vocabulary IDs. + $vids = array(); + if (is_array($conditions['bundle']['value'])) { + foreach ($conditions['bundle']['value'] as $vocabulary_machine_name) { + $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); + $vids[] = $vocabulary->vid; + } + } + else { + $vocabulary = taxonomy_vocabulary_machine_name_load($conditions['bundle']['value']); + $vids = $vocabulary->vid; + } + + $query->propertyCondition('vid', $vids, $conditions['bundle']['operator']); + unset($conditions['bundle']); + } +}