Search Entities when using Domain Access | Drupal 8

Search Entities when using Domain Access

Submitted by editor on Wed, 05/23/2018 - 22:14
Question

How to fiend entities when using domain access ?

Example 1 : 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();
print_r($nids);

If you can't get all domains data, do as the Example 2.

Example 2 : Using admin user 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();
print_r($nids);

// Switch back to old session.
$accountSwitcher->switchBack();

 

Add new comment