Hi,

I've just installed this module and have noticed it doesn't work at all with javascript disabled.

Are there any plans to rectify this issue?

Cheers,
Dave

Comments

alanburke’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Active » Postponed (maintainer needs more info)

No plans to change this - Any suggestions on how it might work?

mattconcentric’s picture

I've done this kind of thing before by adding a menu callback that sets a cookie (and redirects to referer). The links in the switcher would just need to point to this, passing a style to switch to.

I'm not set up for submitting patches, but you are welcome to have a look at the code if you want.

alanburke’s picture

Sounds good
just post the code - I'll have a look.
It sounds like the menu callbacks should be there by default, but then we could use js to to hijack the links

mattconcentric’s picture

Yes, something like that - the links would work as normal links to the callback, unless Js is enabled, in which case an event.preventDefault() somewhere along the line would stop the navigation.

So, something like this then. I expect this violates various style guidelines, but it works OK. Anything that changes state should strictly involve a POST, but that would mean too much of a change to the existing block output.

Helper function _styleswitcher_title_safe() just generates a slug from the title so it's safe to use in a URL:

  function _styleswitcher_title_safe($title) {    
    return strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $title));
  }

The menu would be something like this:

function styleswitcher_menu() {
  
  $items = array();
  
  $items['styleswitcher/set'] = array(
    'title' => 'styleswitcher set style',
    'description' => 'Set style with GET/REDIRECT for non-JS users',
    'page callback' => 'styleswitcher_set_style',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );  
 
  return $items;
}

And the switch itself:

function styleswitcher_set_style($style) {
  global $theme_info;
  if (!empty($theme_info->info['styleswitcher'])) {    
    foreach ($theme_info->info['styleswitcher']['css'] as $title => $file) {
      if($style == _styleswitcher_title_safe($title)) {
        setcookie('styleSwitcher', $title, 0, '/');           
        drupal_goto($_SERVER["HTTP_REFERER"]);
      } 
    }
  }  
}

This just loops back through the styles from the theme to find a matching slug, then sets the cookie to the title that is used elsewhere. Really, it could do with more checks to ensure you aren't trying to redirect to somewhere invalid, and maybe default to drupal_goto(front) if it can't match the style.

We can then change the block display code to something like this:

  if (!empty($theme_info->info['styleswitcher'])) {
      $links = array();
      // Get the style cookie
      $cookie_style = isset($_COOKIE['styleSwitcher']) ? $_COOKIE['styleSwitcher'] : '';
      foreach ($theme_info->info['styleswitcher']['css'] as $title => $file) {
        $theme_directory = path_to_theme();
        $filepath = $theme_directory . '/' . $file;
        
        // Does this style match the cookie style?
        $is_active = $title == $cookie_style;        
        
        // Generate link - adding rel=nofollow, and using the slug
        $links[] = l($title, 'styleswitcher/set/'._styleswitcher_title_safe($title), array('attributes' => array('class' => array('style-switcher'), 'data-rel' => $title, 'rel' => 'nofollow')));          
        
        drupal_add_html_head_link(array(
          // If it's active, make it a normal stylesheet, otherwise an alternate
          'rel' => ($is_active ? 'stylesheet' : 'alternate stylesheet'),
          'type' => 'text/css',
          'title' => $title,
          // Force the URL to be absolute, for consistency with other <link> tags
          // output by Drupal.
          'href' => url($filepath, array('absolute' => TRUE)),
        ));
      }
      // etc...     
  

Writing out the active stylesheet like this could cause problems depending on the ordering, as it may appear before the standard stylesheets. I tend to make rules !important in accessibility stylesheets though, so that's not so much of an issue for me.

I've not really thought about any potential security issues or tested this exhaustively, so include at your own risk!

pingwin4eg’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new2.37 KB

Hello!

I'm working on 2nd branch of the module which will introduce new features and bug fixes.
So here is the patch that implements JavaScript disabled functionality of style switching. But it can be only applied after another patch (which covers a lot of fixes for proper workflow) - #1966982: Load active (selected) CSS on page init. / Use another technique for style switching.

Hope to hear your feedback on it.

  • Commit af10012 on 7.x-2.x by pingwin4eg:
    Issue #522178 by pingwin4eg: Fixed JS-less switching.
    
pingwin4eg’s picture

Issue summary: View changes
Status: Needs review » Fixed

Committed to 7.x-2.x. Please update.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.