You are here

function search_by_page_attach_search_by_page_query_modify in Search by Page 8

Implements Search by Page hook_search_by_page_query_modify().

Adds to the Search by Page query.

File

search_by_page_attach/search_by_page_attach.module, line 210
Module file for Search by Page Attachments, a sub-module for Search by Page.

Code

function search_by_page_attach_search_by_page_query_modify($environment, $query) {

  // Note: At the moment, this function is assuming that we're only
  // searching for files attached to nodes. So we use the node access
  // permissions, and assume tacitly that the object type is node.
  // If this module is ever expanded to do other attachments, this will
  // need to be addressed!
  $cond = new \Drupal\Core\Database\Query\Condition('AND');
  if (!\Drupal::currentUser()
    ->hasPermission('access content')) {

    // This user cannot access content, so don't bother with the query
    // mods, they should not see anything from this module.
    $cond
      ->where('0=1');
    return $cond;
  }

  // Attach to our attachments table and the node table, using a subquery.
  $subquery = \Drupal::database()
    ->select('sbpa_attachments', 'sbpa_sq');
  $subquery
    ->condition('sbpa_sq.objtype', 'node');
  $subquery
    ->condition('sbpa_sq.environment', $environment);
  $subquery
    ->leftJoin('node', 'sbpa_n', 'sbpa_sq.objid = sbpa_n.nid');
  $subquery
    ->addTag('node_access');
  $subquery
    ->condition('sbpa_n.status', 1);
  $subquery
    ->addField('sbpa_sq', 'sbpaid', 'sbpaid');

  // If we're only looking for listed attachments, add condition.
  $listed_only = \Drupal::service('search_by_page.settings')
    ->getSetting('search_by_page_attach_only_listed', $environment, 0);
  if ($listed_only) {
    $subquery
      ->condition('sbpa_sq.display', 1);
  }
  $query
    ->leftJoin($subquery, 'sbpa_a', 'sbpa_a.sbpaid = sp.modid');
  return $cond;
}