2

Is there a way to hide and relocate the Reset password form from standard location? The login form needs to be shown as is.

I wasn't able to do what the answer on How to print the local task bar different places depending on the base route? says to do.


I found the No Request New Password module. However, I'd like to still access this form on a special link.

  • There is a block for the login form, but there isn't any block for the "Reset password" form. Where exactly you would put that form? – kiamlaluno Feb 27 at 8:16
  • The reset form would go on an obscure page like /hidden/user/reset this way few special role users can still use it when needed. Most users on the site are being logged via sso, so they should not be resting password in drupal. – amit Feb 27 at 8:31
0

How do I print the local task bar in different places depending on the base route? is about changing where the tabs for local tasks are rendered in the page. It's different from what you want to achieve.

The module you found (No Request New Password) is for removing the Request new password link from block and user page. This is probably part of the task you want to achieve, but you cannot use that module because it has not been completely ported to Drupal 8, and it would not work. If you look at the content of the noreqnewpass.module file, you will notice it is using functions that don't exist anymore in Drupal 8, as in the following hook implementation.

function noreqnewpass_form_alter(&$form, $form_state, $form_id) {
  if (($form_id == 'user_login_block' || $form_id == 'user_login') && variable_get('noreqnewpass_disabled', true)) {
    $key = array_search('user_login_final_validate', $form['#validate']);
    $form['#validate'][$key] = 'noreqnewpass_user_login_final_validate';
  }
}

The task you want to achieve requires to:

  • Remove the Reset your password local task from the /user/login page
  • Change the path associated with the user.pass route
  • (Eventually) remove the Reset your password link that appears in the login block

The fist step requires a module implementing the following hook.

function custom_menu_local_tasks_alter(&$data, $route_name) {
  // Remove the "Reset your password" tab only from the login page.
  if ($route_name == 'user.login') {
    unset($data['tabs']['0']['user.pass']);
  }
}

The second step requires altering a route another module defines. There isn't any hook to do the task, which requires writing code for an event subscriber. Fortunately, Drupal 8 has a class that can be extended to do the task.

class CustomSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    // Change path '/user/password' to '/hidden/user/reset'.
    if ($route = $collection->get('user.pass')) {
      $route->setPath('/hidden/user/reset');
    }
  }

}

An event subscriber is a service using the event_subscriber tag. This means it needs to be defined in the .services.yml file of the module.

For the last step, it's sufficient to alter the login block.

function custom_block_view_user_login_block_alter(array &$build, BlockPluginInterface $block) {
 unset($build['user_links']['#items']['request_password']);
}
  • thanks for the detailed response, we are now using this method. – amit Mar 18 at 23:22
0

Yes, the module removes the local task by denying access to the route. To only hide the tab unset it in a hook_menu_local_tasks_alter():

/**
 * Implements hook_menu_local_tasks_alter().
 */
function mymodule_menu_local_tasks_alter(&$data, $route_name) {
  if ($route_name == 'user.login') {
    unset($data['tabs']['0']['user.pass']);
  }
}

Then you can still access /user/password. If you want to change this path as well you have to alter the route, see https://www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.