You are here

function domain_entity_query_alter in Domain Access Entity 7

Same name and namespace in other branches
  1. 8 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 131
Defines field (e.g. domain_entity) for entities, and access query alter.

Code

function domain_entity_query_alter(&$query) {
  if (!variable_get('domain_entity_bypass_access_conditions', FALSE) && method_exists($query, 'getTables')) {
    $tables = $query
      ->getTables();
    $base_table_alias = key($tables);
    $base_table = $tables[$base_table_alias]['table'];

    // Have we to check access for this entity types.
    $domain_entity_base_tables = domain_entity_allowed_entity_types_base_tables();
    if (!in_array($base_table, array_keys($domain_entity_base_tables))) {
      return;
    }

    // This is an enabled domain entity,
    // prepare our custom access conditions.
    // Get the accessible domain id's by the current user, in the current path.
    $accessible_domain_ids = domain_entity_get_user_available_domains();

    // Show to all user the entities that are assigned to all domains,
    // add the joker domain entity value to the list of the domain id's.
    $accessible_domain_ids[] = DOMAIN_ENTITY_SEND_TO_ALL;

    // Get the entity_type and key for this base_table:
    $entity_type = $domain_entity_base_tables[$base_table]['entity type'];
    $entity_key = $domain_entity_base_tables[$base_table]['entity key'];

    // Get the field name for this entity:
    $field_instance = domain_entity_entity_field_instance($entity_type);
    $field_instance_name = $field_instance['name'];

    // Add our subquery condition:
    $domain_query = db_select('field_data_' . $field_instance_name, 'dom');
    $domain_query
      ->where('dom.entity_id = ' . $base_table_alias . '.' . $entity_key);
    $domain_query
      ->condition('dom.' . $field_instance_name . '_domain_id', $accessible_domain_ids, 'IN');
    $domain_query
      ->fields('dom', array(
      $field_instance_name . '_domain_id',
    ));
    $query
      ->exists($domain_query);
  }
}