View source
<?php
namespace Drupal\domain;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\domain\DomainInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DomainListBuilder extends DraggableListBuilder {
protected $entitiesKey = 'domains';
protected $currentUser;
protected $destinationHandler;
protected $entityTypeManager;
protected $accessHandler;
protected $moduleHandler;
protected $domainStorage;
protected $domainElementManager;
protected $userStorage;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('current_user'), $container
->get('redirect.destination'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('domain.element_manager'));
}
public function __construct(EntityTypeInterface $entity_type, DomainStorageInterface $domain_storage, AccountInterface $account, RedirectDestinationInterface $destination_handler, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, DomainElementManagerInterface $domain_element_manager) {
parent::__construct($entity_type, $domain_storage);
$this->entityTypeId = $entity_type
->id();
$this->domainStorage = $domain_storage;
$this->entityType = $entity_type;
$this->currentUser = $account;
$this->destinationHandler = $destination_handler;
$this->entityTypeManager = $entity_type_manager;
$this->accessHandler = $this->entityTypeManager
->getAccessControlHandler('domain');
$this->moduleHandler = $module_handler;
$this->domainElementManager = $domain_element_manager;
$this->userStorage = $this->entityTypeManager
->getStorage('user');
$this->limit = 50;
}
public function getFormId() {
return 'domain_admin_overview_form';
}
public function getOperations(EntityInterface $entity) {
$operations = parent::getOperations($entity);
$destination = $this->destinationHandler
->getAsArray();
$default = $entity
->isDefault();
$id = $entity
->id();
$access = $this->accessHandler
->checkAccess($entity, 'update');
if ($access
->isForbidden()) {
return $operations;
}
$super_admin = $this->currentUser
->hasPermission('administer domains');
if ($super_admin || $this->currentUser
->hasPermission('access inactive domains')) {
if ($entity
->status() && !$default) {
$operations['disable'] = [
'title' => $this
->t('Disable'),
'url' => Url::fromRoute('domain.inline_action', [
'op' => 'disable',
'domain' => $id,
]),
'weight' => 50,
];
}
elseif (!$default) {
$operations['enable'] = [
'title' => $this
->t('Enable'),
'url' => Url::fromRoute('domain.inline_action', [
'op' => 'enable',
'domain' => $id,
]),
'weight' => 40,
];
}
}
if (!$default && $super_admin) {
$operations['default'] = [
'title' => $this
->t('Make default'),
'url' => Url::fromRoute('domain.inline_action', [
'op' => 'default',
'domain' => $id,
]),
'weight' => 30,
];
}
if (!$default && $this->accessHandler
->checkAccess($entity, 'delete')
->isAllowed()) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.domain.delete_form', [
'domain' => $id,
]),
'weight' => 20,
];
}
$operations += $this->moduleHandler
->invokeAll('domain_operations', [
$entity,
$this->currentUser,
]);
foreach ($operations as $key => $value) {
if (isset($value['query']['token'])) {
$operations[$key]['query'] += $destination;
}
}
$default = $this->domainStorage
->loadDefaultDomain();
if ($default && $id == $default
->id()) {
unset($operations['delete']);
}
return $operations;
}
public function buildHeader() {
$header['label'] = $this
->t('Name');
$header['hostname'] = $this
->t('Hostname');
$header['status'] = $this
->t('Status');
$header['is_default'] = $this
->t('Default');
$header['scheme'] = $this
->t('Scheme');
$header += parent::buildHeader();
if (!$this->currentUser
->hasPermission('administer domains')) {
unset($header['weight']);
}
return $header;
}
public function buildRow(EntityInterface $entity) {
$admin = $this->accessHandler
->checkAccess($entity, 'view');
if ($admin
->isForbidden()) {
return;
}
$row['label'] = $entity
->label();
$row['hostname'] = [
'#markup' => $entity
->getLink(),
];
if ($entity
->isActive()) {
$row['hostname']['#prefix'] = '<strong>';
$row['hostname']['#suffix'] = '</strong>';
}
$row['status'] = [
'#markup' => $entity
->status() ? $this
->t('Active') : $this
->t('Inactive'),
];
$row['is_default'] = [
'#markup' => $entity
->isDefault() ? $this
->t('Yes') : $this
->t('No'),
];
$row['scheme'] = [
'#markup' => $entity
->getRawScheme(),
];
$row += parent::buildRow($entity);
if ($entity
->getRawScheme() === 'variable') {
$row['scheme']['#markup'] .= ' (' . $entity
->getScheme(FALSE) . ')';
}
if (!$this->currentUser
->hasPermission('administer domains')) {
unset($row['weight']);
}
return $row;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form[$this->entitiesKey]['#domains'] = $this->entities;
$form['actions']['submit']['#value'] = $this
->t('Save configuration');
if (!$this->currentUser
->hasPermission('administer domains')) {
$form['actions']['submit']['#access'] = FALSE;
unset($form['#tabledrag']);
}
$count = count($this->domainStorage
->loadMultiple()) + 1;
foreach (Element::children($form['domains']) as $key) {
if (isset($form['domains'][$key]['weight'])) {
$form['domains'][$key]['weight']['#delta'] = $count;
}
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValue($this->entitiesKey) as $id => $value) {
if (isset($this->entities[$id]) && $this->entities[$id]
->get($this->weightKey) != $value['weight']) {
$this->entities[$id]
->set($this->weightKey, $value['weight']);
$this->entities[$id]
->set('hostname', $this->entities[$id]
->getCanonical());
$this->entities[$id]
->save();
}
}
}
private function sortByWeight($a, $b) {
if ($a['weight'] < $b['weight']) {
return 0;
}
return 1;
}
public function render() {
$form = $this
->formBuilder()
->getForm($this);
if ($this->limit) {
$form['pager'] = [
'#type' => 'pager',
];
}
return $form;
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery()
->sort($this->entityType
->getKey('weight'));
if (!$this->currentUser
->hasPermission('administer domains') && !$this->currentUser
->hasPermission('view domain list')) {
$user = $this->userStorage
->load($this->currentUser
->id());
$allowed = $this->domainElementManager
->getFieldValues($user, DomainInterface::DOMAIN_ADMIN_FIELD);
$query
->condition('id', array_keys($allowed), 'IN');
}
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
}