Skip to main content
added a comment to make the code clearer
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id => $info) {
    // If 'join type' is not set, then it's the main table.a
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}

I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
    global $user;
    if ($user->uid > 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mymodule_get_main_table_alias($query->getTables())) {
        $alias = $query->leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query->condition(db_or()
          ->isNullcondition("$alias.uid");        
          ->condition("$alias.uid", $user->uid, '=')
        );          
      }
    }
  }
}

Notice that:

  • In $query->leftJoin(), %alias is replaced with the alias the query is using for the table being joined. As you are altering an existing query, you cannot know if the alias you chose is already used.
  • The result of $query->leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
  • To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
  • There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query->condition() are ANDed with each other.

hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id => $info) {
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}

I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
    global $user;
    if ($user->uid > 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mymodule_get_main_table_alias($query->getTables())) {
        $alias = $query->leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query->condition(db_or()
          ->isNullcondition("$alias.uid");        
          ->condition("$alias.uid", $user->uid, '=')
        );          
      }
    }
  }
}

Notice that:

  • In $query->leftJoin(), %alias is replaced with the alias the query is using for the table being joined. As you are altering an existing query, you cannot know if the alias you chose is already used.
  • The result of $query->leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
  • To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
  • There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query->condition() are ANDed with each other.

hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id => $info) {
    // If 'join type' is not set, then it's the main table.a
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}

I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
  global $user;
  if ($user->uid > 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mymodule_get_main_table_alias($query->getTables())) {
        $alias = $query->leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query->condition(db_or()
          ->isNullcondition("$alias.uid");        
          ->condition("$alias.uid", $user->uid, '=')
        );          
      }
    }
  }
}

Notice that:

  • In $query->leftJoin(), %alias is replaced with the alias the query is using for the table being joined. As you are altering an existing query, you cannot know if the alias you chose is already used.
  • The result of $query->leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
  • To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
  • There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query->condition() are ANDed with each other.
fixed a typo in the code
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id => $info) {
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}

I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
    global $user;
    if ($user->uid > 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mympdule_get_main_table_aliasmymodule_get_main_table_alias($query->getTables())) {
        $permission_alias$alias = $query->leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query->condition(db_or()
          ->isNullcondition("$permission_alias"$alias.uid");        
          ->condition(""$permission_alias"$alias.uid", $user->uid, '=')
        );          
      }
    }
  }
}

Notice that:

  • In $query->leftJoin(), %alias is replaced with the alias the query is using for the table being joined. As you are altering an existing query, you cannot know if the alias you chosenchose is already used.
  • The result of $query->leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
  • To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
  • There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query->condition() are ANDed with each other.

hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id => $info) {
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}

I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
    global $user;
    if ($user->uid > 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mympdule_get_main_table_alias($query->getTables())) {
        $permission_alias = $query->leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query->condition(db_or()
          ->isNullcondition("$permission_alias.uid");        
          ->condition(""$permission_alias.uid", $user->uid, '=')
        );          
      }
    }
  }
}

Notice that:

  • In $query->leftJoin(), %alias is replaced with the alias the query is using for the table being joined. As you are altering an existing query, you cannot know if the alias you chosen is already used.
  • The result of $query->leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
  • To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
  • There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query->condition() are ANDed with each other.

hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id => $info) {
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}

I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
    global $user;
    if ($user->uid > 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mymodule_get_main_table_alias($query->getTables())) {
        $alias = $query->leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query->condition(db_or()
          ->isNullcondition("$alias.uid");        
          ->condition("$alias.uid", $user->uid, '=')
        );          
      }
    }
  }
}

Notice that:

  • In $query->leftJoin(), %alias is replaced with the alias the query is using for the table being joined. As you are altering an existing query, you cannot know if the alias you chose is already used.
  • The result of $query->leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
  • To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
  • There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query->condition() are ANDed with each other.
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

hook_query_alter() is executed for every dynamic query run by Drupal. If you want to execute the hook for queries having a specific tag, you should implement hook_query_TAG_alter().

The code for getting the alias for the main table is similar to the following one.

function mymodule_get_main_table_alias($tables) {
  $alias = '';

  foreach ($tables as $id => $info) {
    if (empty($info['join type'])) {
      $alias = $id;
    }
  }

  return $alias;
}

I would write then the hook code as follows.

function mymodule_query_term_access_alter(QueryAlterableInterface $query) { 
    global $user;
    if ($user->uid > 1) {
    if (strstr(current_path(), 'admin/structure/taxonomy')) {
      if ($alias = mympdule_get_main_table_alias($query->getTables())) {
        $permission_alias = $query->leftJoin('term_permissions_user', 'tpu', "$alias.tid = %alias.tid");      
        $query->condition(db_or()
          ->isNullcondition("$permission_alias.uid");        
          ->condition(""$permission_alias.uid", $user->uid, '=')
        );          
      }
    }
  }
}

Notice that:

  • In $query->leftJoin(), %alias is replaced with the alias the query is using for the table being joined. As you are altering an existing query, you cannot know if the alias you chosen is already used.
  • The result of $query->leftJoin() is the alias being used for the joined table. It is better not to hard core that alias, for the reason said in the previous point.
  • To check if a field is NULL, you should not use QueryConditionInterface::condition(); as the documentation says, you should use QueryConditionInterface::isNull().
  • There is not need to create a QueryCondition object with db_and(); the conditions used to a query with $query->condition() are ANDed with each other.