1

So, I'm maintaining a Drupal 7 site for which we recently switched the users over to SSO through a third-party OAuth provider. The implementation isn't fancy, but added a login link to what I assume is the standard login page with a tabs for "registration, login, and password reset."

What I would like to do is remove the menu options altogether for regular users, keeping just the OAuth login link and some additional text to inform users of new registration procedures. For admins, I would like to preserve the regular name and password based login options.

I am aware of the Tab Tamer module, but I'd like a little more granular control. I believe I need to approach this with hook targeting the login block, but I haven't found any examples on how to control what HTML in the block itself.

So, how to do modify the login block's HTML to remove the extra tabs for regular users, but preserve it for admin users?

1 Answer 1

1

I don't think you've thought this through. The standard login page (/user/login) only shows the login form when a user is not yet logged in. So if you want the ability for admin users to see the usual options on that page, you will need to show them to all users -- since you don't know whether or not the user is an admin before they log in. If a user is already logged in (whether through OAuth or standard login) they will not be prompted to log in again.

If, instead, you want to modify the user login options for all users, this can be accomplshed via hook_form_alter() in a custom module, replacing MODULENAME with the name of your module in the hook implementation:

function MODULENAME_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    // Your code to modify the form goes here
    $form['some_text'] = [
      '#markup' => '<p>Some text to inform users of new registration procedures</p>',
    ];
  }
}

EDIT: If you have never made a Drupal module before, don't be intimidated! The type of thing you're trying to do can be accomplished with a very small module.

Keep in mind that the Drupal Form API is basically just a bunch of PHP arrays.

To add markup to a form array, use the #markup form element as I've shown im my example above.

To remove an element from a form array, you can unset() that value before the array is rendered. For example:

<?php 
unset($form['buttons']['preview']);
?>

I'm not sure what form elements you'll want to remove, but this technique should help.

This stuff is well documented on Drupal.org and all over this forum.

7
  • Hi, you're right, I wasn't thinking about that. Separately from my brain fart, I'm looking for whatever will let me hook in and modify the links that are being loaded into the User Login Block and remove/hide the register and password re-set options. From what I read, Tab Tamer will mostly do this, but I was hoping for some clarity on how to do so myself.
    – nizz0k
    Commented Oct 13, 2019 at 16:24
  • Check out the links at the bottom of my answer! Commented Oct 13, 2019 at 16:25
  • 1
    I haven’t had the time to sit with this, but your answer tracks with my prior research. I will review tonight and accept ASAP, many thanks for the update
    – nizz0k
    Commented Oct 14, 2019 at 11:07
  • 1
    1.) Just the user login block and not the page: if ($form_id == 'user_login_block'). Commented Oct 14, 2019 at 19:09
  • 1
    2.) To examine the structure of a form array, try dpm($form);. Google Drupal 7 dpm for info. You'll need to enable Devel module to use dpm, which is not a great idea in a production site, but fine in your local. You can also use var_dump(); if that seems easier. Commented Oct 14, 2019 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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