function party_query_alter in Party 7
Implements hook_query_alter().
When loading an entity, join to the party_attached_entity table and add the party id and data set name to the query.
File
- ./
party.module, line 1427 - Provides a generic CRM party entity.
Code
function party_query_alter(&$query) {
// Deal with queries that need match our tags.
if ($query
->hasTag('party_access')) {
// See if we have access to archived parties.
$archived = !(user_access('administer parties') || user_access('view archived parties'));
// Find all the party tables and apply.
$tables =& $query
->getTables();
foreach ($tables as $alias => &$table) {
if ($table['table'] == 'party') {
// If it's the primary table, add as query conditions.
if ($table['join type'] === NULL) {
$query
->condition("{$alias}.hidden", 0);
if ($archived) {
$query
->condition("{$alias}.archived", 0);
}
}
else {
$table['condition'] .= " AND {$alias}.hidden = 0";
if ($archived) {
$table['condition'] .= " AND {$alias}.archived = 0";
}
}
}
}
}
// Cache the $entity_types and $query_tags
$entity_types =& drupal_static(__FUNCTION__ . 'entity_types', array());
$query_tags =& drupal_static(__FUNCTION__ . 'query_tags', array());
if (empty($entity_types)) {
// Get data sets.
$data_sets = party_get_data_set_info();
foreach ($data_sets as $name => $def) {
$entity_types[$def['entity type']] = $def['entity type'];
$query_tags[$def['entity type']] = $def['entity type'] . '_load_multiple';
}
}
// Don't do anything if none of the tags are present.
if (!call_user_func_array(array(
$query,
'hasAnyTag',
), $query_tags)) {
return;
}
// Find which tag the query has.
foreach ($query_tags as $entity_type => $tag) {
// Skip if this isn't the right entity type.
if (!call_user_func_array(array(
$query,
'hasTag',
), array(
$tag,
))) {
continue;
}
$entity_info = entity_get_info($entity_type);
// Add the join to the query.
$join_condition = '%alias.eid = base.' . $entity_info['entity keys']['id'] . ' AND %alias.entity_type = :entity_type';
$join_args = array(
':entity_type' => $entity_type,
);
$pae_alias = $query
->leftJoin('party_attached_entity', 'pae', $join_condition, $join_args);
$query
->addField($pae_alias, 'pid', 'party_attaching_party');
$query
->addField($pae_alias, 'data_set', 'data_set_name');
break;
}
}