You are here

styles.module in Styles 6

Same filename and directory in other branches
  1. 6.2 styles.module
  2. 7.2 styles.module
  3. 7 styles.module

Bundles similar display formatters together.

File

styles.module
View source
<?php

/**
 *  @file
 *  Bundles similar display formatters together.
 */

// A registry of variable_get defaults.
include_once 'includes/styles.variables.inc';

/**
 *  Implements CCK's hook_field_formatter_info().
 */
function styles_field_formatter_info() {
  $info = array();
  $presets = styles_presets();
  foreach ($presets as $field_type => $styles) {
    foreach ($styles as $style_name => $style) {
      $info['styles_' . $field_type . '_' . $style_name] = array(
        'label' => t('@field Style: @style', array(
          '@field' => ucfirst($field_type),
          '@style' => $style_name,
        )),
        'field types' => array(
          $field_type,
        ),
      );
    }
  }
  return $info;
}

/**
 *  Implements hook_theme().
 */
function styles_theme($existing, $type, $theme, $path) {
  $themes = array(
    'styles' => array(
      'arguments' => array(
        'field_type' => NULL,
        'style_name' => NULL,
        'object' => NULL,
      ),
      'template' => 'styles',
      'path' => $path . '/themes',
      'file' => 'styles.theme.inc',
    ),
    'field_formatter_styles' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'path' => $path . '/themes',
      'file' => 'styles.theme.inc',
    ),
  );
  foreach (styles_presets() as $field_name => $presets) {
    foreach ($presets as $preset_name => $preset) {
      $themes['styles_formatter_styles_' . $field_name . '_' . $preset_name] = array(
        'arguments' => array(
          'element' => NULL,
        ),
        'function' => 'theme_field_formatter_styles',
        'path' => $path . '/themes',
        'file' => 'styles.theme.inc',
      );
    }
  }
  return $themes;
}

/**
 *  Get an array of all defined style containers.
 */
function styles_containers() {
  $containers =& _styles_drupal_static(__FUNCTION__);

  // Grab from cache or build the array.
  if (!isset($containers)) {
    if ($cache = cache_get('styles_containers', 'cache')) {
      $containers = $cache->data;
    }
    else {
      module_load_all_includes('inc', 'styles');
      foreach (module_implements('styles_containers') as $module) {
        $module_containers = module_invoke($module, 'styles_containers');
        foreach ($module_containers as $field_type => $field_container) {
          foreach ($field_container['containers'] as $container_name => $container) {
            $container['name'] = $container_name;
            $container['module'] = $module;
            $container['available styles'] = styles_containers_available_styles($field_type, $container_name);
            $containers[$field_type]['containers'][$container_name] = $container;
          }
        }
      }
      drupal_alter('styles_containers', $containers);
      foreach ($containers as $field_type => $container) {

        // Sort the containers by weight.
        uasort($containers[$field_type]['containers'], 'styles_uasort');
      }
      cache_set('styles_containers', $containers);
    }
  }
  return $containers;
}

/**
 *  Sort the containers array by weight and label.
 *  Use like usort($containers, 'styles_usort');
 */
function styles_uasort($a, $b) {
  $retval = $a['weight'] - $b['weight'];
  if (!$retval) {
    return strnatcasecmp($a['label'], $b['label']);
  }
  return $retval;
}

/**
 *  Get an array of all available styles.
 */
function styles_styles() {
  $styles =& _styles_drupal_static(__FUNCTION__);

  // Grab from cache or build the array.
  if (!isset($styles)) {
    if ($cache = cache_get('styles_styles', 'cache')) {
      $styles = $cache->data;
    }
    else {
      $styles = array();
      module_load_all_includes('inc', 'styles');
      foreach (module_implements('styles_styles') as $module) {
        $module_styles = module_invoke($module, 'styles_styles');
        foreach ($module_styles as $field_name => $containers) {
          if (!isset($styles[$field_name])) {
            $styles[$field_name] = array(
              'containers' => array(),
            );
          }
          foreach ($containers['containers'] as $container_name => $container) {
            if (!isset($styles[$field_name]['containers'][$container_name])) {
              $styles[$field_name]['containers'][$container_name] = array();
            }
            foreach ($container['styles'] as $style_name => $style) {
              $style['name'] = $style_name;
              $style['module'] = $module;
              $style['storage'] = STYLES_STORAGE_DEFAULT;
              $styles[$field_name]['containers'][$container_name]['styles'][$style_name] = $style;
            }
          }
        }
      }
      drupal_alter('styles_styles', $styles);
      cache_set('styles_styles', $styles);
    }
  }
  return $styles;
}

/**
 *  Grab all styles available to a specific field type/container.
 *  @param $field_type
 *    The field type name.
 *  @param $container_name
 *    The name of the container to fetch for that field type.
 *  @param $style_name
 *    (optional) The name of the specific style for the field/container to
 *    return. If not specified, then return all styles for the combination.
 */
function styles_containers_available_styles($field_type, $container_name, $style_name = NULL) {
  $styles = styles_styles();
  if (isset($style_name)) {
    return isset($styles[$field_type]['containers'][$container_name]['styles'][$style_name]) ? $styles[$field_type]['containers'][$container_name]['styles'][$style_name] : FALSE;
  }
  return isset($styles[$field_type]) ? isset($styles[$field_type]['containers'][$container_name]['styles']) ? $styles[$field_type]['containers'][$container_name]['styles'] : FALSE : FALSE;
}

/**
 *  Return an array of all style presets.
 */
