Loading config/schema/enforce_user_fields.schema.yml +3 −0 Changes for config/schema/enforce_user_fields.schema.yml: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,9 @@ enforce_user_fields.settings: message: type: label label: 'Message to user that need to fill required fields before continue' whitelist: type: string label: 'List of paths for ignore redirect' langcode: type: string label: 'Language code' enforce_user_fields.services.yml +1 −1 Changes for enforce_user_fields.services.yml: 1 added line, 1 removed line. Original line number Diff line number Diff line services: enforce_user_fields.event_subscriber: class: Drupal\enforce_user_fields\EventSubscriber\EnforceUserFieldsSubscriber arguments: [ '@current_user', '@current_route_match', '@request_stack', '@messenger', '@config.factory' ] arguments: [ '@current_user', '@current_route_match', '@path.matcher', '@request_stack', '@messenger', '@config.factory' ] tags: - { name: event_subscriber } src/EventSubscriber/EnforceUserFieldsSubscriber.php +22 −1 Changes for src/EventSubscriber/EnforceUserFieldsSubscriber.php: 22 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ namespace Drupal\enforce_user_fields\EventSubscriber; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; Loading Loading @@ -32,6 +33,13 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { */ protected $routeMatch; /** * The path matcher. * * @var \Drupal\Core\Path\PathMatcherInterface */ protected $pathMatcher; /** * The request stack. * Loading Loading @@ -60,6 +68,8 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { * The current user. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher * The path matcher service. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param \Drupal\Core\Messenger\MessengerInterface $messenger Loading @@ -67,12 +77,13 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. */ public function __construct(AccountProxyInterface $current_user, RouteMatchInterface $route_match, RequestStack $request_stack, MessengerInterface $messenger, ConfigFactoryInterface $config_factory) { public function __construct(AccountProxyInterface $current_user, RouteMatchInterface $route_match, PathMatcherInterface $path_matcher, RequestStack $request_stack, MessengerInterface $messenger, ConfigFactoryInterface $config_factory) { $this->currentUser = $current_user; $this->routeMatch = $route_match; $this->requestStack = $request_stack; $this->messenger = $messenger; $this->globalConfig = $config_factory->get('enforce_user_fields.settings'); $this->pathMatcher = $path_matcher; } /** Loading @@ -93,6 +104,16 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { if ($is_ajax) { return; } // Ignore paths specified in the whitelist. if ($pages = mb_strtolower($this->globalConfig->get('whitelist'))) { $path = $this->routeMatch->getRouteObject()->getPath(); // Do not trim a trailing slash if that is the complete path. $path_to_match = $path === '/' ? $path : rtrim($path, '/'); if ($this->pathMatcher->matchPath(mb_strtolower($path_to_match), $pages)) { return; } } if (!empty($_SESSION['enforce_user_fields'])) { $this->messenger->addMessage($this->globalConfig->get('message'), MessengerInterface::TYPE_ERROR); $route_params = [ Loading src/Form/SettingsForm.php +21 −8 Changes for src/Form/SettingsForm.php: 21 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -28,29 +28,42 @@ class SettingsForm extends ConfigFormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { /** @var \Drupal\Core\Config\Config $config */ $config = $this->config('enforce_user_fields.settings'); $form['message'] = [ '#type' => 'textfield', '#title' => $this->t('Message'), '#description' => $this->t('Message to user when need to fill required fields before proceed.'), '#required' => TRUE, '#default_value' => $this->config('enforce_user_fields.settings')->get('message'), '#default_value' => $config->get('message'), ]; $form['whitelist'] = [ '#type' => 'textarea', '#title' => $this->t('Pages to not enforce'), '#default_value' => $config->get('whitelist'), '#description' => $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [ '%user-wildcard' => '/user/*', '%front' => '<front>', ]), ]; return parent::buildForm($form, $form_state); } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Make sure to store the 'pages to not enforce' with the leading slash. $input = explode(PHP_EOL, $form_state->getValue('whitelist')); $pages = ''; foreach ($input as $page) { if (!empty($page)) { $pages .= '/' . ltrim($page, '/') . "\n"; } } $this->config('enforce_user_fields.settings') ->set('message', $form_state->getValue('message')) ->set('whitelist', $pages) ->save(); parent::submitForm($form, $form_state); } Loading Loading
config/schema/enforce_user_fields.schema.yml +3 −0 Changes for config/schema/enforce_user_fields.schema.yml: 3 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,9 @@ enforce_user_fields.settings: message: type: label label: 'Message to user that need to fill required fields before continue' whitelist: type: string label: 'List of paths for ignore redirect' langcode: type: string label: 'Language code'
enforce_user_fields.services.yml +1 −1 Changes for enforce_user_fields.services.yml: 1 added line, 1 removed line. Original line number Diff line number Diff line services: enforce_user_fields.event_subscriber: class: Drupal\enforce_user_fields\EventSubscriber\EnforceUserFieldsSubscriber arguments: [ '@current_user', '@current_route_match', '@request_stack', '@messenger', '@config.factory' ] arguments: [ '@current_user', '@current_route_match', '@path.matcher', '@request_stack', '@messenger', '@config.factory' ] tags: - { name: event_subscriber }
src/EventSubscriber/EnforceUserFieldsSubscriber.php +22 −1 Changes for src/EventSubscriber/EnforceUserFieldsSubscriber.php: 22 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ namespace Drupal\enforce_user_fields\EventSubscriber; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; Loading Loading @@ -32,6 +33,13 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { */ protected $routeMatch; /** * The path matcher. * * @var \Drupal\Core\Path\PathMatcherInterface */ protected $pathMatcher; /** * The request stack. * Loading Loading @@ -60,6 +68,8 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { * The current user. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher * The path matcher service. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param \Drupal\Core\Messenger\MessengerInterface $messenger Loading @@ -67,12 +77,13 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. */ public function __construct(AccountProxyInterface $current_user, RouteMatchInterface $route_match, RequestStack $request_stack, MessengerInterface $messenger, ConfigFactoryInterface $config_factory) { public function __construct(AccountProxyInterface $current_user, RouteMatchInterface $route_match, PathMatcherInterface $path_matcher, RequestStack $request_stack, MessengerInterface $messenger, ConfigFactoryInterface $config_factory) { $this->currentUser = $current_user; $this->routeMatch = $route_match; $this->requestStack = $request_stack; $this->messenger = $messenger; $this->globalConfig = $config_factory->get('enforce_user_fields.settings'); $this->pathMatcher = $path_matcher; } /** Loading @@ -93,6 +104,16 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { if ($is_ajax) { return; } // Ignore paths specified in the whitelist. if ($pages = mb_strtolower($this->globalConfig->get('whitelist'))) { $path = $this->routeMatch->getRouteObject()->getPath(); // Do not trim a trailing slash if that is the complete path. $path_to_match = $path === '/' ? $path : rtrim($path, '/'); if ($this->pathMatcher->matchPath(mb_strtolower($path_to_match), $pages)) { return; } } if (!empty($_SESSION['enforce_user_fields'])) { $this->messenger->addMessage($this->globalConfig->get('message'), MessengerInterface::TYPE_ERROR); $route_params = [ Loading
src/Form/SettingsForm.php +21 −8 Changes for src/Form/SettingsForm.php: 21 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -28,29 +28,42 @@ class SettingsForm extends ConfigFormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { /** @var \Drupal\Core\Config\Config $config */ $config = $this->config('enforce_user_fields.settings'); $form['message'] = [ '#type' => 'textfield', '#title' => $this->t('Message'), '#description' => $this->t('Message to user when need to fill required fields before proceed.'), '#required' => TRUE, '#default_value' => $this->config('enforce_user_fields.settings')->get('message'), '#default_value' => $config->get('message'), ]; $form['whitelist'] = [ '#type' => 'textarea', '#title' => $this->t('Pages to not enforce'), '#default_value' => $config->get('whitelist'), '#description' => $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [ '%user-wildcard' => '/user/*', '%front' => '<front>', ]), ]; return parent::buildForm($form, $form_state); } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Make sure to store the 'pages to not enforce' with the leading slash. $input = explode(PHP_EOL, $form_state->getValue('whitelist')); $pages = ''; foreach ($input as $page) { if (!empty($page)) { $pages .= '/' . ltrim($page, '/') . "\n"; } } $this->config('enforce_user_fields.settings') ->set('message', $form_state->getValue('message')) ->set('whitelist', $pages) ->save(); parent::submitForm($form, $form_state); } Loading