You are here

function styleswitcher_block_view in Style Switcher 7.2

Same name and namespace in other branches
  1. 6.2 styleswitcher.module \styleswitcher_block_view()
  2. 7 styleswitcher.module \styleswitcher_block_view()

Implements hook_block_view().

File

./styleswitcher.module, line 33
Module's hooks implementations and helper functions.

Code

function styleswitcher_block_view($delta) {
  global $theme;
  switch ($delta) {

    // List of styles a user can switch between.
    case 'styleswitcher':
      $styles = styleswitcher_style_load_multiple($theme, array(
        'status' => TRUE,
      ));

      // Do not display block if there is only one style (no alternatives).
      if (count($styles) > 1) {
        uasort($styles, 'styleswitcher_sort');
        $links = array();
        $destination = drupal_get_destination();
        foreach ($styles as $name => $style) {
          $name_hyphenated = strtr($name, '_', '-');
          $name_parts = explode('/', $name_hyphenated);
          $class = array(
            'style-switcher',
            $name_parts[0] . '-style',
            'style-' . $name_parts[1],
          );
          $links[] = l($style['label'], 'styleswitcher/switch/' . $theme . '/' . $name_hyphenated, array(
            'attributes' => array(
              'class' => $class,
              'data-rel' => $name,
              'rel' => 'nofollow',
            ),
            'query' => $destination,
          ));

          // Make paths absolute for JS.
          if (isset($style['path'])) {
            $styles[$name]['path'] = file_create_url($style['path']);
          }
          else {
            $styles[$name]['path'] = url('styleswitcher/css/' . $theme, array(
              'absolute' => TRUE,
            ));
          }
        }
        $path_to_module = drupal_get_path('module', 'styleswitcher');
        $js_settings = array(
          'styleSwitcher' => array(
            'styles' => $styles,
            'default' => styleswitcher_default_style_key($theme),
            'enableOverlay' => variable_get('styleswitcher_enable_overlay', 1),
            'cookieExpire' => STYLESWITCHER_COOKIE_EXPIRE,
            'theme' => $theme,
          ),
        );
        $attached['css'][] = $path_to_module . '/styleswitcher.overlay.css';
        $attached['js'][] = $path_to_module . '/styleswitcher.js';
        $attached['js'][] = array(
          'type' => 'setting',
          'data' => $js_settings,
        );
        $block['subject'] = t('Style Switcher');
        $block['content'] = array(
          '#theme' => 'item_list',
          '#items' => $links,
          '#attached' => $attached,
        );
        return $block;
      }
      break;
  }
}