class EntityReferenceSelectionHandlerGeneric implements EntityReferenceSelectionHandlerInterface {
protected $label_key;
public $field;
public $instance;
public $entity_type;
public $entity;
public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
$target_entity_type = $field['settings']['target_type'];
$entity_info = entity_get_info($target_entity_type);
if (empty($entity_info['base table'])) {
include_once (dirname(__FILE__) . '/EntityReferenceSelectionHandler.inc');
return EntityReferenceSelectionHandlerBroken::getInstance($field, $instance);
}
if (class_exists($class_name = 'EntityReferenceSelectionHandlerGeneric_' . $target_entity_type)) {
return new $class_name($field, $instance, $entity_type, $entity);
}
else {
return new EntityReferenceSelectionHandlerGeneric($field, $instance, $entity_type, $entity);
}
}
protected function __construct($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
$this->field = $field;
$this->instance = $instance;
$this->entity_type = $entity_type;
$this->entity = $entity;
}
public static function settingsForm($field, $instance) {
$entity_info = entity_get_info($field['settings']['target_type']);
$field['settings']['handler_settings'] += array(
'target_bundles' => array(),
'sort' => array(
'type' => 'none',
)
);
if (!empty($entity_info['entity keys']['bundle'])) {
$bundles = array();
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$bundles[$bundle_name] = $bundle_info['label'];
}
$form['target_bundles'] = array(
'#type' => 'checkboxes',
'#title' => t('Target bundles'),
'#options' => $bundles,
'#default_value' => $field['settings']['handler_settings']['target_bundles'],
'#size' => 6,
'#multiple' => TRUE,
'#description' => t('The bundles of the entity type that can be referenced. Optional, leave empty for all bundles.'),
'#element_validate' => array('_entityreference_element_validate_filter'),
);
}
else {
$form['target_bundles'] = array(
'#type' => 'value',
'#value' => array(),
);
}
$form['sort']['type'] = array(
'#type' => 'select',
'#title' => t('Sort by'),
'#options' => array(
'none' => t("Don't sort"),
'property' => t('A property of the base table of the entity'),
'field' => t('A field attached to this entity'),
),
'#ajax' => TRUE,
'#limit_validation_errors' => array(),
'#default_value' => $field['settings']['handler_settings']['sort']['type'],
);
$form['sort']['settings'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('entityreference-settings')),
'#process' => array('_entityreference_form_process_merge_parent'),
);
if ($field['settings']['handler_settings']['sort']['type'] == 'property') {
$field['settings']['handler_settings']['sort'] += array(
'property' => NULL,
);
$form['sort']['settings']['property'] = array(
'#type' => 'select',
'#title' => t('Sort property'),
'#required' => TRUE,
'#options' => backdrop_map_assoc($entity_info['schema_fields_sql']['base table']),
'#default_value' => $field['settings']['handler_settings']['sort']['property'],
);
}
elseif ($field['settings']['handler_settings']['sort']['type'] == 'field') {
$field['settings']['handler_settings']['sort'] += array(
'field' => NULL,
);
$fields = array();
foreach (field_info_instances($field['settings']['target_type']) as $bundle_name => $bundle_instances) {
foreach ($bundle_instances as $instance_name => $instance_info) {
$field_info = field_info_field($instance_name);
foreach ($field_info['columns'] as $column_name => $column_info) {
$fields[$instance_name . ':' . $column_name] = t('@label (column @column)', array('@label' => $instance_info['label'], '@column' => $column_name));
}
}
}
$form['sort']['settings']['field'] = array(
'#type' => 'select',
'#title' => t('Sort field'),
'#required' => TRUE,
'#options' => $fields,
'#default_value' => $field['settings']['handler_settings']['sort']['field'],
);
}
if ($field['settings']['handler_settings']['sort']['type'] != 'none') {
$field['settings']['handler_settings']['sort'] += array(
'direction' => 'ASC',
);
$form['sort']['settings']['direction'] = array(
'#type' => 'select',
'#title' => t('Sort direction'),
'#required' => TRUE,
'#options' => array(
'ASC' => t('Ascending'),
'DESC' => t('Descending'),
),
'#default_value' => $field['settings']['handler_settings']['sort']['direction'],
);
}
return $form;
}
public function getReferencableEntities($match = '', $match_operator = 'CONTAINS', $limit = 0) {
$options = array();
$entity_type = $this->field['settings']['target_type'];
if (is_null($match)) {
$match = '';
}
$match = trim(str_replace('""', '"', $match));
$query = $this->buildEntityFieldQuery($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) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$options[$bundle][$entity_id] = check_plain($this->getLabel($entity));
}
}
return $options;
}
public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
$query = $this->buildEntityFieldQuery($match, $match_operator);
return $query
->count()
->execute();
}
public function validateReferencableEntities(array $ids) {
if ($ids) {
$entity_type = $this->field['settings']['target_type'];
$query = $this->buildEntityFieldQuery();
$query->entityCondition('entity_id', $ids, 'IN');
$result = $query->execute();
if (!empty($result[$entity_type])) {
return array_keys($result[$entity_type]);
}
}
return array();
}
public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
$bundled_entities = $this->getReferencableEntities($input, '=', 6);
$entities = array();
foreach ($bundled_entities as $entities_list) {
$entities += $entities_list;
}
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);
}
}
protected function buildEntityFieldQuery($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)) {
if (!empty($this->label_key)) {
$query->propertyCondition($this->label_key, $match, $match_operator);
}
}
$query->addTag($this->field['settings']['target_type'] . '_access');
$query->addTag('entityreference');
$query->addMetaData('field', $this->field);
$query->addMetaData('entityreference_selection_handler', $this);
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;
}
public function entityFieldQueryAlter(SelectQueryInterface $query) {
}
protected function reAlterQuery(SelectQueryInterface $query, $tag, $base_table) {
$query->addTag($tag);
$query->addMetaData('base_table', $base_table);
backdrop_alter(array('query', 'query_' . $tag), $query);
}
public function getLabel($entity) {
$target_type = $this->field['settings']['target_type'];
return entity_access('view', $target_type, $entity) ? entity_label($target_type, $entity) : t(ENTITYREFERENCE_DENIED);
}
public function ensureBaseTable(SelectQueryInterface $query) {
$tables = $query->getTables();
foreach ($tables as $table) {
if (empty($table['join'])) {
$alias = $table['alias'];
break;
}
}
if (strpos($alias, 'field_data_') !== 0) {
return $alias;
}
$target_type = $this->field['settings']['target_type'];
$entity_info = entity_get_info($target_type);
$target_type_base_table = $entity_info['base table'];
$id = $entity_info['entity keys']['id'];
return $query->innerJoin($target_type_base_table, NULL, "%alias.$id = $alias.entity_id");
}
}