DomainSwitcherBlock.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Drupal\domain\Plugin\Block;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Session\AccountInterface;
  5. /**
  6. * Provides a block that links to all domains.
  7. *
  8. * @Block(
  9. * id = "domain_switcher_block",
  10. * admin_label = @Translation("Domain switcher (for admins and testing)")
  11. * )
  12. */
  13. class DomainSwitcherBlock extends DomainBlockBase {
  14. /**
  15. * Overrides \Drupal\block\BlockBase::access().
  16. */
  17. public function access(AccountInterface $account, $return_as_object = FALSE) {
  18. $access = AccessResult::allowedIfHasPermissions($account, ['administer domains', 'use domain switcher block'], 'OR');
  19. return $return_as_object ? $access : $access->isAllowed();
  20. }
  21. /**
  22. * Build the output.
  23. */
  24. public function build() {
  25. /** @var \Drupal\domain\DomainInterface $active_domain */
  26. $active_domain = \Drupal::service('domain.negotiator')->getActiveDomain();
  27. $items = [];
  28. /** @var \Drupal\domain\DomainInterface $domain */
  29. foreach (\Drupal::entityTypeManager()->getStorage('domain')->loadMultipleSorted() as $domain) {
  30. $string = $domain->getLink();
  31. if (!$domain->status()) {
  32. $string .= '*';
  33. }
  34. if ($domain->id() == $active_domain->id()) {
  35. $string = '<em>' . $string . '</em>';
  36. }
  37. $items[] = ['#markup' => $string];
  38. }
  39. return [
  40. '#theme' => 'item_list',
  41. '#items' => $items,
  42. ];
  43. }
  44. }