function redhen_relation_get_available_entities in RedHen CRM 7
Return a list of entities the given entity can be associated with.
Parameters
object $entity: Entity.
string $entity_type_to_relate: Entity type.
string $relation_type: Relation type.
string $search: Search string.
int $limit: Limit.
Return value
array Available entities.
1 call to redhen_relation_get_available_entities()
- redhen_relation_autocomplete in modules/
redhen_relation/ redhen_relation.module - Autocomplete callback for listing available entities for a relation type.
File
- modules/
redhen_relation/ redhen_relation.module, line 606 - Redhen CRM Relation Module.
Code
function redhen_relation_get_available_entities($entity, $entity_type_to_relate, $relation_type, $search = '', $limit = 0) {
$entities = array();
if (!in_array($entity_type_to_relate, array(
'redhen_contact',
'redhen_org',
))) {
return $entities;
}
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $entity_type_to_relate)
->propertyCondition('redhen_state', REDHEN_STATE_ACTIVE);
// Exclude existing relationships.
$existing = redhen_relation_relations($entity, $relation_type);
if (!empty($existing)) {
$info = entity_get_info($entity_type_to_relate);
$id_key = $info['entity keys']['id'];
$to_exclude = array();
foreach ($existing as $relation_id => $related_entities) {
foreach ($related_entities as $related_entity) {
$to_exclude[] = entity_id($related_entity
->entityType(), $related_entity);
}
}
$query
->propertyCondition($id_key, $to_exclude, 'NOT IN');
}
// Keyword search.
if ($search != '') {
// If we're searching for redhen_contacts, set a query tag so we can alter
// the fields that are searched in redhen_contact.
if ($entity_type_to_relate == 'redhen_contact') {
$query
->addTag('redhen_contact_label');
}
$query
->propertyCondition('label', $search, 'CONTAINS');
}
if ($limit > 0) {
$query
->range(0, $limit);
}
if ($result = $query
->execute()) {
$entities = entity_load($entity_type_to_relate, array_keys($result[$entity_type_to_relate]));
}
return $entities;
}