DomainContentController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\domain_content\Controller;
  3. use Drupal\Core\Link;
  4. use Drupal\domain\DomainInterface;
  5. use Drupal\Core\Controller\ControllerBase;
  6. use Drupal\Core\Url;
  7. /**
  8. * Controller routines domain content pages.
  9. */
  10. class DomainContentController extends ControllerBase {
  11. /**
  12. * Builds the list of domains and relevant entities.
  13. *
  14. * @param array $options
  15. * A list of variables required to build editor or content pages.
  16. *
  17. * @see contentlist()
  18. *
  19. * @return array
  20. * A Drupal page build array.
  21. */
  22. public function buildList(array $options) {
  23. $account = $this->getUser();
  24. $build = [
  25. '#theme' => 'table',
  26. '#header' => [$this->t('Domain'), $options['column_header']],
  27. ];
  28. if ($account->hasPermission($options['all_permission'])) {
  29. $build['#rows'][] = [
  30. Link::fromTextAndUrl($this->t('All affiliates'), Url::fromUri('internal:/admin/content/' . $options['path'] . '/all_affiliates')),
  31. $this->getCount($options['type']),
  32. ];
  33. }
  34. // Loop through domains.
  35. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultipleSorted();
  36. $manager = \Drupal::service('domain_access.manager');
  37. /** @var \Drupal\domain\DomainInterface $domain */
  38. foreach ($domains as $domain) {
  39. if ($account->hasPermission($options['all_permission']) || $manager->hasDomainPermissions($account, $domain, [$options['permission']])) {
  40. $row = [
  41. Link::fromTextAndUrl($domain->label(), Url::fromUri('internal:/admin/content/' . $options['path'] . '/' . $domain->id())),
  42. $this->getCount($options['type'], $domain),
  43. ];
  44. $build['#rows'][] = $row;
  45. }
  46. }
  47. return $build;
  48. }
  49. /**
  50. * Generates a list of content by domain.
  51. */
  52. public function contentList() {
  53. $options = [
  54. 'type' => 'node',
  55. 'column_header' => $this->t('Content count'),
  56. 'permission' => 'publish to any assigned domain',
  57. 'all_permission' => 'publish to any domain',
  58. 'path' => 'domain-content',
  59. ];
  60. return $this->buildList($options);
  61. }
  62. /**
  63. * Generates a list of editors by domain.
  64. */
  65. public function editorsList() {
  66. $options = [
  67. 'type' => 'user',
  68. 'column_header' => $this->t('Editor count'),
  69. 'permission' => 'assign domain editors',
  70. 'all_permission' => 'assign editors to any domain',
  71. 'path' => 'domain-editors',
  72. ];
  73. return $this->buildList($options);
  74. }
  75. /**
  76. * Counts the content for a domain.
  77. *
  78. * @param string $entity_type
  79. * The entity type.
  80. * @param \Drupal\domain\DomainInterface $domain
  81. * The domain to query. If passed NULL, checks status for all affiliates.
  82. *
  83. * @return int
  84. * The content count for the given domain.
  85. */
  86. protected function getCount($entity_type = 'node', DomainInterface $domain = NULL) {
  87. if (is_null($domain)) {
  88. $field = DOMAIN_ACCESS_ALL_FIELD;
  89. $value = 1;
  90. }
  91. else {
  92. $field = DOMAIN_ACCESS_FIELD;
  93. $value = $domain->id();
  94. }
  95. // Note that we ignore node access so these queries work on any domain.
  96. $query = \Drupal::entityQuery($entity_type)
  97. ->condition($field, $value)
  98. ->accessCheck(FALSE);
  99. return count($query->execute());
  100. }
  101. /**
  102. * Returns a fully loaded user object for the current request.
  103. *
  104. * @return \Drupal\Core\Session\AccountInterface
  105. * The current user object.
  106. */
  107. protected function getUser() {
  108. $account = $this->currentUser();
  109. // Advanced grants for edit/delete require permissions.
  110. return \Drupal::entityTypeManager()->getStorage('user')->load($account->id());
  111. }
  112. }