You are here

function _ctools_buildQuery in Chaos Tool Suite (ctools) 7

Use EntityReference_SelectionHandler_Generic class to build our search query.

1 call to _ctools_buildQuery()
_ctools_getReferencableEntities in includes/content.menu.inc
Private function to get referencable entities. Based on code from the Entity Reference module.

File

includes/content.menu.inc, line 68
Contains menu item registration for the content tool.

Code

function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') {
  $base_table = $entity_info['base table'];
  $label_key = $entity_info['entity keys']['label'];
  $query = db_select($base_table)
    ->fields($base_table, array(
    $entity_info['entity keys']['id'],
  ));
  if (isset($match)) {
    if (isset($label_key)) {
      $query
        ->condition($base_table . '.' . $label_key, '%' . $match . '%', $match_operator);
    }
    else {
      return array();
    }
  }

  // Add a generic entity access tag to the query.
  $query
    ->addTag('ctools');

  // We have to perform two checks. First check is a query alter (with tags)
  // in an attempt to only return results that have access. However, this is
  // not full-proof since entities many not implement hook_access query tag.
  // This is why we have a second check after entity load, before we display
  // the label of an entity.
  if ($entity_type == 'comment') {

    // Adding the 'comment_access' tag is sadly insufficient for comments: core

    // requires us to also know about the concept of 'published' and

    // 'unpublished'.
    if (!user_access('administer comments')) {
      $query
        ->condition('comment.status', COMMENT_PUBLISHED);
    }

    // Join to a node if the user does not have node access bypass permissions

    // to obey node published permissions.
    if (!user_access('bypass node access')) {
      $node_alias = $query
        ->innerJoin('node', 'n', '%alias.nid = comment.nid');
      $query
        ->condition($node_alias . '.status', NODE_PUBLISHED);
    }
    $query
      ->addTag('node_access');
  }
  else {
    $query
      ->addTag($entity_type . '_access');
  }

  // Add the sort option.
  if (isset($label_key)) {
    $query
      ->orderBy($base_table . '.' . $label_key, 'ASC');
  }
  return $query;
}