@@ -169,7 +169,8 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
|
||||
if (!empty($results[$entity_type])) {
|
||||
$entities = entity_load($entity_type, array_keys($results[$entity_type]));
|
||||
foreach ($entities as $entity_id => $entity) {
|
||||
$options[$entity_id] = check_plain($this->getLabel($entity));
|
||||
list(,, $bundle) = entity_extract_ids($entity_type, $entity);
|
||||
$options[$bundle][$entity_id] = check_plain($this->getLabel($entity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,6 +306,42 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select
|
||||
public function getLabel($entity) {
|
||||
return entity_label($this->field['settings']['target_type'], $entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a base table exists for the query.
|
||||
*
|
||||
* If we have a field-only query, we want to assure we have a base-table
|
||||
* so we can later alter the query in entityFieldQueryAlter().
|
||||
*
|
||||
* @param $query
|
||||
* The Select query.
|
||||
*
|
||||
* @return
|
||||
* The alias of the base-table.
|
||||
*/
|
||||
public function ensureBaseTable(SelectQueryInterface $query) {
|
||||
$tables = $query->getTables();
|
||||
|
||||
// Check the current base table.
|
||||
foreach ($tables as $table) {
|
||||
if (empty($table['join'])) {
|
||||
$alias = $table['alias'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (strpos($alias, 'field_data_') !== 0) {
|
||||
// The existing base-table is the correct one.
|
||||
return $alias;
|
||||
}
|
||||
|
||||
// Join the known base-table.
|
||||
$target_type = $this->field['settings']['target_type'];
|
||||
$entity_info = entity_get_info($target_type);
|
||||
$id = $entity_info['entity keys']['id'];
|
||||
// Return the alias of the table.
|
||||
return $query->innerJoin($target_type, NULL, "$target_type.$id = $alias.entity_id");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,8 +357,8 @@ class EntityReference_SelectionHandler_Generic_node extends EntityReference_Sele
|
||||
// modules in use on the site. As long as one access control module is there,
|
||||
// it is supposed to handle this check.
|
||||
if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
|
||||
$tables = $query->getTables();
|
||||
$query->condition(key($tables) . '.status', NODE_PUBLISHED);
|
||||
$base_table = $this->ensureBaseTable($query);
|
||||
$query->condition("$base_table.status", NODE_PUBLISHED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -332,27 +369,6 @@ class EntityReference_SelectionHandler_Generic_node extends EntityReference_Sele
|
||||
* This only exists to workaround core bugs.
|
||||
*/
|
||||
class EntityReference_SelectionHandler_Generic_user extends EntityReference_SelectionHandler_Generic {
|
||||
/**
|
||||
* Implements EntityReferenceHandler::settingsForm().
|
||||
*/
|
||||
public static function settingsForm($field, $instance) {
|
||||
$settings = $field['settings']['handler_settings'];
|
||||
$form = parent::settingsForm($field, $instance);
|
||||
$form['referenceable_roles'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('User roles that can be referenced'),
|
||||
'#default_value' => isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array(),
|
||||
'#options' => user_roles(TRUE),
|
||||
);
|
||||
$form['referenceable_status'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('User status that can be referenced'),
|
||||
'#default_value' => isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active'),
|
||||
'#options' => array('active' => t('Active'), 'blocked' => t('Blocked')),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
|
||||
$query = parent::buildEntityFieldQuery($match, $match_operator);
|
||||
|
||||
@@ -361,33 +377,21 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
|
||||
$query->propertyCondition('name', $match, $match_operator);
|
||||
}
|
||||
|
||||
$field = $this->field;
|
||||
$settings = $field['settings']['handler_settings'];
|
||||
$referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
|
||||
$referenceable_status = isset($settings['referenceable_status']) ? array_filter($settings['referenceable_status']) : array('active' => 'active');
|
||||
|
||||
// If this filter is not filled, use the users access permissions.
|
||||
if (empty($referenceable_status)) {
|
||||
// Adding the 'user_access' tag is sadly insufficient for users: core
|
||||
// requires us to also know about the concept of 'blocked' and 'active'.
|
||||
if (!user_access('administer users')) {
|
||||
$query->propertyCondition('status', 1);
|
||||
}
|
||||
// Adding the 'user_access' tag is sadly insufficient for users: core
|
||||
// requires us to also know about the concept of 'blocked' and
|
||||
// 'active'.
|
||||
if (!user_access('administer users')) {
|
||||
$query->propertyCondition('status', 1);
|
||||
}
|
||||
elseif (count($referenceable_status) == 1) {
|
||||
$values = array('active' => 1, 'blocked' => 0);
|
||||
$query->propertyCondition('status', $values[key($referenceable_status)]);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function entityFieldQueryAlter(SelectQueryInterface $query) {
|
||||
$conditions = &$query->conditions();
|
||||
if (user_access('administer users')) {
|
||||
// If the user is administrator, we need to make sure to
|
||||
// In addition, if the user is administrator, we need to make sure to
|
||||
// match the anonymous user, that doesn't actually have a name in the
|
||||
// database.
|
||||
$conditions = &$query->conditions();
|
||||
foreach ($conditions as $key => $condition) {
|
||||
if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'users.name') {
|
||||
// Remove the condition.
|
||||
@@ -414,19 +418,6 @@ class EntityReference_SelectionHandler_Generic_user extends EntityReference_Sele
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$field = $this->field;
|
||||
$settings = $field['settings']['handler_settings'];
|
||||
$referenceable_roles = isset($settings['referenceable_roles']) ? array_filter($settings['referenceable_roles']) : array();
|
||||
if (!$referenceable_roles || !empty($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
|
||||
// Return early if "authenticated user" choosen.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($referenceable_roles[DRUPAL_AUTHENTICATED_RID])) {
|
||||
$query->join('users_roles', 'users_roles', 'users.uid = users_roles.uid');
|
||||
$query->condition('users_roles.rid', array_keys($referenceable_roles), 'IN');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,8 +432,8 @@ class EntityReference_SelectionHandler_Generic_comment extends EntityReference_S
|
||||
// requires us to also know about the concept of 'published' and
|
||||
// 'unpublished'.
|
||||
if (!user_access('administer comments')) {
|
||||
$tables = $query->getTables();
|
||||
$query->condition(key($tables) . '.status', COMMENT_PUBLISHED);
|
||||
$base_table = $this->ensureBaseTable($query);
|
||||
$query->condition("$base_table.status", COMMENT_PUBLISHED);
|
||||
}
|
||||
|
||||
// The Comment module doesn't implement any proper comment access,
|
||||
@@ -514,8 +505,7 @@ class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityRefer
|
||||
// The Taxonomy module doesn't implement any proper taxonomy term access,
|
||||
// and as a consequence doesn't make sure that taxonomy terms cannot be viewed
|
||||
// when the user doesn't have access to the vocabulary.
|
||||
$tables = $query->getTables();
|
||||
$base_table = key($tables);
|
||||
$base_table = $this->ensureBaseTable($query);
|
||||
$vocabulary_alias = $query->innerJoin('taxonomy_vocabulary', 'n', '%alias.vid = ' . $base_table . '.vid');
|
||||
$query->addMetadata('base_table', $vocabulary_alias);
|
||||
// Pass the query to the taxonomy access control.
|
||||
@@ -532,4 +522,32 @@ class EntityReference_SelectionHandler_Generic_taxonomy_term extends EntityRefer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements EntityReferenceHandler::getReferencableEntities().
|
||||
*/
|
||||
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'];
|
||||
|
||||
// We imitate core by calling taxonomy_get_tree().
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user