DomainServerBlock.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Drupal\domain\Plugin\Block;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Core\Access\AccessResult;
  5. use Drupal\Core\Session\AccountInterface;
  6. /**
  7. * Provides a server information block for a domain request.
  8. *
  9. * @Block(
  10. * id = "domain_server_block",
  11. * admin_label = @Translation("Domain server information")
  12. * )
  13. */
  14. class DomainServerBlock extends DomainBlockBase {
  15. /**
  16. * Overrides \Drupal\block\BlockBase::access().
  17. */
  18. public function access(AccountInterface $account, $return_as_object = FALSE) {
  19. $access = AccessResult::allowedIfHasPermissions($account, ['administer domains', 'view domain information'], 'OR');
  20. return $return_as_object ? $access : $access->isAllowed();
  21. }
  22. /**
  23. * Build the output.
  24. */
  25. public function build() {
  26. /** @var \Drupal\domain\DomainInterface $domain */
  27. $domain = \Drupal::service('domain.negotiator')->getActiveDomain();
  28. if (!$domain) {
  29. return [
  30. '#markup' => $this->t('No domain record could be loaded.'),
  31. ];
  32. }
  33. $header = [$this->t('Server'), $this->t('Value')];
  34. $rows[] = [
  35. $this->t('HTTP_HOST request'),
  36. Html::escape($_SERVER['HTTP_HOST']),
  37. ];
  38. // Check the response test.
  39. $domain->getResponse();
  40. $check = \Drupal::entityTypeManager()->getStorage('domain')->loadByHostname($_SERVER['HTTP_HOST']);
  41. $match = $this->t('Exact match');
  42. // This value is not translatable.
  43. $environment = 'default';
  44. if (!$check) {
  45. // Specific check for Domain Alias.
  46. if (isset($domain->alias)) {
  47. $match = $this->t('ALIAS: Using alias %id', ['%id' => $domain->alias->getPattern()]);
  48. // Get the environment.
  49. $environment = $domain->alias->getEnvironment();
  50. }
  51. else {
  52. $match = $this->t('FALSE: Using default domain.');
  53. }
  54. }
  55. $rows[] = [
  56. $this->t('Domain match'),
  57. $match,
  58. ];
  59. $rows[] = [
  60. $this->t('Environment'),
  61. $environment,
  62. ];
  63. $rows[] = [
  64. $this->t('Canonical hostname'),
  65. $domain->getCanonical(),
  66. ];
  67. $rows[] = [
  68. $this->t('Base path'),
  69. $domain->getPath(),
  70. ];
  71. $rows[] = [
  72. $this->t('Current URL'),
  73. $domain->getUrl(),
  74. ];
  75. $www = \Drupal::config('domain.settings')->get('www_prefix');
  76. $rows[] = [
  77. $this->t('Strip www prefix'),
  78. !empty($www) ? $this->t('On') : $this->t('Off'),
  79. ];
  80. $list = $domain->toArray();
  81. ksort($list);
  82. foreach ($list as $key => $value) {
  83. if (is_null($value)) {
  84. $value = $this->t('NULL');
  85. }
  86. elseif ($value === TRUE) {
  87. $value = $this->t('TRUE');
  88. }
  89. elseif ($value === FALSE) {
  90. $value = $this->t('FALSE');
  91. }
  92. elseif ($key == 'status' || $key == 'is_default') {
  93. $value = empty($value) ? $this->t('FALSE') : $this->t('TRUE');
  94. }
  95. $rows[] = [
  96. Html::escape($key),
  97. !is_array($value) ? Html::escape($value) : $this->printArray($value),
  98. ];
  99. }
  100. return [
  101. '#theme' => 'table',
  102. '#rows' => $rows,
  103. '#header' => $header,
  104. ];
  105. }
  106. /**
  107. * Prints array data for the server block.
  108. *
  109. * @param array $array
  110. * An array of data. Note that we support two levels of nesting.
  111. *
  112. * @return string
  113. * A suitable output string.
  114. */
  115. public function printArray(array $array) {
  116. $items = [];
  117. foreach ($array as $key => $val) {
  118. if (!is_array($val)) {
  119. $value = Html::escape($val);
  120. }
  121. else {
  122. $list = [];
  123. foreach ($val as $k => $v) {
  124. $list[] = $this->t('@key : @value', ['@key' => $k, '@value' => $v]);
  125. }
  126. $value = implode('<br />', $list);
  127. }
  128. $items[] = $this->t('@key : @value', ['@key' => $key, '@value' => $value]);
  129. }
  130. $variables['domain_server'] = [
  131. '#theme' => 'item_list',
  132. '#items' => $items,
  133. ];
  134. return render($variables);
  135. }
  136. }