View source
<?php
class RedhenGroupSelectionHandler implements EntityReference_SelectionHandler {
protected function __construct($field, $instance) {
$this->field = $field;
$this->instance = $instance;
}
public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
return new RedhenGroupSelectionHandler($field, $instance, $entity_type, $entity);
}
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$entities = array();
foreach ($this
->getContactGroups() as $org_id => $org) {
$entities[$org->type][$org_id] = $this
->getLabel($org);
}
return $entities;
}
protected function getContactGroups() {
global $user;
return redhen_org_group_contact_groups($user, FALSE, $this->instance['bundle']);
}
public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
return count($this
->getContactGroups());
}
public function validateReferencableEntities(array $ids) {
$ids = array();
foreach ($this
->getContactGroups() as $org_id => $org) {
$ids[] = $org_id;
}
return $ids;
}
public function entityFieldQueryAlter(SelectQueryInterface $query) {
}
public function getLabel($entity) {
return entity_label($this->field['settings']['target_type'], $entity);
}
public static function settingsForm($field, $instance) {
return array();
}
public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
$entities = $this
->getReferencableEntities($input, '=', 6);
if (empty($entities)) {
form_error($element, t('There are no entities matching "%value"', array(
'%value' => $input,
)));
}
elseif (count($entities) > 5) {
form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array(
'%value' => $input,
'@value' => $input,
'@id' => key($entities),
)));
}
elseif (count($entities) > 1) {
$multiples = array();
foreach ($entities as $id => $name) {
$multiples[] = $name . ' (' . $id . ')';
}
form_error($element, t('Multiple entities match this reference; "%multiple"', array(
'%multiple' => implode('", "', $multiples),
)));
}
else {
return key($entities);
}
}
}