123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- <?php
- class EntityReference_SelectionHandler_Generic implements EntityReference_SelectionHandler {
-
- 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'])) {
- return EntityReference_SelectionHandler_Broken::getInstance($field, $instance);
- }
- if (class_exists($class_name = 'EntityReference_SelectionHandler_Generic_' . $target_entity_type)) {
- return new $class_name($field, $instance, $entity_type, $entity);
- }
- else {
- return new EntityReference_SelectionHandler_Generic($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' => drupal_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 = NULL, $match_operator = 'CONTAINS', $limit = 0) {
- $options = array();
- $entity_type = $this->field['settings']['target_type'];
- $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) {
- $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);
- }
- }
-
- 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)) {
- $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);
- }
- }
-
- $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) {
-
-
- $old_tags = $query->alterTags;
- $old_metadata = $query->alterMetaData;
- $query->alterTags = array($tag => TRUE);
- $query->alterMetaData['base_table'] = $base_table;
- drupal_alter(array('query', 'query_' . $tag), $query);
-
- $query->alterTags = $old_tags;
- $query->alterMetaData = $old_metadata;
- }
-
- public function getLabel($entity) {
- return entity_label($this->field['settings']['target_type'], $entity);
- }
-
- 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);
- $id = $entity_info['entity keys']['id'];
-
- return $query->innerJoin($target_type, NULL, "$target_type.$id = $alias.entity_id");
- }
- }
- class EntityReference_SelectionHandler_Generic_node extends EntityReference_SelectionHandler_Generic {
- public function entityFieldQueryAlter(SelectQueryInterface $query) {
-
-
-
-
-
- if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
- $base_table = $this->ensureBaseTable($query);
- $query->condition("$base_table.status", NODE_PUBLISHED);
- }
- }
- }
- class EntityReference_SelectionHandler_Generic_user extends EntityReference_SelectionHandler_Generic {
- public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
- $query = parent::buildEntityFieldQuery($match, $match_operator);
-
- if (isset($match)) {
- $query->propertyCondition('name', $match, $match_operator);
- }
-
-
-
- if (!user_access('administer users')) {
- $query->propertyCondition('status', 1);
- }
- return $query;
- }
- public function entityFieldQueryAlter(SelectQueryInterface $query) {
- if (user_access('administer users')) {
-
-
-
- $conditions = &$query->conditions();
- foreach ($conditions as $key => $condition) {
- if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'users.name') {
-
- unset($conditions[$key]);
-
-
-
- $or = db_or();
- $or->condition($condition['field'], $condition['value'], $condition['operator']);
-
-
-
-
-
- $value_part = db_and();
- $value_part->condition('anonymous_name', $condition['value'], $condition['operator']);
- $value_part->compile(Database::getConnection(), $query);
- $or->condition(db_and()
- ->where(str_replace('anonymous_name', ':anonymous_name', (string) $value_part), $value_part->arguments() + array(':anonymous_name' => format_username(user_load(0))))
- ->condition('users.uid', 0)
- );
- $query->condition($or);
- }
- }
- }
- }
- }
- class EntityReference_SelectionHandler_Generic_comment extends EntityReference_SelectionHandler_Generic {
- public function entityFieldQueryAlter(SelectQueryInterface $query) {
-
-
-
- if (!user_access('administer comments')) {
- $base_table = $this->ensureBaseTable($query);
- $query->condition("$base_table.status", COMMENT_PUBLISHED);
- }
-
-
-
- $tables = $query->getTables();
- $base_table = key($tables);
- $node_alias = $query->innerJoin('node', 'n', '%alias.nid = ' . $base_table . '.nid');
-
- $this->reAlterQuery($query, 'node_access', $node_alias);
-
-
-
- $conditions = &$query->conditions();
- foreach ($conditions as $key => &$condition) {
- if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'node_type') {
- $condition['field'] = $node_alias . '.type';
- foreach ($condition['value'] as &$value) {
- if (substr($value, 0, 13) == 'comment_node_') {
- $value = substr($value, 13);
- }
- }
- break;
- }
- }
-
-
-
- if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
- $query->condition($node_alias . '.status', 1);
- }
- }
- }
- class EntityReference_SelectionHandler_Generic_file extends EntityReference_SelectionHandler_Generic {
- public function entityFieldQueryAlter(SelectQueryInterface $query) {
-
- $tables = $query->getTables();
- $base_table = key($tables);
- $query->condition('status', FILE_STATUS_PERMANENT);
-
-
-
- return $query;
- }
- public function getLabel($entity) {
-
-
- return $entity->filename !== '' ? $entity->filename : basename($entity->uri);
- }
- }
- class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityReference_SelectionHandler_Generic {
- public function entityFieldQueryAlter(SelectQueryInterface $query) {
-
-
-
- $base_table = $this->ensureBaseTable($query);
- $vocabulary_alias = $query->innerJoin('taxonomy_vocabulary', 'n', '%alias.vid = ' . $base_table . '.vid');
- $query->addMetadata('base_table', $vocabulary_alias);
-
- $this->reAlterQuery($query, 'taxonomy_vocabulary_access', $vocabulary_alias);
-
-
-
- $conditions = &$query->conditions();
- foreach ($conditions as $key => &$condition) {
- if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'vocabulary_machine_name') {
- $condition['field'] = $vocabulary_alias . '.machine_name';
- break;
- }
- }
- }
-
- public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
- if ($match || $limit) {
- return parent::getReferencableEntities($match , $match_operator, $limit);
- }
- $options = array();
- $entity_type = $this->field['settings']['target_type'];
-
- $entity_info = entity_get_info('taxonomy_term');
- $bundles = !empty($this->field['settings']['handler_settings']['target_bundles']) ? $this->field['settings']['handler_settings']['target_bundles'] : array_keys($entity_info['bundles']);
- foreach ($bundles as $bundle) {
- if ($vocabulary = taxonomy_vocabulary_machine_name_load($bundle)) {
- if ($terms = taxonomy_get_tree($vocabulary->vid, 0)) {
- foreach ($terms as $term) {
- $options[$vocabulary->machine_name][$term->tid] = str_repeat('-', $term->depth) . check_plain($term->name);
- }
- }
- }
- }
- return $options;
- }
- }
|