0

I am trying to figure out a way to override the entire /user page in D7. There is a lot of articles out there that goes about altering the form, but I want to be able to rework the entire page (forms included) to get them into a nice bootstrap layout.

A lot of the information on the web is talking about page--user--login.tpl.php, but this does not work at all.

When using page--user.tpl.php, it seems to render my template file, but I cannot re-render the form.

When using

<?php
  $form = drupal_get_form('user_login');
  print drupal_render($form);
?>
My HTML goes here
<?php
  // Render any remaining elements, such as hidden inputs (token, form_id, etc).
  print drupal_render_children($form);
?>

I keep getting an infinite redirect loop. My best guess is because of a problem with the form_id that does not match.

Ps: I am using a bootstrap subtheme.

All information about this would be greatly appreciated.

0

You should copy your theme's page.tpl.php to page--user.tpl.php as you already have to create the template that will be used for the page.

Following that, in your template.php create implement hook_form_FORM_ID_alter() using either user_login or user_register as the form ID depending on which part of the page you're wishing to alter. If you either add a dpm() or an xdebug breakpoint in here and refresh the page, you should see the form structure and the available fields. in this field, you can then alter the form elements' #attributes, #prefix, and #suffix fields to add the required Bootstrap classes.

I.e. (not a user register field, just an example)

$form['my_element'] = [
  '#type' => 'textfield',
  '#title' => t('My textfield'),
  '#prefix' => '<div class="form-group">,
  '#suffix' => '</div>',
  '#attributes' => [
    'class' => [
      'form-control',
    ]
  ],
]

One last thing that might be helpful; there was a change in Drupal 7.33 (I think) that added a theme_debug value to the global $conf. If you add $conf['theme_debug'] = TRUE; to your settings.php, then any template the is used will be shown in the dev tools as a comment just before it is loaded - this makes is super easy to work out which template is being called, as well as listing any possible alternatives.

  • Thanks you very much Chapabu! I totally missed the point of page.tpl.php. I was overly focussed on finding a user-login.tpl.php file or something like this. The form_alter() solution, i am well aware of. I have marked you anwser over the other one below because the page.tpl.php was actually the point i was missing! – Guardian Dec 24 '15 at 13:10
1

In template.php, you could add an alter form to add classes, a wrapper, and I think also be able to add/edit fields in the form. Would that be of any help?

Example:

function THEME_NAME_form_user_login_alter(&$form, &$form_state) {
    $form['#attributes']['class'][] = 'custom-class';
}

in template.php would add the class custom-class around the login form, which you can then use to style the form.

As for using templates, I think they might have to be added in the correct location as well, e.g. I have recently built a site using custom templates with the user pages templates in /sites/all/theme-name/templates/user/. This also allows you to use custom markup. For example:

<div class="custom-class--wrapper">
    <?php print drupal_render_children($form) ?>
</div>

which is used in the file user-register-form.tpl.php.

Also, have you considered using panelizer to customise pages, that would be a lot easier than using template.php or custom templates. And if a change doesn't seem to take effect, you should try and clear the drupal cache.

  • This doesn't really answer the question; to write a good answer you should describe how the asker can actually go about performing the task they've required, not make an exceptionally broad statement. – Chapabu Dec 23 '15 at 21:41
  • @Chapabu Sorry about that. Hopefully this is better – dhruveonmars Dec 23 '15 at 23:03

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.