2

I am attempting to add a conditional stylesheet to my user registration form which has an alias called "signup"

This is the code I am using, however this does not seem to work. I have tried loads of other different ways but I cannot seem to find any clear documentation on how to do this.

Any ideas on this? The simpoer the better, my code seems to be quite complex for something so simple.

Thanks

function mytheme_preprocess_page(&$variables, $hook) {
$matches = "signup";
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $matches);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $matches);
}
if ($page_match) {
drupal_add_css(drupal_get_path('theme', 'mytheme') . '/style.css');
}

Also tried this to no avail

function mytheme_form_alter(&$form, &$form_state, $form_id) {
global $user, $theme;
switch ($form_id) {
    case 'user/register':
      $form['#attached']['css'] = array(
        drupal_get_path('theme', 'mytheme') . '/css/new.css',
      );
    break;
 }
  • Your switch is on $form_id but user registration form's form ID isn't user/register. – AyeshK Sep 8 '13 at 21:08
2

You're right, that is overly complex.

First of all, make sure your mytheme_preprocess_page() function is actually getting called by adding print & then exit statements. If it's not getting called, clear your theme registry by clearing the site's cache.

Once you're sure the preprocessing function is getting called, this will do the trick for you:

if (preg_match('#user/register#', $_GET['q'])) {
  drupal_add_css(drupal_get_path('theme', 'mytheme') . '/style.css');
}

$_GET['q'] always contains the 'internal' Drupal path, not the alias. The internal path for the normal registration form is user/register, so that's what you want to check for.

  • Thank you - that works (kind of). Perhaps you can now point me in the right direction? It adds a reference to my CSS file in jquery.extend(drupal.settings) "sites\/all\/themes\/mytheme\/css\/font-awesome-min.css":1 but this does not allow me to use the stylesheet as when I reference classes in it, they dont load. The CSS file does not show as an import URl or link in <head> – Jeremy Sep 8 '13 at 9:34
  • 1
    @Jeremy please don't ask follow up questions in the comments; if you have more questions, please ask them separately. Thanks – Clive Sep 8 '13 at 10:59
0

The simplest and best practice to add a css to user registration form is implementing hook_form_FORM_ID_alter():

function YOURTHEME_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  drupal_add_css(drupal_get_path('theme', 'YOURTHEME') . '/YOURCSS.css');
}

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.