$field_styles) { foreach ($field_styles['styles'] as $style_name => $style) { $info['styles_' . $field_type . '_' . $style_name] = array( 'label' => t('@field style: @style', array('@field' => ucfirst($field_type), '@style' => t($style['label']))), '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_default_styles() as $field_name => $field_styles) { foreach ($field_styles['styles'] as $style_name => $style) { $themes['styles_formatter_styles_'. $field_name .'_'. $style_name] = array( 'arguments' => array('element' => NULL), 'function' => 'theme_field_formatter_styles', 'path' => $path .'/themes', 'file' => 'styles.theme.inc', ); } } return $themes; } /** * Return all available containers for a specific field type. * * Modules implementing this hook should supply an array for each field type * that it supports, with at least two keys: 'containers', containing an * associated array described below, and 'filter callback' => A function to call * with the field object to determine which container to implement. * * Each container under a field type should be an associative array with the * following keys: * 'class' => The class to instantiate when filtering to this container. * 'filter match' => (Optional) A value to pass to the filter callback * to match the field object against this container. * * Example implementation of hook_styles_default_containers(): * array( * 'emvideo' => array( * 'filter callback' => 'my_styles_emvideo_filter', * 'containers' => array( * 'brightcove' => array( * 'class' => 'BrightcoveStyles', * 'filter match' => array('brightcove'), * ), * 'vimeo' => array( * 'class' => 'VimeoStyles', * 'filter match' => array('vimeo'), * ), * 'youtube' => array( * 'class' => 'YouTubeStyles', * 'filter match' => array('youtube', 'my_youtube'), * ), * ), * ), * ); * * @param string $field_type * (Optional) The field type, such as filefield or nodereference. * @param boolean $reset * (Optional) If TRUE, then we reset the cache. */ function styles_default_containers($return_type = NULL, $reset = FALSE) { $styles = &_styles_drupal_static(__FUNCTION__); // Grab from cache or build the array. if (!isset($styles) || $reset) { if (($cache = cache_get('styles_default_containers', 'cache')) && !$reset) { $styles = $cache->data; } else { $styles = array(); styles_module_load_all_includes(); foreach (module_implements('styles_default_containers') as $module) { $module_styles = module_invoke($module, 'styles_default_containers'); foreach ($module_styles as $field_type => $container) { $styles[$field_type] = $container; foreach ($container['containers'] as $style_name => $style) { $style['name'] = $style_name; $style['module'] = $module; $style['storage'] = STYLES_STORAGE_DEFAULT; $styles[$field_type]['containers'][$style_name] = $style; } } } // @TODO: Add user containers next. drupal_alter('styles_default_containers', $styles); cache_set('styles_default_containers', $styles); } } if (isset($return_type)) { return $styles[$return_type]; } return $styles; } /** * Return all available styles for a specific field type. * * Each style under a field type should be an associative array with the * following optional keys: * 'label' => An untranslated human readable title for the style. * 'description' => An untranslated human readable description for the style. * 'default theme' => The theme to call for display if there is no preset * returned when filtering the field. * 'default theme arguments' => Any arguments to pass after the first (of * the field's object itself). * * Example implementation of hook_styles_default_styles(): * array( * 'nodereference' => array( * 'styles' => array( * 'thumbnail' => array( * 'label' => 'Thumbnail', * 'description' => 'Representative thumbnails linking to the content page.', * 'default theme' => 'my_styles_default_thumbnail', * ), * 'small' => array( * 'label' => 'Small', * 'description' => 'Small images linking to the content page.', * 'default theme' => 'my_styles_default_thumbnail', * ), * 'teaser' => array( * 'label' => 'Teaser', * 'description' => 'A short summary of the content.', * 'default theme' => 'node_view', * 'default theme arguments' => array(TRUE), * ), * ), * ), * ); * * This will create those styles, allowing * * @param string $field_type * (Optional) The field type, such as filefield or nodereference. * @param boolean $reset * (Optional) If TRUE, then we reset the cache. */ function styles_default_styles($return_type = NULL, $reset = FALSE) { $styles = &_styles_drupal_static(__FUNCTION__); // Grab from cache or build the array. if (!isset($styles) || $reset) { if (($cache = cache_get('styles_default_styles', 'cache')) && !$reset) { $styles = $cache->data; } else { $styles = array(); styles_module_load_all_includes(); foreach (module_implements('styles_default_styles') as $module) { $module_styles = module_invoke($module, 'styles_default_styles'); foreach ($module_styles as $field_type => $container) { $styles[$field_type] = $container; foreach ($container['styles'] as $style_name => $style) { $style['name'] = $style_name; $style['module'] = $module; $style['storage'] = STYLES_STORAGE_DEFAULT; $styles[$field_type]['styles'][$style_name] = $style; } } } // @TODO: Add user styles next. drupal_alter('styles_default_styles', $styles); cache_set('styles_default_styles', $styles); } } if (isset($return_type)) { return $styles[$return_type]; } return $styles; } /** * Return all available presets for field type containers. * * Each container under a field type should be an associative array with the * following keys: * 'default preset' => The preset to select by default, which may be * overridden later. * 'presets' => An associative array keyed by the preset name, each with * an array of effects to be passed to the implementing class on display. * * Example implementation of hook_styles_default_presets(): * array( * 'filefield' => array( * 'containers' => array( * 'imagefield' => array( * 'default preset' => 'imagecache_thumbnail_linked', * 'presets' => array( * 'imagecache_thumbnail_linked' => array( * 'title' => '[node-title]', * 'alt' => '[file-description]', * 'imagecache_preset' => 'thumbnail', * 'link' => '[node-path]', * ), * 'link_to_file' => array( * 'title' => '[file-title-raw]', * 'link' => '[file-path]', * ), * ), * ), * ), * ); * * @param string $field_type * (Optional) The field type, such as filefield or nodereference. * @param boolean $reset * (Optional) If TRUE, then we reset the cache. */ function styles_default_presets($return_type = NULL, $reset = FALSE) { $styles = &_styles_drupal_static(__FUNCTION__); // Grab from cache or build the array. if (!isset($styles) || $reset) { if (($cache = cache_get('styles_default_presets', 'cache')) && !$reset) { $styles = $cache->data; } else { $styles = array(); styles_module_load_all_includes(); foreach (module_implements('styles_default_presets') as $module) { $module_styles = module_invoke($module, 'styles_default_presets'); foreach ($module_styles as $field_type => $container) { $all_styles = styles_default_styles($field_type); $styles[$field_type] = $container; foreach ($container['containers'] as $style_name => $style) { $style['name'] = $style_name; $style['module'] = $module; $style['storage'] = STYLES_STORAGE_DEFAULT; $style['styles'] = isset($style['styles']) ? $style['styles'] : array(); $default_preset = isset($style['default preset']) ? $style['default preset'] : ''; foreach ($all_styles['styles'] as $container_style_name => $container_style) { if (!isset($style['styles'][$container_style_name])) { $style['styles'][$container_style_name] = array( 'default preset' => $default_preset, ); } } $styles[$field_type]['containers'][$style_name] = $style; } } } // @TODO: Add user styles next. drupal_alter('styles_default_presets', $styles); cache_set('styles_default_presets', $styles); } } if (isset($return_type)) { return $styles[$return_type]; } return $styles; } /** * Load all registered module/styles.inc and modules/includes/styles.inc files. */ function styles_module_load_all_includes() { foreach (styles_get_registered_classes() as $class) { module_load_include('inc', $class['module'], 'styles'); module_load_include('inc', $class['module'], $class['module'] .'/styles'); module_load_include('inc', $class['module'], 'includes/'. $class['module'] .'.styles'); module_load_include('inc', $class['module'], 'includes/styles/'. $class['module'] .'.styles'); } } /** * 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) && isset($classes[$class_name])) { return $classes[$class_name]; } else if (!isset($class_name)) { return $classes; } } /** * Implementation of hook_perm(). */ function styles_perm() { return array('administer styles'); }