class DomainAliasForm in Domain Access 8
Base form controller for domain alias edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\domain_alias\DomainAliasForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of DomainAliasForm
File
- domain_alias/
src/ DomainAliasForm.php, line 15
Namespace
Drupal\domain_aliasView source
class DomainAliasForm extends EntityForm {
/**
* The domain alias validator.
*
* @var \Drupal\domain_alias\DomainAliasValidatorInterface
*/
protected $validator;
/**
* The configuration factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $config;
/**
* The domain entity access control handler.
*
* @var \Drupal\domain\DomainAccessControlHandler
*/
protected $accessHandler;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The domain storage manager.
*
* @var \Drupal\domain\DomainStorageInterface
*/
protected $domainStorage;
/**
* The domain alias storage manager.
*
* @var \Drupal\domain_alias\DomainAliasStorageInterface
*/
protected $aliasStorage;
/**
* Constructs a DomainAliasForm object.
*
* @param \Drupal\domain_alias\DomainAliasValidatorInterface $validator
* The domain alias validator.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The configuration factory service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
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');
// Not loaded directly since it is not an interface.
$this->accessHandler = $this->entityTypeManager
->getAccessControlHandler('domain');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('domain_alias.validator'), $container
->get('config.factory'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\domain_alias\DomainAliasInterface $alias */
$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) {
// If the user cannot edit the domain, then don't show in the list.
$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);
}
/**
* Returns a list of valid redirect options for the form.
*
* @return array
* A list of valid redirect options.
*/
public function redirectOptions() {
return [
0 => $this
->t('Do not redirect'),
301 => $this
->t('301 redirect: Moved Permanently'),
302 => $this
->t('302 redirect: Found'),
];
}
/**
* Returns a list of valid environement options for the form.
*
* @return array
* A list of valid environment options.
*/
public function environmentOptions() {
$list = $this->config
->get('domain_alias.settings')
->get('environments');
$environments = [];
foreach ($list as $item) {
$environments[$item] = $item;
}
return $environments;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$errors = $this->validator
->validate($this->entity);
if (!empty($errors)) {
$form_state
->setErrorByName('pattern', $errors);
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\domain_alias\DomainAliasInterface $alias */
$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(),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DomainAliasForm:: |
protected | property | The domain entity access control handler. | |
DomainAliasForm:: |
protected | property | The domain alias storage manager. | |
DomainAliasForm:: |
protected | property | The configuration factory service. | |
DomainAliasForm:: |
protected | property | The domain storage manager. | |
DomainAliasForm:: |
protected | property |
The entity type manager. Overrides EntityForm:: |
|
DomainAliasForm:: |
protected | property | The domain alias validator. | |
DomainAliasForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
DomainAliasForm:: |
public | function | Returns a list of valid environement options for the form. | |
DomainAliasForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
DomainAliasForm:: |
public | function | Returns a list of valid redirect options for the form. | |
DomainAliasForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
DomainAliasForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
DomainAliasForm:: |
public | function | Constructs a DomainAliasForm object. | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |