class DomainMenusSettingsForm in Domain Menus for Domains 3.x
Same name and namespace in other branches
- 9.x src/Form/DomainMenusSettingsForm.php \Drupal\domain_menus\Form\DomainMenusSettingsForm
- 9.1.x src/Form/DomainMenusSettingsForm.php \Drupal\domain_menus\Form\DomainMenusSettingsForm
Class DomainMenusSettingsForm.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\domain_menus\Form\DomainMenusSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of DomainMenusSettingsForm
1 string reference to 'DomainMenusSettingsForm'
File
- src/
Form/ DomainMenusSettingsForm.php, line 18
Namespace
Drupal\domain_menus\FormView source
class DomainMenusSettingsForm extends ConfigFormBase {
/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Drupal\Core\Messenger\MessengerInterface definition.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Config settings.
*
* @var string
*/
const SETTINGS = 'domain_menus.settings';
/**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($config_factory);
$this->messenger = $messenger;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('messenger'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'domain_menus_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config(static::SETTINGS);
$form['domain_menus_menu_names'] = [
'#type' => 'textarea',
'#rows' => 5,
'#columns' => 40,
'#title' => $this
->t('Domain menus'),
'#default_value' => $config
->get('domain_menus_menu_names'),
'#description' => $this
->t('Enter unique menu names/identifiers (less than 10 characters each, alphanumeric only), one per line.'),
];
$form['domain_menus_filter_node_autocomplete'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Filter internal node link autocomplete results on menu link form'),
'#default_value' => $config
->get('domain_menus_filter_node_autocomplete') ?? 0,
'#description' => $this
->t('Makes autocomplete show only same domain nodes. Prevents creating inaccessible relative path links.'),
];
$form['domain_menus_create'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Operation: Create menus'),
'#default_value' => 0,
'#description' => $this
->t('Create domain menus (not already existent) for every domain.'),
];
$form['domain_menus_delete'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Operation: Delete menus'),
'#default_value' => 0,
'#description' => $this
->t('Delete all domain menus and all links in them!'),
];
return parent::buildForm($form, $form_state);
}
/**
* Returns an array of settings keys.
*/
public function settingsKeys() {
return [
'domain_menus_menu_names',
'domain_menus_filter_node_autocomplete',
];
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config(static::SETTINGS);
foreach ($this
->settingsKeys() as $key) {
$config
->set($key, $form_state
->getValue($key));
}
$config
->save();
if ($form_state
->getValue('domain_menus_delete')) {
$menus = $this->entityTypeManager
->getStorage('menu')
->loadMultiple();
foreach ($menus as $menu) {
$menu_domains = $menu
->getThirdPartySetting("domain_menus", "domains");
if ($menu_domains !== NULL) {
$menu
->delete();
}
}
$this->messenger
->addMessage($this
->t('Domain menu(s) deleted.'), 'status');
}
if ($form_state
->getValue('domain_menus_create')) {
$count = 0;
$domain_menus_menu_names = $form_state
->getValue('domain_menus_menu_names');
if (!empty($domain_menus_menu_names)) {
$menu_names = explode("\r\n", $domain_menus_menu_names);
$domains = $this->entityTypeManager
->getStorage('domain')
->loadMultiple();
if (!empty($domains)) {
foreach ($domains as $domain) {
$domain_id = $domain
->id();
$domain_domainid = $domain
->getDomainId();
$domain_label = Markup::create($domain
->label());
foreach ($menu_names as $menu_name) {
if (!empty($menu_name)) {
$menu_id = trim($this
->t(DOMAIN_MENUS_MENU_ID_PATTERN, [
'@domain_domainid' => $domain_domainid,
'@menu_name' => $menu_name,
]));
$menu_label = trim($this
->t(DOMAIN_MENUS_MENU_LABEL_PATTERN, [
'@domain_label' => $domain_label,
'@menu_name' => $menu_name,
]));
if (!($menu = Menu::load($menu_id))) {
$menu = Menu::create(array(
'id' => $menu_id,
'label' => $menu_label,
));
$count++;
}
$menu
->setThirdPartySetting('domain_menus', 'domains', array(
$domain_id => $domain_id,
));
$menu
->setThirdPartySetting('domain_menus', 'auto-created', 1);
$menu
->save();
}
}
}
}
}
$this->messenger
->addMessage($this
->t('@count domain menu(s) created.', [
'@count' => $count,
]), 'status');
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$domain_menus_menu_names = $form_state
->getValue('domain_menus_menu_names');
if (!empty($domain_menus_menu_names)) {
$menu_names = explode("\r\n", $domain_menus_menu_names);
foreach ($menu_names as $menu_name) {
if (strlen($menu_name) > 9 || !ctype_alnum($menu_name)) {
$form_state
->setErrorByName('domain_menus_menu_names', $this
->t('Domain menu names must be alphanumeric and less than 10 characters each.'));
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
DomainMenusSettingsForm:: |
protected | property | Drupal\Core\Entity\EntityTypeManagerInterface definition. | |
DomainMenusSettingsForm:: |
protected | property |
Drupal\Core\Messenger\MessengerInterface definition. Overrides MessengerTrait:: |
|
DomainMenusSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
DomainMenusSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
DomainMenusSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
DomainMenusSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
DomainMenusSettingsForm:: |
constant | Config settings. | ||
DomainMenusSettingsForm:: |
public | function | Returns an array of settings keys. | |
DomainMenusSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
DomainMenusSettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
DomainMenusSettingsForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
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. | |
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. | |
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:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. |