DomainContentController.php in Domain Access 8        
                          
                  
                        
  
  
  
  
  
File
  domain_content/src/Controller/DomainContentController.php
  
    View source  
  <?php
namespace Drupal\domain_content\Controller;
use Drupal\Core\Link;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\domain\DomainInterface;
use Drupal\domain_access\DomainAccessManagerInterface;
class DomainContentController extends ControllerBase {
  
  public function buildList(array $options) {
    $account = $this
      ->getUser();
    $build = [
      '#theme' => 'table',
      '#header' => [
        $this
          ->t('Domain'),
        $options['column_header'],
      ],
    ];
    if ($account
      ->hasPermission($options['all_permission'])) {
      $build['#rows'][] = [
        Link::fromTextAndUrl($this
          ->t('All affiliates'), Url::fromUri('internal:/admin/content/' . $options['path'] . '/all_affiliates')),
        $this
          ->getCount($options['type']),
      ];
    }
    
    $domains = \Drupal::entityTypeManager()
      ->getStorage('domain')
      ->loadMultipleSorted();
    $manager = \Drupal::service('domain_access.manager');
    
    foreach ($domains as $domain) {
      if ($account
        ->hasPermission($options['all_permission']) || $manager
        ->hasDomainPermissions($account, $domain, [
        $options['permission'],
      ])) {
        $row = [
          Link::fromTextAndUrl($domain
            ->label(), Url::fromUri('internal:/admin/content/' . $options['path'] . '/' . $domain
            ->id())),
          $this
            ->getCount($options['type'], $domain),
        ];
        $build['#rows'][] = $row;
      }
    }
    return $build;
  }
  
  public function contentList() {
    $options = [
      'type' => 'node',
      'column_header' => $this
        ->t('Content count'),
      'permission' => 'publish to any assigned domain',
      'all_permission' => 'publish to any domain',
      'path' => 'domain-content',
    ];
    return $this
      ->buildList($options);
  }
  
  public function editorsList() {
    $options = [
      'type' => 'user',
      'column_header' => $this
        ->t('Editor count'),
      'permission' => 'assign domain editors',
      'all_permission' => 'assign editors to any domain',
      'path' => 'domain-editors',
    ];
    return $this
      ->buildList($options);
  }
  
  protected function getCount($entity_type = 'node', DomainInterface $domain = NULL) {
    if (is_null($domain)) {
      $field = DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD;
      $value = 1;
    }
    else {
      $field = DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD;
      $value = $domain
        ->id();
    }
    
    $query = \Drupal::entityQuery($entity_type)
      ->condition($field, $value)
      ->accessCheck(FALSE);
    return count($query
      ->execute());
  }
  
  protected function getUser() {
    $account = $this
      ->currentUser();
    
    return \Drupal::entityTypeManager()
      ->getStorage('user')
      ->load($account
      ->id());
  }
}