The auto complete widget for entityreference does not work with crm_core_contact entity type as it does not return the appropriate result when typing in the autocomplete textfield, this is due to the fact entityreference below:
crm_core_contact_ui_entityreference.inc
if (isset($match)) {
$entity_info = entity_get_info($this->field['settings']['target_type']);
if (isset($entity_info['entity keys']['label'])) {
$query->propertyCondition($entity_info['entity keys']['label'], $match, $match_operator);
}
}
The entity reference field tries to fetch the ID of the entity type (but we have no $entity_info['entity keys']['label'] defined.
The patch is attached as well as the new entityreference class i created to fix this problem
crm_core_contact_ui_entityreference.inc
if (!module_exists('entityreference')) {
exit();
}
/**
* Overwrite entity reference handler class so autocomplete widget works with crm_core_contact
*/
class EntityReference_SelectionHandler_Generic_crm_core_contact extends EntityReference_SelectionHandler_Generic {
/**
* Implements EntityReferenceHandler::getReferencableEntities().
*/
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$options = array();
$entity_type = $this->field['settings']['target_type'];
$query = $this->buildContactFieldQuery($match, $match_operator);
if ($limit > 0) {
$query->range(0, $limit);
}
$results = $query->execute();
if (!empty($results[$entity_type])) {
$entities = entity_load($entity_type, array_keys($results[$entity_type]));
foreach ($entities as $entity_id => $entity) {
$options[$entity_id] = $this->getLabel($entity);
}
}
return $options;
}
/**
* Build an EntityFieldQuery to get referencable entities.
*/
public function buildContactFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $this->field['settings']['target_type']);
if (!empty($this->field['settings']['handler_settings']['target_bundles'])) {
$query->entityCondition('bundle', $this->field['settings']['handler_settings']['target_bundles'], 'IN');
}
if (isset($match)) {
// @todo, we must choose from the field setting on what field to use from the contact entity
// we might also search by given name as well
$query->fieldCondition('contact_name', 'family', $match, 'CONTAINS');
}
// Add a generic entity access tag to the query.
$query->addTag($this->field['settings']['target_type'] . '_access');
$query->addTag('entityreference');
$query->addMetaData('field', $this->field);
// Add the sort option.
if (!empty($this->field['settings']['handler_settings']['sort'])) {
$sort_settings = $this->field['settings']['handler_settings']['sort'];
if ($sort_settings['type'] == 'property') {
$query->propertyOrderBy($sort_settings['property'], $sort_settings['direction']);
}
elseif ($sort_settings['type'] == 'field') {
list($field, $column) = explode(':', $sort_settings['field'], 2);
$query->fieldOrderBy($field, $column, $sort_settings['direction']);
}
}
return $query;
}
}
Comments
Comment #1
ygerasimov commentedI cannot reproduce the problem here. I have entityreference latest rc1 version. Created three contacts (different types) and entityreference field on the node. I can create references from the node to all contacts.
There is problem that contacts are not shown on the node view page. But that is different problem.
Patch is not well formed (it has only changes in .info file).
Please advise how to reproduce the problem?
Comment #2
dealancer commentedI have ported patch so it can work good with recent version of Entity Reference, also updated naming and added search by other name subfields (given, middle, etc), it was tricky, though.
Committed:
http://drupalcode.org/project/crm_core.git/commit/2e007fa
http://drupalcode.org/project/crm_core.git/commit/f5160cc
I used part of code from Name module, but it this code needs to be improved here and there, see http://drupal.org/node/1369618#comment-6278744. So I am not closing this issue.
Comment #3
dealancer commentedHere is a fix that allows to use own later hook when implementing contact search in entity reference, this could be
a part of solution for #1701926: Name field settings are generic to all contact types.
Comment #4
philipz commentedI don't know if this is right place to write this but the issue topic is perfect :)
I'm trying to add activity to the contact and I'm getting this ajax error:
StatusText: Service unavailable (with message)
ResponseText: PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'crm.field_data_contact_name' doesn't exist:
I have replaced the default core crm contacts with my own and I guess this is because I've got no such field anymore but what if I wanted to reference my contact by other fields or do I have to add this field for now to make it work? Is this Entity Reference or CRM Core problem?
Comment #5
dealancer commentedLooks like it is related to issue: http://drupal.org/node/1586366, for now just add new contact_name field.
Comment #6
dealancer commentedComment #7
pingers commentedperhaps a custom views field handler to display crm core contact label will work better in default views?
then we're not relying on name field specifically in views.
Comment #8
Anonymous (not verified) commentedI have been running into a lot of issues with how to present entity references.
What might make sense is a default callback for entity references that format the name of the contact and pass back the entity id. It could be something users could customize as they go along.
Comment #9
rosk0Not changing status, but this problem is currently solved.
Comment #10
luckyape commentedMy deployment running all the latest version produces the following query
The query is wrong in a couple of places - the first condition should not be present and the search string is not being passed into the query, see "LIKE '% %'"
This query will only return items with 'title' values within the name widget.
Comment #11
rosk0As i mentioned before, the problem is already fixed.
@luckyape: please ensure that you are using stable release of crm_core and other modules.
If you still experience problems feel free to post here, but with more info(use cases, module versions, etc.).
Comment #12
pingers commentedI.e. use the "development" branch.
Comment #13
pingers commentedThis was broken. See attached which finds the query base table alias automatically and join against the crm_core_contact table.
This seems to fix the problem when I add some contacts, add an entityreference field to a node using autocomplete and try to use it at node/add/{type}.
Comment #14
pingers commentedUgh, attach the patch.
Comment #15
tintisha_andy commentedI've applied pingers' patch on comment #14 and it has fixed the autocomplete widget for a contact entity reference. However it has broken the autocomplete widget for adding a relationship.
I've got a 'Members' contact type and an 'Organisations' contact type and created an 'Employee of' relationship type. Since applying the patch the add a relationship autocomplete widget throws a long javascript error.
Reversing the patch solves the error but then obviously the original issue is still present.
Comment #16
Anonymous (not verified) commentedCan someone explain the problem a little more clearly? I can't seem to reproduce it, and I don't want to leave this open without a resolution.
Testing on an activity type with an entity reference field, I was able to successfully add contacts, save the activity, and see the contacts on the activity page. Is this not the issue?
Comment #17
pingers commentedIt's about removing the assumption that autocomplete works by "name". This is true for an individual, but for an organisation, this is not the case.
Comment #18
Anonymous (not verified) commentedOh, what other field does an organization use?
My understanding was that we store organization name in the name field, in the last name part.
Comment #19
pingers commentedIt was from a long time ago... I remember a time when we used a text field for household and organisations. This is probably no longer valid, given I could successfully use the autocomplete for relations and entityreferences.
I did see that organisations and households require a first and last name. This is another issue though.
Comment #20
Anonymous (not verified) commentedLet me look at that again, but the last time I checked the name module could be set up with different required fields for each bundle in the entity.
Comment #23
jelle_sRerolled against 7.x-0.x
Comment #26
rosk0Was fixed by eeae84b6d074483c668236eb89b58b99ee6807b9.