function styles_presets() {
  $presets =& _styles_drupal_static(__FUNCTION__);

  // First check the cache.
  if (!isset($presets)) {

    // Build the default presets.
    $presets = array();
    module_load_all_includes('inc', 'styles');
    foreach (module_implements('styles_presets') as $module) {
      $styles_containers = module_invoke($module, 'styles_presets');
      foreach ($styles_containers as $field_type => $preset) {
        foreach ($preset as $preset_name => $container) {
          foreach ($container as $container_name => $styles) {
            foreach ($styles as $style_name) {
              if ($style = styles_containers_available_styles($field_type, $container_name, $style_name)) {
                if (!isset($presets[$field_type])) {
                  $presets[$field_type] = array();
                }
                if (!isset($presets[$field_type][$preset_name])) {
                  $presets[$field_type][$preset_name] = array();
                }
                if (!isset($presets[$field_type][$preset_name][$container_name])) {
                  $presets[$field_type][$preset_name][$container_name] = array();
                }
                $presets[$field_type][$preset_name][$container_name][$style_name] = $style;
              }
            }
          }
        }
      }
    }

    // @TODO
    // Add user-defined presets and overrides.
    // Save the cache.
  }
  return $presets;
}

/**
 * Builds a registry of Style classes.
 *
 * Each module supporting a Style will need to implement
 * hook_styles_register, which will need to return an associated array keyed by
 * the style class name, with an array containing the following key => value
 * pairs:
 *  'field_types' => An array of field types to apply this style to.
 * The following key => value pairs are optional, which will otherwise be
 * automatically derived:
 *  'name' => The human-readable name of the style.
 *  'description' => A description of the style.
 *  'path' => The path where the class file resides.
 *  'file' => The file containing the class definition.
 *  'module' => The module defining the class.
 * The following key => value pair will be automatically set to the association
 * and cannot be overridden:
 *   'class_name' => The actual name of the class.
 *
 * @param string $style
 *  (Optional) The style of the specific class registration to return.
 * @param boolean $reset
 *  (Optional) If TRUE, then reset the registration.
 * @return array
 *  If $style is specified, then return only the specified class definition, or
 *  NULL if there is no such registered class. Otherwise, return the entire
 *  class definition registry.
 */
function styles_get_registered_classes($style = NULL, $reset = FALSE) {
  $registered_classes =& _styles_drupal_static(__FUNCTION__);
  if ($reset || !isset($registered_classes)) {
    $registered_classes = array();

    // Build our media object class registry.
    foreach (module_implements('styles_register') as $module) {
      foreach (module_invoke($module, 'styles_register') as $style => $class) {
        $registered_classes[$style] = is_array($class) ? $class : array();
        $registered_classes[$style]['class_name'] = $style;
        if (!isset($registered_classes[$style]['name'])) {
          $registered_classes[$style]['name'] = t($style);
        }
        if (!isset($registered_classes[$style]['description'])) {
          $registered_classes[$style]['description'] = t('Class definition for @style.', array(
            '@style' => $style,
          ));
        }
        if (!isset($registered_classes[$style]['path'])) {
          $registered_classes[$style]['path'] = drupal_get_path('module', $module);
        }
        if (!isset($registered_classes[$style]['file'])) {
          $registered_classes[$style]['file'] = $style . '.inc';
        }
        if (!isset($registered_classes[$style]['module'])) {
          $registered_classes[$style]['module'] = $module;
        }
      }
    }
  }
  if (isset($style)) {
    return $registered_classes[$style];
  }
  return $registered_classes;
}

/**
 * Implementation of hook_init().
 */
function styles_init() {

  // Load all registered class definitions.
  styles_get_registered_classes();

  // Ensure the proper files are loaded when a new Styles object is initiated.
  spl_autoload_register('styles_autoload');
}

/**
 * Autoload the Styles object classes when needed.
 *
 * @param string $class_name
 *  The name of the registered class definition.
 */
function styles_autoload($class_name) {
  if ($class_name == 'StylesDefault') {
    module_load_include('inc', 'styles', 'includes/Styles');
  }
  else {
    if ($class = styles_get_styles_class_by_class_name($class_name)) {
      include_once $class['path'] . '/' . $class['file'];
    }
  }
}

/**
 * Return the registered Styles class definition specified by name.
 *
 * @param string $class_name
 *  (Optional) The name of the class definition to return. If NULL, then return
 *  all class definitions.
 * @param boolean $reset
 *  (Optional) If TRUE, then reset the static array of class definitions.
 * @return mixed
 *  Either the specified Styles class definition, or all defined definitions
 *  if $class_name is NULL.
 */
function styles_get_styles_class_by_class_name($class_name = NULL, $reset = FALSE) {
  $classes =& _styles_drupal_static(__FUNCTION__);
  if (!isset($classes) || $reset) {
    $classes = array();
    $registered_classes = styles_get_registered_classes();
    foreach ($registered_classes as $scheme => $class) {
      $classes[$class['class_name']] = $class;
    }
  }
  if (isset($class_name)) {
    return $classes[$class_name];
  }
  return $classes;
}

Functions

Namesort descending Description
styles_autoload Autoload the Styles object classes when needed.
styles_containers Get an array of all defined style containers.
styles_containers_available_styles Grab all styles available to a specific field type/container.
styles_field_formatter_info Implements CCK's hook_field_formatter_info().
styles_get_registered_classes Builds a registry of Style classes.
styles_get_styles_class_by_class_name Return the registered Styles class definition specified by name.
styles_init Implementation of hook_init().
styles_presets Return an array of all style presets.
styles_styles Get an array of all available styles.
styles_theme Implements hook_theme().
styles_uasort Sort the containers array by weight and label. Use like usort($containers, 'styles_usort');