How to search Entities when using Domain Access in drupal8?

Entity query without changing user session

$query = \Drupal::entityQuery('node');
$query->condition(DOMAIN_ACCESS_FIELD, 'domain_id');// Set the domain id hear 
$query->condition('type', 'article'); 
$query->condition('status', 1);
$nids = $query->execute(); 
echo '<pre>';print_r($nids);echo '</pre>';// return all node type article

    Entity query using user session (admin session) 

    // Call the account switcher service
    $accountSwitcher = \Drupal::service('account_switcher');
    // Switch to the admin user
    $accountSwitcher->switchTo(new \Drupal\Core\Session\UserSession(['uid' => 1]));
    
    $query = \Drupal::entityQuery('node');
    $query->condition(DOMAIN_ACCESS_FIELD, 'domain_id'); // Set the domain id hear
    $query->condition('type', 'article');
    $query->condition('status', 1);
    $nids = $query->execute();
    echo '<pre>';print_r($nids);echo '</pre>';
    
    // Switch back to old session.
    $accountSwitcher->switchBack();

     

     

    Add new comment

    Plain text

    • No HTML tags allowed.
    • Lines and paragraphs break automatically.
    • Web page addresses and email addresses turn into links automatically.