Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save flocondetoile/6cc12d9b3dc7f507235261d908419c5a to your computer and use it in GitHub Desktop.

Select an option

Save flocondetoile/6cc12d9b3dc7f507235261d908419c5a to your computer and use it in GitHub Desktop.
/**
* Implements hook_query_TAG_alter().
*/
function micro_page_query_node_access_alter(\Drupal\Core\Database\Query\SelectInterface $query) {
// Read meta-data from query, if provided.
if (!($account = $query->getMetaData('account'))) {
$account = \Drupal::currentUser();
}
if ($account->hasPermission('bypass node access') || $account->hasPermission('link to any page')) {
return;
}
$route_name = \Drupal::routeMatch()->getRouteName();
static $active_site;
/** @var \Drupal\micro_site\SiteNegotiatorInterface $negotiator */
$negotiator = \Drupal::service('micro_site.negotiator');
if (!isset($active_site)) {
$active_site = $negotiator->getSite();
}
if ($active_site) {
// @TODO support node associated to multiple sites.
$query->condition('site_id', $active_site->id());
}
elseif ($route_name == 'system.entity_autocomplete') {
$previousUrl = \Drupal::request()->server->get('HTTP_REFERER');
$fake_request = Request::create($previousUrl);
$url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri());
// blabla code to fetch the site entity from the referer route object
$query->condition('site_id', $active_site->id());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment