I tried to change the text and position of the error message.
My Drupal version is 8.8.4

In the Drupal standard,
An error message is displayed above the username.
How can I change the position of this message? 

Now I want to display below the submit button. and If possible, it is best to control them in the twig template.

I also want to change this text, and although I can change it, But it looks like there is a problem.
I referred to the links these.

https://www.drupal.org/forum/support/post-installation/2013-10-21/customize-login-error-message
https://drupal.stackexchange.com/questions/89705/customize-login-error-message

and may code is below 
hook_form_alter:replacement(?) final validate

function THEME_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if ($form_id == 'user_login_form' ) {
   
    $final_validate_index = array_search('user_login_final_validate', $form['#validate']);
    if ($final_validate_index >= 0) {
       $form['#validate'][$final_validate_index] = 'THEME_user_login_form_validate';
    }
}
function THEME_user_login_form_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {

  $flood  = \Drupal::flood();
  $form_state->clearErrors();

  if (!$form_state->get('uid')) {

    // Always register an IP-based failed login event.
    $flood->register('user.failed_login_ip', \Drupal::config('ip_window'));

    // Register a per-user failed login event.
    if ($flood_control_user_identifier = $form_state->get('flood_control_user_identifier')) {

       $flood->register('user.failed_login_user',
                  \Drupal::config('user_window'), $flood_control_user_identifier);
    }

    if ($flood_control_triggered = $form_state->get('flood_control_triggered')) {
       if ($flood_control_triggered == 'user') {

           $form_state->setErrorByName('message',
                \Drupal::translation()->formatPlural(\Drupal::config('user_limit'),
                  'Sorry, there has been more than one failed login attempt for this account.' .
                  ' It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.',
                  '!!Sorry, there have been more than :count failed login attempts for this account.' .
                  'It is temporarily blocked. Try again later or <a href=":url">request a new password</a>.',
                  )
                );
        } else {
           // We did not find a uid, so the limit is IP-based.
           $form_state->setErrorByName('message',
              t('Sorry, too many failed login attempts from your IP address. This IP address is temporarily blocked.'.
                ' Try again later or <a href=":url">request a new password</a>.')
              );
            }
        } else {
            $user_input = $form_state->getUserInput();
            $query      = isset($user_input['name']) ? array('name' => $user_input['name'],) : array();
Change here ->  $form_state->setErrorByName('message', t('hogehoge'));

            if (isset($user_input['name'])) {
                \Drupal::logger('user')->notice('Login attempt failed for %user.', array('%user' => $user_input['name']));
            } else {
                \Drupal::logger('user')->notice('Login attempt failed from ip.');
            }
        }
    } elseif ($flood_control_user_identifier = $form_state->get('flood_control_user_identifier')) {
        // Clear past failures for this user so as not to block a user who might
        // log in and out more than once in an hour.
        $flood->clear('user.failed_login_user', $flood_control_user_identifier);
    }
}

I thought I had replaced finalValidation. but the original finalValidation also seemed to be working.
Because the error message always displayed both the message I changed and the original.

Please teach me how is the right way as a drupal ?

Comments

VM’s picture

you can change the text in settings.php using a string override or you can use a site translation to change the text (heavy-handed if you aren't changing a significant amount of text.).

dqtuan200’s picture

Could you please tell me more details, what doing in settings.php (you mean settinng.php in sites\default ?) . Can you change the message POSITION to anywhere you want. My problem now is when I use drupal_get_messages() in theme_preprocess hook, the message not clear => the message text session not empty => so that it shows always without clear.

Thanks much

VM’s picture

The response is about how to change the text of the message not the position of it. To change the positon of the messages in Drupal 8, you move the block.

To change the text of a message search settings.php for the words 'string overrides'. It is in that section that you will need to perform the changes.

dqtuan200’s picture

Thanks for your reply, but could you tell me more detail about change position of message block, solve by admin dashboard or by programmatically (I'm finding solution for this). I have create a new topic, if you cann't talk here, please goto this topic https://www.drupal.org/forum/support/theme-development/2020-04-09/how-to-change-the-position-of-status-message-for-a

Thanks very much

VM’s picture

in your blocks UI