Skip to content

Instantly share code, notes, and snippets.

@tobiasbaehr
Created November 3, 2013 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasbaehr/7292175 to your computer and use it in GitHub Desktop.
Save tobiasbaehr/7292175 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Features export for core components.
*
* - date_format_types
*
* date_format_types_features_revert() and date_format_types_features_rebuild()
* should be managed by hook_date_format_types().
*
* - locale_date_format
* Localized date formats.
*
* - custom_date_formats
* These formats can be deleted via gui and can be used in all date format types.
*/
/**
* Implements hook_features_api().
*/
function features_date_features_api() {
$components['date_format_types'] = array(
'name' => t('Date format types'),
'default_hook' => 'date_format_types',
'feature_source' => TRUE,
);
$components['custom_date_formats'] = array(
'name' => t('Custom date formats'),
'default_hook' => 'custom_date_formats',
'feature_source' => TRUE,
);
if (module_exists('locale')) {
$components['locale_date_format'] = array(
'name' => t('Locale date format'),
'default_hook' => 'locale_date_format',
'feature_source' => TRUE
);
}
return $components;
}
/**
* 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_format_types_features_export($data, &$export, $module_name = '') {
$export['dependencies']['features_date'] = 'features_date';
$pipe = array('variable' => array());
$map_variable = features_get_default_map('variable');
$map_custom_formats = features_get_default_map('custom_date_formats');
$map_date_format_types = features_get_default_map('date_format_types');
foreach ($data as $type) {
// Only add format type to export, if it is available.
if ($spec = system_get_date_types($type)) {
$variable = 'date_format_' . $type;
// Add a dependency when the variable already in a feature but not by use.
if (isset($map_variable[$variable]) && $map_variable[$variable] != $module_name) {
$export['dependencies'][$map_variable[$variable]] = $map_variable[$variable];
}
else {
// Add strongarm variable for given default date format for this type.
$pipe['variable'][$variable] = $variable;
}
$default_format = _features_date_get_default_format($type);
// Add a dependency when the default date format is a custom date format and already in a feature but not by use.
if (isset($map_custom_formats[$default_format]) && $map_custom_formats[$default_format] != $module_name) {
$export['dependencies'][$map_custom_formats[$default_format]] = $map_custom_formats[$default_format];
}
else {
// Add custom date format for this type.
$pipe['custom_date_formats'][$default_format] = $default_format;
}
// Add a dependency when the date_format_type already in a feature but not by use.
if (isset($map_date_format_types[$type]) && $map_date_format_types[$type] != $module_name) {
$export['dependencies'][$map_date_format_types[$type]] = $map_date_format_types[$type];
}
else {
$export['features']['date_format_types'][$type] = $type;
}
}
}
return $pipe;
}
/**
* 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 date 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_export_options().
*/
function locale_date_format_features_export_options() {
$options = array();
$format_types = system_get_date_types();
$languages = locale_language_list('native');
foreach ($format_types as $type => $format_type) {
foreach ($languages as $langcode => $language) {
$format = _features_date_get_locale_date_format($type, $langcode);
if (!empty($format)) {
$options["$type::$langcode"] = "{$format_type['title']} ($language)";
}
}
}
return $options;
}
/**
* Implements hook_features_export().
*/
function locale_date_format_features_export($data, &$export, $module_name = '') {
$export['dependencies']['features'] = 'features';
$export['dependencies']['features_date'] = 'features_date';
$map_language = features_get_default_map('language');
$map_date_format_types = features_get_default_map('date_format_types');
$map_locale_date_format = features_get_default_map('locale_date_format');
$pipe = array('date_format_types' => array(), 'language' => array());
foreach ($data as $name) {
list($type, $langcode) = explode('::', $name . '::', 3);
if ($format = _features_date_get_locale_date_format($type, $langcode)) {
// Add a dependency when the type already in a feature but not by use or system.
if (isset($map_date_format_types[$type]) && !in_array($map_date_format_types[$type], array('system', $module_name))) {
$export['dependencies'][$map_date_format_types[$type]] = $map_date_format_types[$type];
}
else {
// Add date_format_type to pipe.
$pipe['date_format_types'][$type] = $type;
}
// Add a dependency when the language is in a feature but not by use.
if (isset($map_language[$langcode]) && $map_language[$langcode] != $module_name) {
$export['dependencies'][$map_language[$langcode]] = $map_language[$langcode];
}
else {
// Add language to pipe.
$pipe['language'][$langcode] = $langcode;
}
if (isset($map_locale_date_format[$name]) && $map_locale_date_format[$name] != $module_name) {
$export['dependencies'][$map_locale_date_format[$name]] = $map_locale_date_format[$name];
}
else{
// Add format to exports
$export['features']['locale_date_format'][$name] = $name;
}
}
}
return $pipe;
}
/**
* Implements hook_features_export_render().
*/
function locale_date_format_features_export_render($module, $data, $export = NULL) {
$code = array();
$code[] = ' $locale_date_formats = array();';
$code[] = '';
foreach ($data as $name) {
list($type, $langcode) = explode('::', $name .'::', 3);
if ($format = _features_date_get_locale_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[] = " \$locale_date_formats[{$format_identifier}] = {$format_export};";
}
}
$code[] = ' return $locale_date_formats;';
$code = implode("\n", $code);
return array('locale_date_format' => $code);
}
/**
* Implements hook_features_revert().
*/
function locale_date_format_features_revert($module) {
return locale_date_format_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 locale_date_format_features_rebuild($module) {
if ($defaults = features_get_default('locale_date_format', $module)) {
foreach ($defaults as $key => $spec) {
foreach ($spec['locales'] as $locale) {
locale_date_format_save($locale, $spec['type'], $spec['format']);
}
}
}
}
/**
* Helper function to get the pure format (locale or from variable).
*/
function _features_date_get_locale_date_format($type, $langcode) {
// Try to get a locale date format
$format = system_date_format_locale($langcode, $type);
// If no format was found, we get the default value.
if (empty($format)) {
$format = _features_date_get_default_format($type);
}
return ($format) ? $format : NULL;
}
/**
* Helper function to get the pure format (from variable).
*/
function _features_date_get_default_format($type) {
// Do not use variable_get() as this may fetch an already localized variable.
$format = db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => 'date_format_' . $type))->fetchField();
return ($format !== FALSE) ? unserialize($format) : NULL;
}
/**
* Implements hook_features_export_options().
*/
function custom_date_formats_features_export_options() {
$formats = system_get_date_formats('custom');
$options = array();
if (!empty($formats)) {
foreach ($formats as $format => $format_data) {
$options[$format] = $format;
}
}
return $options;
}
/**
* Implements hook_features_export().
*/
function custom_date_formats_features_export($data, &$export, $module_name = '') {
$export['dependencies']['features'] = 'features';
$export['dependencies']['features_date'] = 'features_date';
foreach ($data as $format) {
$map = features_get_default_map('custom_date_formats');
if (!empty($map[$format]) && $map[$format] != $module_name) {
$export['dependencies'][$map[$format]] = $map[$format];
}
else{
$export['features']['custom_date_formats'][$format] = $format;
}
}
return array();
}
/**
* Implements hook_features_export_render().
*/
function custom_date_formats_features_export_render($module, $data, $export = NULL) {
$formats = system_get_date_formats('custom');
$code = array();
$code[] = ' $custom_date_formats = array();';
foreach ($data as $format) {
if (!empty($formats[$format])) {
$format = features_var_export($format);
$code[] = " \$custom_date_formats[{$format}] = {$format};";
}
}
$code[] = ' return $custom_date_formats;';
$code = implode("\n", $code);
return array('custom_date_formats' => $code);
}
/**
* Implements hook_features_rebuild().
*/
function custom_date_formats_features_rebuild($module) {
return custom_date_formats_features_revert($module);
}
/**
* Implements hook_features_revert().
*/
function custom_date_formats_features_revert($module) {
if ($defaults = features_get_default('custom_date_formats', $module)) {
$formats = system_get_date_formats('custom');
foreach ($defaults as $format) {
if (empty($formats[$format])) {
$format_data['format'] = trim($format);
$format_data['type'] = 'custom';
$format_data['locked'] = 0;
$format_data['is_new'] = 1;
system_date_format_save($format_data);
}
}
}
}
@ckidow
Copy link

ckidow commented Dec 10, 2013

merci - it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment