diff --git a/entityreference.module b/entityreference.module index 5f03c5b..8670615 100644 --- a/entityreference.module +++ b/entityreference.module @@ -820,7 +820,7 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan $entities = entity_load($field['settings']['target_type'], $entity_ids); foreach ($entities as $entity_id => $entity_item) { - $label = $handler->getLabel($entity_item); + $label = $handler->getLabel($entity_item, $entity_id); $key = "$label ($entity_id)"; // Labels containing commas or quotes must be wrapped in quotes. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { @@ -1212,7 +1212,7 @@ function entityreference_field_formatter_view($entity_type, $entity, $field, $in $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity); foreach ($items as $delta => $item) { - $label = $handler->getLabel($item['entity']); + $label = $handler->getLabel($item['entity'], $item['target_id']); // If the link is to be displayed and the entity has a uri, display a link. // Note the assignment ($url = ) here is intended to be an assignment. if ($display['settings']['link'] && ($uri = entity_uri($field['settings']['target_type'], $item['entity']))) { diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php index 2d1c5d7..7efba71 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php @@ -303,7 +303,7 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select /** * Implements EntityReferenceHandler::getLabel(). */ - public function getLabel($entity) { + public function getLabel($entity, $entity_id = NULL) { return entity_label($this->field['settings']['target_type'], $entity); } @@ -488,7 +488,7 @@ class EntityReference_SelectionHandler_Generic_file extends EntityReference_Sele return $query; } - public function getLabel($entity) { + public function getLabel($entity, $entity_id = NULL) { // The file entity doesn't have a label. More over, the filename is // sometimes empty, so use the basename in that case. return $entity->filename !== '' ? $entity->filename : basename($entity->uri); diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php index 1b036a7..09e2039 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Views.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php @@ -153,7 +153,22 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio /** * Implements EntityReferenceHandler::getLabel(). */ - public function getLabel($entity) { + public function getLabel($entity, $entity_id = NULL) { + $display_name = $this->field['settings']['handler_settings']['view']['display_name']; + $args = $this->field['settings']['handler_settings']['view']['args']; + $result = array(); + if ($this->initializeView(NULL, 'CONTAINS', 0, array($entity_id))) { + // Get the results. + $entities = $this->view->execute_display($display_name, $args); + $key = array_pop($entities); + $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) . '"'; + } + return $key; + } + return entity_label($this->field['settings']['target_type'], $entity); }