2

I am using the following code to switch to a custom theme based on the URL

function job_agent_custom_theme() {
  if (arg(0) == 'vista') {
    return 'vista';
  }
}

This works great and will switch to my "vista" theme when "vista" is in the URL. The problem is that one of my pages has a form that is submitted via AJAX. I created the form using the standard Drupal form API and creating my submit button like so

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Submit'),
  '#attributes' => array('class' => array('submit')),
  '#ajax' => array(
    'callback' => '_display_form',
    'wrapper' => 'ajax_wrapper',      
    'effect' => 'fade',
    'event' => 'click',      
  ),
);

So the problem is that when I submit the AJAX form, the page switches to this weird hybrid of the default theme and my custom theme. It's like they are both loaded. Am I missing something? How do I preserve the custom theme when submitting an AJAX form. It also seems really weird to me that the entire page would refresh and switch to a different theme anyway. I would think that defeats the whole purpose of AJAX. I can understand the contents of my ajax_wrapper changing, but not the entire page. Anyway, does anyone know of a way to preserve my theme when submitting via AJAX?

EDIT: Oh and I forgot a big piece to this puzzle. This ONLY happens with anonymous users. If I am logged in, it works as expected and when submitting the form, the custom theme is preserved, but if I am NOT logged in I get the mish mash theme.

THANKS

1
  • If you are caching anonymous pages, can you either turn that off temporarily, or put a drupal_set_message('Hello world'); on the page somewhere (and then clear caches) to see if some initial caching is somehow causing this (pages with messages aren't cached and this seems a quick way to see if this is an issue)
    – Jimajamma
    Commented Apr 3, 2014 at 15:51

1 Answer 1

1

The form AJAX callback uses /system/ajax as the default. The menu router item for /system/ajax implements a theme callback to try and render the ajax contents in the same theme. For example, perhaps this is a node edit form and you have set node forms to use the same theme as the admin theme. The callback will find that. In your case, it is likely rendering in what it thinks is the default theme. Fortunately, you can specify your own ajax path like so:

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Submit'),
  '#attributes' => array('class' => array('submit')),
  '#ajax' => array(
    'callback' => '_display_form',
    'wrapper' => 'ajax_wrapper',      
    'effect' => 'fade',
    'event' => 'click',   
    'path' => 'vista/system/ajax'   
  ),
);

You'll then have to create a menu item for 'vista/system/ajax', which I guess would be a duplicate of 'system/ajax' without theme callback. Perhaps something like this:

/**
 * implements hook_menu
 */
function mymodule_menu() {
  $items['vista/system/ajax'] = array(
    'title' => 'AHAH callback',
    'page callback' => 'ajax_form_callback',
    'delivery callback' => 'ajax_deliver',
    'access callback' => TRUE,
    // 'theme callback' => 'ajax_base_page_theme',
    'type' => MENU_CALLBACK,
    'file path' => 'includes',
    'file' => 'form.inc',
  );
  return $items;
}

Don't forget to clear cache so the new menu item takes affect.

Also, I wanted to bring your attention to the ThemeKey module which is great way to change themes based on path.

1
  • AWESOME. That totally worked. Thank you so much! If anyone else ends up reading this, I also ended up using the Dev version of a contrib module called ThemeKey and that also did what I needed after tweaking it's AJAX settings in the admin, however I like Evil E's method better because it's cleaner and I don't need to have another module loaded. Thanks again.
    – Erich H.
    Commented Apr 4, 2014 at 15:12

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.