function ShortcutHooks::queryShortcutAccessAlter

Same name and namespace in other branches
  1. main core/modules/shortcut/src/Hook/ShortcutHooks.php \Drupal\shortcut\Hook\ShortcutHooks::queryShortcutAccessAlter()

Implements hook_query_TAG_alter() for 'shortcut_access'.

Unless the user can administer shortcuts, restricts queries to only return shortcuts from the user's currently displayed shortcut set.

Attributes

#[Hook('query_shortcut_access_alter')]

See also

\Drupal\shortcut\ShortcutAccessControlHandler::checkAccess()

File

core/modules/shortcut/src/Hook/ShortcutHooks.php, line 170

Class

ShortcutHooks
Hook implementations for shortcut.

Namespace

Drupal\shortcut\Hook

Code

public function queryShortcutAccessAlter(AlterableInterface $query) : void {
  $account = $query->getMetaData('account') ?: \Drupal::currentUser();
  // Administrators can access all shortcuts.
  if ($account->hasPermission('administer shortcuts')) {
    return;
  }
  // Non-administrators can only access shortcuts in their displayed set.
  $shortcut_set = \Drupal::entityTypeManager()->getStorage('shortcut_set')
    ->getDisplayedToUser($account);
  $tables = $query->getTables();
  $base_table = $query->getMetaData('base_table');
  if (!$base_table) {
    foreach ($tables as $table_info) {
      if (!$table_info instanceof SelectInterface && $table_info['table'] === 'shortcut_field_data') {
        $base_table = $table_info['alias'];
        break;

      }
    }
  }
  if ($base_table) {
    $query->condition("{$base_table}.shortcut_set", $shortcut_set->id());
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.