View source
<?php
namespace Drupal\domain_alias;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\domain\DomainStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DomainAliasForm extends EntityForm {
protected $validator;
protected $config;
protected $accessHandler;
protected $entityTypeManager;
protected $domainStorage;
protected $aliasStorage;
public function __construct(DomainAliasValidatorInterface $validator, ConfigFactoryInterface $config, EntityTypeManagerInterface $entity_type_manager) {
$this->validator = $validator;
$this->config = $config;
$this->entityTypeManager = $entity_type_manager;
$this->aliasStorage = $entity_type_manager
->getStorage('domain_alias');
$this->domainStorage = $entity_type_manager
->getStorage('domain');
$this->accessHandler = $this->entityTypeManager
->getAccessControlHandler('domain');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('domain_alias.validator'), $container
->get('config.factory'), $container
->get('entity_type.manager'));
}
public function form(array $form, FormStateInterface $form_state) {
$alias = $this->entity;
$form['domain_id'] = [
'#type' => 'value',
'#value' => $alias
->getDomainId(),
];
$form['pattern'] = [
'#type' => 'textfield',
'#title' => $this
->t('Pattern'),
'#size' => 40,
'#maxlength' => 80,
'#default_value' => $alias
->getPattern(),
'#description' => $this
->t('The matching pattern for this alias.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $alias
->id(),
'#machine_name' => [
'source' => [
'pattern',
],
'exists' => '\\Drupal\\domain_alias\\Entity\\DomainAlias::load',
],
];
$form['redirect'] = [
'#type' => 'select',
'#options' => $this
->redirectOptions(),
'#default_value' => $alias
->getRedirect(),
'#description' => $this
->t('Set an optional redirect directive when this alias is invoked.'),
];
$environments = $this
->environmentOptions();
$form['environment'] = [
'#type' => 'select',
'#options' => $environments,
'#default_value' => $alias
->getEnvironment(),
'#description' => $this
->t('Map the alias to a development environment.'),
];
$form['environment_help'] = [
'#type' => 'details',
'#open' => FALSE,
'#collapsed' => TRUE,
'#title' => $this
->t('Environment list'),
'#description' => $this
->t('The table below shows the registered aliases for each environment.'),
];
$domains = $this->domainStorage
->loadMultipleSorted();
$rows = [];
foreach ($domains as $domain) {
$access = $this->accessHandler
->checkAccess($domain, 'update');
if ($access
->isForbidden()) {
continue;
}
$row = [];
$row[] = $domain
->label();
foreach ($environments as $environment) {
$match_output = [];
if ($environment == 'default') {
$match_output[] = $domain
->getCanonical();
}
$matches = $this->aliasStorage
->loadByEnvironmentMatch($domain, $environment);
foreach ($matches as $match) {
$match_output[] = $match
->getPattern();
}
$output = [
'#items' => $match_output,
'#theme' => 'item_list',
];
$row[] = \Drupal::service('renderer')
->render($output);
}
$rows[] = $row;
}
$form['environment_help']['table'] = [
'#type' => 'table',
'#header' => array_merge([
$this
->t('Domain'),
], $environments),
'#rows' => $rows,
];
return parent::form($form, $form_state);
}
public function redirectOptions() {
return [
0 => $this
->t('Do not redirect'),
301 => $this
->t('301 redirect: Moved Permanently'),
302 => $this
->t('302 redirect: Found'),
];
}
public function environmentOptions() {
$list = $this->config
->get('domain_alias.settings')
->get('environments');
$environments = [];
foreach ($list as $item) {
$environments[$item] = $item;
}
return $environments;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$errors = $this->validator
->validate($this->entity);
if (!empty($errors)) {
$form_state
->setErrorByName('pattern', $errors);
}
}
public function save(array $form, FormStateInterface $form_state) {
$alias = $this->entity;
$edit_link = $alias
->toLink($this
->t('Edit'), 'edit-form')
->toString();
if ($alias
->save() == SAVED_NEW) {
\Drupal::messenger()
->addMessage($this
->t('Created new domain alias.'));
$this
->logger('domain_alias')
->notice('Created new domain alias %name.', [
'%name' => $alias
->label(),
'link' => $edit_link,
]);
}
else {
\Drupal::messenger()
->addMessage($this
->t('Updated domain alias.'));
$this
->logger('domain_alias')
->notice('Updated domain alias %name.', [
'%name' => $alias
->label(),
'link' => $edit_link,
]);
}
$form_state
->setRedirect('domain_alias.admin', [
'domain' => $alias
->getDomainId(),
]);
}
}