View source
<?php
namespace Drupal\domain\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
class DomainNavBlock extends DomainBlockBase {
public $settings = [];
public function getCacheContexts() {
if ($this
->getSetting('link_options') == 'home') {
return [
'url.site',
];
}
return [
'url',
];
}
public function blockForm($form, FormStateInterface $form_state) {
$defaults = $this
->defaultConfiguration();
$elements['link_options'] = [
'#title' => $this
->t('Link paths'),
'#type' => 'radios',
'#required' => TRUE,
'#options' => [
'active' => $this
->t('Link to active url'),
'home' => $this
->t('Link to site home page'),
],
'#default_value' => !empty($this->configuration['link_options']) ? $this->configuration['link_options'] : $defaults['link_options'],
'#description' => $this
->t('Determines how links to each domain will be written. Note that some paths may not be accessible on all domains.'),
];
$options = [
'select' => $this
->t('JavaScript select list'),
'menus' => $this
->t('Menu-style tab links'),
'ul' => $this
->t('Unordered list of links'),
];
$elements['link_theme'] = [
'#type' => 'radios',
'#title' => t('Link theme'),
'#default_value' => !empty($this->configuration['link_theme']) ? $this->configuration['link_theme'] : $defaults['link_theme'],
'#options' => $options,
'#description' => $this
->t('Select how to display the block output.'),
];
$options = [
'name' => $this
->t('The domain display name'),
'hostname' => $this
->t('The raw hostname'),
'url' => $this
->t('The domain base URL'),
];
$elements['link_label'] = [
'#type' => 'radios',
'#title' => $this
->t('Link text'),
'#default_value' => !empty($this->configuration['link_label']) ? $this->configuration['link_label'] : $defaults['link_label'],
'#options' => $options,
'#description' => $this
->t('Select the text to display for each link.'),
];
return $elements;
}
public function blockSubmit($form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
foreach (array_keys($this
->defaultConfiguration()) as $element) {
$this->configuration[$element] = $form_state
->getValue($element);
}
}
}
public function access(AccountInterface $account, $return_as_object = FALSE) {
$access = AccessResult::allowedIfHasPermissions($account, [
'administer domains',
'use domain nav block',
], 'OR');
return $return_as_object ? $access : $access
->isAllowed();
}
public function build() {
$active_domain = \Drupal::service('domain.negotiator')
->getActiveDomain();
$access_handler = \Drupal::entityTypeManager()
->getAccessControlHandler('domain');
$account = \Drupal::currentUser();
$items = [];
$add_path = $this
->getSetting('link_options') == 'active';
foreach (\Drupal::entityTypeManager()
->getStorage('domain')
->loadMultipleSorted() as $domain) {
if ($add_path) {
$url = Url::fromUri($domain
->getUrl());
}
else {
$url = Url::fromUri($domain
->getPath());
}
$label = $domain
->label();
if ($this
->getSetting('link_label') == 'hostname') {
$label = $domain
->getHostname();
}
elseif ($this
->getSetting('link_label') == 'url') {
$label = $domain
->getPath();
}
if ($domain
->status() || $account
->hasPermission('access inactive domains')) {
if ($this
->getSetting('link_theme') == 'menus') {
$items[] = [
'title' => $label,
'url' => $url,
'attributes' => [],
'below' => [],
'is_expanded' => FALSE,
'is_collapsed' => FALSE,
'in_active_trail' => $domain
->isActive(),
];
}
elseif ($this
->getSetting('link_theme') == 'select') {
$items[] = [
'label' => $label,
'url' => $url
->toString(),
'active' => $domain
->isActive(),
];
}
else {
$items[] = [
'#markup' => Link::fromTextAndUrl($label, $url)
->toString(),
];
}
}
}
switch ($this
->getSetting('link_theme')) {
case 'select':
$build['#theme'] = 'domain_nav_block';
$build['#items'] = $items;
break;
case 'menus':
$build['#items'] = $items;
$build['#menu_name'] = 'domain-nav';
$build['#sorted'] = TRUE;
$build['#theme'] = 'menu__' . strtr($build['#menu_name'], '-', '_');
break;
case 'ul':
default:
$build['#theme'] = 'item_list';
$build['#items'] = $items;
break;
}
return $build;
}
public function defaultConfiguration() {
return [
'link_options' => 'home',
'link_theme' => 'ul',
'link_label' => 'name',
];
}
public function getSetting($key) {
if (isset($this->settings[$key])) {
return $this->settings[$key];
}
$defaults = $this
->defaultConfiguration();
if (isset($this->configuration[$key])) {
$this->settings[$key] = $this->configuration[$key];
}
else {
$this->settings[$key] = $defaults[$key];
}
return $this->settings[$key];
}
}