diff --git a/entityreference.module b/entityreference.module
index d49ba7f..3fd0e45 100644
--- a/entityreference.module
+++ b/entityreference.module
@@ -762,7 +762,12 @@ function entityreference_field_widget_settings_form($field, $instance) {
* Implements hook_options_list().
*/
function entityreference_options_list($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
- return entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities();
+ $options = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities();
+ if (count($options) == 1) {
+ $key = key($options);
+ $options = $options[$key];
+ }
+ return $options;
}
/**
@@ -993,15 +998,17 @@ function entityreference_autocomplete_callback($type, $field_name, $entity_type,
$entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10);
// Loop through the products and convert them into autocomplete output.
- foreach ($entity_labels as $entity_id => $label) {
- $key = "$label ($entity_id)";
- // Strip things like starting/trailing white spaces, line breaks and tags.
- $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
- // Names containing commas or quotes must be wrapped in quotes.
- if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
- $key = '"' . str_replace('"', '""', $key) . '"';
+ foreach ($entity_labels as $values) {
+ foreach ($values as $entity_id => $label) {
+ $key = "$label ($entity_id)";
+ // Strip things like starting/trailing white spaces, line breaks and tags.
+ $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
+ // Names containing commas or quotes must be wrapped in quotes.
+ if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
+ $key = '"' . str_replace('"', '""', $key) . '"';
+ }
+ $matches[$prefix . $key] = '
' . $label . '
';
}
- $matches[$prefix . $key] = '' . $label . '
';
}
}
diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
index 9c34258..217853f 100644
--- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
+++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php
@@ -169,7 +169,8 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
if (!empty($results[$entity_type])) {
$entities = entity_load($entity_type, array_keys($results[$entity_type]));
foreach ($entities as $entity_id => $entity) {
- $options[$entity_id] = check_plain($this->getLabel($entity));
+ list(,, $bundle) = entity_extract_ids($entity_type, $entity);
+ $options[$bundle][$entity_id] = check_plain($this->getLabel($entity));
}
}
@@ -486,4 +487,32 @@ class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityRefer
}
}
}
+
+ /**
+ * Implements EntityReferenceHandler::getReferencableEntities().
+ */
+ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
+ if ($match || $limit) {
+ return parent::getReferencableEntities($match , $match_operator, $limit);
+ }
+
+ $options = array();
+ $entity_type = $this->field['settings']['target_type'];
+
+ // We imitate core by calling taxonomy_get_tree().
+ $entity_info = entity_get_info('taxonomy_term');
+ $bundles = !empty($this->field['settings']['handler_settings']['target_bundles']) ? $this->field['settings']['handler_settings']['target_bundles'] : array_keys($entity_info['bundles']);
+
+ foreach ($bundles as $bundle) {
+ if ($vocabulary = taxonomy_vocabulary_machine_name_load($bundle)) {
+ if ($terms = taxonomy_get_tree($vocabulary->vid, 0)) {
+ foreach ($terms as $term) {
+ $options[$vocabulary->name][$term->tid] = str_repeat('-', $term->depth) . check_plain($term->name);
+ }
+ }
+ }
+ }
+
+ return $options;
+ }
}
diff --git a/tests/entityreference.handlers.test b/tests/entityreference.handlers.test
index 6c2ceca..b0b904e 100644
--- a/tests/entityreference.handlers.test
+++ b/tests/entityreference.handlers.test
@@ -27,10 +27,18 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
foreach ($tests as $test) {
foreach ($test['arguments'] as $arguments) {
$result = call_user_func_array(array($handler, 'getReferencableEntities'), $arguments);
- $this->assertEqual($result, $test['result'], t('Valid result set returned by @handler.', array('@handler' => $handler_name)));
+ $this->assertEqual($result, $test['result'], format_string('Valid result set returned by @handler.', array('@handler' => $handler_name)));
$result = call_user_func_array(array($handler, 'countReferencableEntities'), $arguments);
- $this->assertEqual($result, count($test['result']), t('Valid count returned by @handler.', array('@handler' => $handler_name)));
+ if (!empty($test['result'])) {
+ $bundle = key($test['result']);
+ $count = count($test['result'][$bundle]);
+ }
+ else {
+ $count = 0;
+ }
+
+ $this->assertEqual($result, $count, format_string('Valid count returned by @handler.', array('@handler' => $handler_name)));
}
}
}
@@ -93,8 +101,10 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array(NULL, 'CONTAINS'),
),
'result' => array(
- $nodes['published1']->nid => $node_labels['published1'],
- $nodes['published2']->nid => $node_labels['published2'],
+ 'article' => array(
+ $nodes['published1']->nid => $node_labels['published1'],
+ $nodes['published2']->nid => $node_labels['published2'],
+ ),
),
),
array(
@@ -103,7 +113,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array('Published1', 'CONTAINS'),
),
'result' => array(
- $nodes['published1']->nid => $node_labels['published1'],
+ 'article' => array(
+ $nodes['published1']->nid => $node_labels['published1'],
+ ),
),
),
array(
@@ -112,7 +124,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array('Published2', 'CONTAINS'),
),
'result' => array(
- $nodes['published2']->nid => $node_labels['published2'],
+ 'article' => array(
+ $nodes['published2']->nid => $node_labels['published2'],
+ ),
),
),
array(
@@ -139,9 +153,11 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array(NULL, 'CONTAINS'),
),
'result' => array(
- $nodes['published1']->nid => $node_labels['published1'],
- $nodes['published2']->nid => $node_labels['published2'],
- $nodes['unpublished']->nid => $node_labels['unpublished'],
+ 'article' => array(
+ $nodes['published1']->nid => $node_labels['published1'],
+ $nodes['published2']->nid => $node_labels['published2'],
+ $nodes['unpublished']->nid => $node_labels['unpublished'],
+ ),
),
),
array(
@@ -149,7 +165,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array('Node unpublished', 'CONTAINS'),
),
'result' => array(
- $nodes['unpublished']->nid => $node_labels['unpublished'],
+ 'article' => array(
+ $nodes['unpublished']->nid => $node_labels['unpublished'],
+ ),
),
),
);
@@ -215,8 +233,10 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array(NULL, 'CONTAINS'),
),
'result' => array(
- $users['admin']->uid => $user_labels['admin'],
- $users['non_admin']->uid => $user_labels['non_admin'],
+ 'user' => array(
+ $users['admin']->uid => $user_labels['admin'],
+ $users['non_admin']->uid => $user_labels['non_admin'],
+ ),
),
),
array(
@@ -225,7 +245,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array('NON_ADMIN', 'CONTAINS'),
),
'result' => array(
- $users['non_admin']->uid => $user_labels['non_admin'],
+ 'user' => array(
+ $users['non_admin']->uid => $user_labels['non_admin'],
+ ),
),
),
array(
@@ -250,10 +272,12 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array(NULL, 'CONTAINS'),
),
'result' => array(
- $users['anonymous']->uid => $user_labels['anonymous'],
- $users['admin']->uid => $user_labels['admin'],
- $users['non_admin']->uid => $user_labels['non_admin'],
- $users['blocked']->uid => $user_labels['blocked'],
+ 'user' => array(
+ $users['anonymous']->uid => $user_labels['anonymous'],
+ $users['admin']->uid => $user_labels['admin'],
+ $users['non_admin']->uid => $user_labels['non_admin'],
+ $users['blocked']->uid => $user_labels['blocked'],
+ ),
),
),
array(
@@ -261,7 +285,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array('blocked', 'CONTAINS'),
),
'result' => array(
- $users['blocked']->uid => $user_labels['blocked'],
+ 'user' => array(
+ $users['blocked']->uid => $user_labels['blocked'],
+ ),
),
),
array(
@@ -270,7 +296,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array('anonymous', 'CONTAINS'),
),
'result' => array(
- $users['anonymous']->uid => $user_labels['anonymous'],
+ 'user' => array(
+ $users['anonymous']->uid => $user_labels['anonymous'],
+ ),
),
),
);
@@ -364,7 +392,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array(NULL, 'CONTAINS'),
),
'result' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
+ 'comment_node_article' => array(
+ $comments['published_published']->cid => $comment_labels['published_published'],
+ ),
),
),
array(
@@ -372,7 +402,9 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array('Published', 'CONTAINS'),
),
'result' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
+ 'comment_node_article' => array(
+ $comments['published_published']->cid => $comment_labels['published_published'],
+ ),
),
),
array(
@@ -399,8 +431,10 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array(NULL, 'CONTAINS'),
),
'result' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
- $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
+ 'comment_node_article' => array(
+ $comments['published_published']->cid => $comment_labels['published_published'],
+ $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
+ ),
),
),
);
@@ -415,9 +449,11 @@ class EntityReferenceHandlersTestCase extends DrupalWebTestCase {
array(NULL, 'CONTAINS'),
),
'result' => array(
- $comments['published_published']->cid => $comment_labels['published_published'],
- $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
- $comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
+ 'comment_node_article' => array(
+ $comments['published_published']->cid => $comment_labels['published_published'],
+ $comments['published_unpublished']->cid => $comment_labels['published_unpublished'],
+ $comments['unpublished_published']->cid => $comment_labels['unpublished_published'],
+ ),
),
),
);