Skip to content

Instantly share code, notes, and snippets.

@derhasi
Created September 7, 2011 15:30
Show Gist options
  • Save derhasi/1200878 to your computer and use it in GitHub Desktop.
Save derhasi/1200878 to your computer and use it in GitHub Desktop.
various exportables
<?php
/**
* Features hooks for date_format.
*/
/**
* Implements hook_features_export_options().
*/
function date_formats_features_export_options() {
$options = array();
$format_types = system_get_date_types();
$languages = locale_language_list('native');
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().
*/
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';
foreach ($data as $name) {
list($type, $langcode) = explode('::', $name .'::', 3);
if ($format = _fexportables_get_date_format($type, $langcode)) {
// Add format to exports
$export['features']['date_formats'][$name] = $name;
}
}
// @TODO: maybe add date_format_type to pipe (if it is not locked so far).
$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 = _fexportables_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_revert().
*/
function date_formats_features_revert($module) {
return date_formats_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']);
}
}
}
}
}
/**
* Helper function to get the pure format (locale or from variable).
*/
function _fexportables_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;
}
name = FExportables
description = Make additional components exportable for features.
core = 7.x
package = Features
dependencies[] = ctools
files[] = fexportables.module
<?php
/**
* @file
* Make date formats exportbale by using ctools exportable tool.
*/
/**
* Implements hook_features_api().
*/
function fexportables_features_api() {
return array(
'date_formats' => array(
'name' => t('Date formats'),
'default_hook' => 'date_formats',
'feature_source' => TRUE,
'file' => drupal_get_path('module', 'fexportables') .'/date_formats.features.inc',
),
);
}
/**
* Implements hook_schema_alter().
*
* - Adds exportables for languages via ctools.
*/
function fexportables_schema_alter(&$schema) {
if (isset($schema['languages']) && !isset($schema['languages']['export'])) {
$schema['languages']['export'] = array(
'key' => 'name',
'key name' => 'Name',
'primary key' => 'language',
'identifier' => 'languages', // Exports will be as $myobj
'default hook' => 'default_languages', // Function hook name.
'api' => array(
'owner' => 'locale',
'api' => 'languages', // Base name for api include files.
'minimum_version' => 1,
'current_version' => 1,
),
);
$schema['languages']['fields']['javascript']['no export'] = TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment