diff --git a/features.module b/features.module index e49dbb2..90958db 100644 --- a/features.module +++ b/features.module @@ -307,7 +307,7 @@ function features_include($reset = FALSE) { // Features provides integration on behalf of these modules. // The features include provides handling for the feature dependencies. // Note that ctools is placed last because it implements hooks "dynamically" for other modules. - $modules = array('features', 'block', 'context', 'field', 'filter', 'image', 'menu', 'node', 'taxonomy', 'user', 'views', 'ctools'); + $modules = array('features', 'block', 'context', 'field', 'filter', 'image', 'menu', 'node', 'taxonomy', 'user', 'views', 'ctools', 'system'); foreach (array_filter($modules, 'module_exists') as $module) { if (!module_hook($module, 'features_api')) { diff --git a/includes/features.system.inc b/includes/features.system.inc new file mode 100644 index 0000000..06e2205 --- /dev/null +++ b/includes/features.system.inc @@ -0,0 +1,216 @@ + array( + 'name' => t('Date formats'), + 'default_hook' => 'date_formats', + 'feature_source' => TRUE + ), + 'date_format_types' => array( + 'name' => t('Date format types'), + 'default_hook' => 'date_format_types', + 'feature_source' => TRUE, + ), + ); +} + +/** + * Implements hook_features_export_options(). + */ +function date_formats_features_export_options() { + $options = array(); + $format_types = system_get_date_types(); + $languages = (module_exists('locale')) ? locale_language_list('native') : array(); + + foreach ($format_types as $type => $format_type) { + $options[$type] = $format_type['title']; + foreach($languages as $langcode => $language) { + $options["$type::$langcode"] = "{$format_type['title']} ($language)"; + } + } + return $options; +} + +/** + * Implements hook_features_export_options(). + */ +function date_format_types_features_export_options() { + $options = array(); + $format_types = system_get_date_types(); + foreach ($format_types as $type => $format_type) { + $options[$type] = $format_type['title']; + } + return $options; +} + +/** + * Implements hook_features_export(). + */ +function date_formats_features_export($data, &$export, $module_name = '') { + // The filter_default_formats() hook integration is provided by the + // features module so we need to add it as a dependency. + $export['dependencies']['features'] = 'features'; + + $type_pipes = array(); + + foreach ($data as $name) { + list($type, $langcode) = explode('::', $name .'::', 3); + if ($format = _features_get_date_format($type, $langcode)) { + // Add format to exports + $export['features']['date_formats'][$name] = $name; + } + $type_pipes[$type] = $type; + } + + // Add date_format_type to pipe. + $pipe = (!empty($type_pipes)) ? array('date_format_types' => $type_pipes) : array(); + return $pipe; +} + +/** + * Implements hook_features_export(). + */ +function date_format_types_features_export($data, &$export, $module_name = '') { + // The filter_default_formats() hook integration is provided by the + // features module so we need to add it as a dependency. + $export['dependencies']['features'] = 'features'; + foreach ($data as $type) { + // Only add format type to export, it it is available. + if ($spec = system_get_date_types($type)) { + $export['features']['date_format_types'][$type] = $type; + } + } + + $pipe = array(); + return $pipe; +} + +/** + * Implements hook_features_export_render(). + */ +function date_formats_features_export_render($module, $data, $export = NULL) { + $code = array(); + $code[] = ' $formats = array();'; + $code[] = ''; + + foreach ($data as $name) { + list($type, $langcode) = explode('::', $name .'::', 3); + if ($format = _features_get_date_format($type, $langcode)) { + $var = array( + 'type' => $type, + 'format' => $format, + 'locales' => $langcode ? array($langcode) : array(), + ); + + $format_export = features_var_export($var, ' '); + $format_identifier = features_var_export($name); + $code[] = " // Exported format: $name"; + $code[] = " \$formats[{$format_identifier}] = {$format_export};"; + } + } + + $code[] = ' return $formats;'; + $code = implode("\n", $code); + return array('date_formats' => $code); +} + +/** + * Implements hook_features_export_render(). + */ +function date_format_types_features_export_render($module, $data, $export = NULL) { + $code = array(); + $code[] = ' $format_types = array();'; + + foreach ($data as $type) { + if ($spec = system_get_date_types($type)) { + + $format_export = features_var_export($spec['title'], ' '); + $format_identifier = features_var_export($type); + $code[] = " // Exported format type: $type"; + $code[] = " \$format_types[{$format_identifier}] = {$format_export};"; + } + } + $code[] = ' return $format_types;'; + $code = implode("\n", $code); + + return array('date_format_types' => $code); +} + +/** + * Implements hook_features_revert(). + */ +function date_formats_features_revert($module) { + return date_formats_features_rebuild($module); +} + +/** + * Implements hook_features_revert(). + */ +function date_format_types_features_revert($module) { + return date_format_types_features_rebuild($module); +} + + +/** + * Implements hook_features_rebuild(). + * + * hook_date_formats() is used to define the necessary formats, but the + * association to the date format type is not set by using that hook. So we have + * to set it in the database by ourselfs. + * + * @see hook_date_formats() + */ +function date_formats_features_rebuild($module) { + if ($defaults = features_get_default('date_formats', $module)) { + foreach ($defaults as $key => $spec) { + if (empty($spec['locales'])) { + variable_set('date_format_'. $spec['type'], $spec['format']); + } + else { + foreach ($spec['locales'] as $locale) { + locale_date_format_save($locale, $spec['type'], $spec['format']); + } + } + } + } +} + +/** + * Implements hook_features_rebuild(). + * + * @see hook_date_format_types() + */ +function date_format_types_features_rebuild($module) { + // Anything should be managed by hook_date_format_types(). +} + +/** + * Helper function to get the pure format (locale or from variable). + */ +function _features_get_date_format($type, $language = NULL) { + $format = ''; + if (!empty($language)) { + // Try to get a locale date format + $format = system_date_format_locale($language, $type); + } + + // If no language specific format was fount, or language is not set. + if (empty($format)) { + // DO not use variable_get() as this may fetch an allready localized variable. + $format = unserialize(db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => "date_format_". $type))->fetchField()); + } + return ($format) ? $format : NULL; +} \ No newline at end of file