function domain_entity_query_alter in Domain Access Entity 8
Same name and namespace in other branches
- 7 domain_entity.module \domain_entity_query_alter()
Implements hook_query_alter().
Alter the enabled entities select query, add domain access conditions.
File
- ./
domain_entity.module, line 192 - Enables domain access for entities, and access query alter.
Code
function domain_entity_query_alter(AlterableInterface $query) {
if (defined('MAINTENANCE_MODE') && (MAINTENANCE_MODE == 'install' || MAINTENANCE_MODE == 'update')) {
// Do not fire at install & update time.
return;
}
if (!\Drupal::lock()
->lockMayBeAvailable('router_rebuild')) {
// Do not fire at route rebuild time.
return;
}
if (!\Drupal::config('domain_entity.settings')
->get('bypass_access_conditions') && method_exists($query, 'getTables')) {
// Have we to check access for this entity types.
$allowed_types = domain_entity_allowed_entity_types();
$entity_type_id = $query
->getMetaData('entity_type');
if (!isset($allowed_types[$entity_type_id])) {
return;
}
// Skip access check for query without related tag.
if (!$query
->hasTag($entity_type_id . '_access')) {
return;
}
// Get primary key of base table from entity type definition.
$entity_key = $allowed_types[$entity_type_id]
->getKey('id');
// Get the accessible domain id's by the current user, in the current path.
$accessible_domain_ids = domain_entity_get_user_available_domains();
$tables = $query
->getTables();
$base_table_alias = key($tables);
$field_name = DomainEntityMapper::FIELD_NAME;
// This is an enabled domain entity,
// prepare our custom access conditions.
$domain_query = \Drupal::database()
->select($entity_type_id . '__' . $field_name, 'dom');
$domain_query
->where('dom.entity_id = ' . $base_table_alias . '.' . $entity_key);
$domain_query
->addExpression('1');
// No domain restrictions found.
$condition = new Condition('OR');
$condition
->notExists($domain_query);
// Or restrictions contains allowed domains.
$domain_query = clone $domain_query;
$domain_query
->condition('dom.' . $field_name . '_target_id', $accessible_domain_ids, 'IN');
$condition
->exists($domain_query);
$query
->condition($condition);
}
}