DomainTokenBlock.php in Domain Access 8
File
domain/src/Plugin/Block/DomainTokenBlock.php
View source
<?php
namespace Drupal\domain\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\domain\DomainInterface;
class DomainTokenBlock extends DomainBlockBase {
public function access(AccountInterface $account, $return_as_object = FALSE) {
$access = AccessResult::allowedIfHasPermissions($account, [
'administer domains',
'view domain information',
], 'OR');
return $return_as_object ? $access : $access
->isAllowed();
}
public function build() {
$domain = \Drupal::service('domain.negotiator')
->getActiveDomain();
if (!$domain) {
return [
'#markup' => $this
->t('No domain record could be loaded.'),
];
}
$header = [
$this
->t('Token'),
$this
->t('Value'),
];
return [
'#theme' => 'table',
'#rows' => $this
->renderTokens($domain),
'#header' => $header,
];
}
private function renderTokens(DomainInterface $domain) {
$rows = [];
$token = \Drupal::token();
$tokens = $token
->getInfo();
$domain_tokens = [
'domain',
'current-domain',
'default-domain',
];
foreach ($domain_tokens as $key) {
if (isset($tokens['tokens'][$key])) {
$data = [];
if ($key == 'domain') {
$data['domain'] = $domain;
}
foreach ($tokens['tokens'][$key] as $name => $info) {
$string = "[{$key}:{$name}]";
$rows[] = [
$string,
$token
->replace($string, $data),
];
}
}
}
return $rows;
}
}