class RoleLoginPageSettings in Multiple role login pages 8
Add login page form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\role_login_page\Form\RoleLoginPageSettings
Expanded class hierarchy of RoleLoginPageSettings
1 string reference to 'RoleLoginPageSettings'
File
- src/
Form/ RoleLoginPageSettings.php, line 19 - Contains \Drupal\role_login_page\Form\RoleLoginPageSettings.
Namespace
Drupal\role_login_page\FormView source
class RoleLoginPageSettings extends FormBase {
/**
* @var
*/
protected $connection;
/**
* RoleLoginPageSettings constructor.
*/
public function __construct() {
$this->connection = Database::getConnection();
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return '_role_login_page_settings';
}
/**
*
* @param array $form
* @param FormStateInterface $form_state
* @return string
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$roles_arr = Role::loadMultiple();
foreach ($roles_arr as $role => $rolesObj) {
$roles[$role] = $rolesObj
->get('label');
}
$form['login_page_menu'] = [
'#type' => 'fieldset',
'#title' => t('Add login page'),
'#collapsible' => FALSE,
];
$form['login_page_menu']['loginmenu_url'] = [
'#type' => 'textfield',
'#title' => 'Login page url',
'#required' => TRUE,
'#description' => t('URL should exclude the basepath, i.e, "http://example.com". Add the path that should be used after base path, i.e, "user or admin/newconfig"'),
];
$form['login_page_menu']['username_label'] = [
'#type' => 'textfield',
'#title' => 'Username label',
];
$form['login_page_menu']['password_label'] = [
'#type' => 'textfield',
'#title' => 'Password label',
];
$form['login_page_menu']['submit_text'] = [
'#type' => 'textfield',
'#title' => 'Submit button text',
];
$form['login_page_menu']['page_title'] = [
'#type' => 'textfield',
'#title' => 'Page title',
];
$form['login_page_menu']['redirect_path'] = [
'#type' => 'textfield',
'#title' => 'Redirect path',
'#description' => t('Path should exclude the basepath, i.e, "http://example.com". Add the path that should be used after base path, i.e, "user or admin/newconfig"'),
];
$form['login_page_menu']['roles'] = [
'#type' => 'select',
'#title' => 'Select the user roles allowed to login through this page : ',
'#options' => $roles,
'#multiple' => TRUE,
'#required' => TRUE,
];
$form['login_page_menu']['parent_class'] = [
'#type' => 'textfield',
'#title' => 'Form parent class',
'#description' => t('This class will be added to the form element.'),
];
$form['login_page_menu']['role_mismatch_error_text'] = [
'#type' => 'textarea',
'#title' => 'Role mismatch error text',
];
$form['login_page_menu']['invalid_credentials_error_text'] = [
'#type' => 'textarea',
'#title' => 'Invalid credentials error text',
];
$form['login_page_menu']['submit'] = [
'#type' => 'submit',
'#value' => 'Create login page',
];
return $form;
}
/**
*
* @global type $base_url
* @param array $form
* @param FormStateInterface $form_state
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
global $base_url;
$url = trim($form_state
->getValue([
'loginmenu_url',
]));
$complete_url = $base_url . '/' . $url;
$complete_url = filter_var($complete_url, FILTER_SANITIZE_URL);
$replacements = [
'!',
'*',
"(",
")",
";",
"@",
"+",
"\$",
",",
"[",
"]",
];
$complete_url = str_replace($replacements, '', $complete_url);
if (!filter_var($complete_url, FILTER_VALIDATE_URL)) {
$form_state
->setErrorByName('loginmenu_url', $this
->t("@comurl is not a valid URL", [
'@comurl' => $complete_url,
]));
}
$menu_exists = \Drupal::service('path.validator')
->getUrlIfValid($url);
if ($menu_exists) {
$form_state
->setErrorByName('loginmenu_url', $this
->t('The menu URL already exists'));
}
$redirect_path = trim($form_state
->getValue([
'redirect_path',
]));
if ($redirect_path) {
$redirect_path_exists = \Drupal::service('path.validator')
->getUrlIfValid($redirect_path);
if ($redirect_path_exists) {
if (!$redirect_path_exists
->getRouteName()) {
$form_state
->setErrorByName('redirect_path', $this
->t('Please enter a valid redirect path.'));
}
}
else {
$form_state
->setErrorByName('redirect_path', $this
->t('Please enter a valid redirect path.'));
}
}
}
/**
*
* @param array $form
* @param FormStateInterface $form_state
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$url = trim($form_state
->getValue([
'loginmenu_url',
]));
$replacements = [
'!',
'*',
"(",
")",
";",
":",
"@",
"+",
"\$",
",",
"[",
"]",
" ",
];
$url = str_replace($replacements, '-', $url);
$username_label = trim($form_state
->getValue([
'username_label',
]));
$password_label = trim($form_state
->getValue([
'password_label',
]));
$submit_text = trim($form_state
->getValue([
'submit_text',
]));
$page_title = trim($form_state
->getValue([
'page_title',
]));
$redirect_path = trim($form_state
->getValue([
'redirect_path',
]));
$role_mismatch_error_text = trim($form_state
->getValue([
'role_mismatch_error_text',
]));
$invalid_credentials_error_text = trim($form_state
->getValue([
'invalid_credentials_error_text',
]));
$parent_class = $form_state
->getValue([
'parent_class',
]);
$roles = $form_state
->getValue([
'roles',
]);
$roles = implode(',', $roles);
$add_login_url = $this->connection
->insert('role_login_page_settings')
->fields([
"url" => $url,
"username_label" => $username_label,
"password_label" => $password_label,
"submit_text" => $submit_text,
"page_title" => $page_title,
"parent_class" => $parent_class,
"redirect_path" => $redirect_path,
"role_mismatch_error_text" => $role_mismatch_error_text,
"invalid_credentials_error_text" => $invalid_credentials_error_text,
"roles" => $roles,
])
->execute();
if ($add_login_url) {
_role_login_page_settings_cache_clear($url, 'add');
}
}
}
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 | |
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:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
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. | |
RoleLoginPageSettings:: |
protected | property | @var | |
RoleLoginPageSettings:: |
public | function |
Overrides FormInterface:: |
|
RoleLoginPageSettings:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
RoleLoginPageSettings:: |
public | function |
Overrides FormInterface:: |
|
RoleLoginPageSettings:: |
public | function |
@global type $base_url Overrides FormBase:: |
|
RoleLoginPageSettings:: |
public | function | RoleLoginPageSettings constructor. | |
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. |