--- languageicons.module.orig 2009-02-02 21:10:33.000000000 +0100 +++ languageicons.module 2009-03-10 02:58:57.000000000 +0100 @@ -120,16 +120,18 @@ * This function can be overridden for no language icons. */ function theme_languageicons_icon($language, $title = NULL) { - if ($path = variable_get('languageicons_path', drupal_get_path('module', 'languageicons') .'/flags/*.png')) { + if ($path = variable_get('languageicons_path', '')) { $src = base_path() . str_replace('*', $language->language, $path); $title = $title ? $title : $language->native; $attribs = array('class' => 'language-icon', 'alt' => $title); - if ($size = variable_get('languageicons_size', '16x12')) { + if ($size = variable_get('languageicons_size', '')) { list($width, $height) = explode('x', $size); $attribs += array('width' => $width, 'height' => $height); } return "'; } + else + drupal_set_message(t('The flag icon theme is not set, this can be done at the !settingspage', array('!settingspage' => l(t('language icons settings page'), 'admin/settings/language/configure/icons'))), 'warning'); } /** --- languageicons.admin.inc.orig 2008-10-11 15:36:12.000000000 +0200 +++ languageicons.admin.inc 2009-03-10 02:57:08.000000000 +0100 @@ -11,8 +11,18 @@ * * @ingroup forms * @see system_settings_form() + * @see languageicons_admin_settings_submit() */ function languageicons_admin_settings() { + $hide_custom_fieldset_js = + '$(document).ready(function(){'. + ' $("input#edit-languageicons-path-custom:not(:checked)").parents("form").find("input#edit-languageicons-custompath").parent().parent().hide();'. + ' $("input#edit-languageicons-path-custom").click(function(){'. + ' $("#edit-languageicons-custompath").parent().parent().show("fast");'. + ' });'. + '})'; + drupal_add_js($hide_custom_fieldset_js, 'inline', 'header'); + $form['show'] = array( '#type' => 'fieldset', '#title' => t('Add language icons'), @@ -36,21 +46,86 @@ '#description' => t('Where to display the icon, relative to the link title.'), ); $form['languageicons_path'] = array( + '#type' => 'radios', + '#title' => t('Flag icon set'), + '#default_value' => variable_get('languageicons_path', drupal_get_path('module', 'languageicons') .'/flags/plain/*.png'), + '#options' => array_merge(_languageicons_flagsets('options'), array('custom' => t('Custom (set below)'))), + '#description' => t('Flag icon sets are located within the flags folder in the languageicons module directory.'), + ); + $form['custom'] = array( + '#type' => 'fieldset', + '#title' => t('Custom icons'), + ); + $form['custom']['languageicons_custompath'] = array( '#type' => 'textfield', - '#title' => t('Icons file path'), - '#default_value' => variable_get('languageicons_path', drupal_get_path('module', 'languageicons') .'/flags/*.png'), + '#title' => t('Custom icons file path'), + '#default_value' => variable_get('languageicons_path', drupal_get_path('module', 'languageicons') .'/flags/plain/*.png'), '#size' => 70, '#maxlength' => 180, '#description' => t('Path for language icons, relative to Drupal installation. \'*\' is a placeholder for language code.'), ); - $form['languageicons_size'] = array( + $form['custom']['languageicons_size'] = array( '#type' => 'textfield', - '#title' => t('Image size'), + '#title' => t('Custom Icon size'), '#default_value' => variable_get('languageicons_size', '16x12'), '#size' => 10, '#maxlength' => 10, - '#description' => t('Image size for language icons, in the form "width x height".'), + '#description' => t('Image size for language icons, in the form "width x height" (only for custom Flag icon set).'), ); + $form['#validate'][] = 'languageicons_admin_settings_validate'; + $form['#submit'][] = 'languageicons_admin_settings_submit'; return system_settings_form($form); } + +/** + * Validate custom path existance + */ +function languageicons_admin_settings_validate($form, &$form_state) { + if ($form_state['values']['languageicons_path'] == 'custom' + && !file_check_location($form_state['values']['languageicons_custompath'])) { + form_set_error('custompath', t('The given path does not exist.')); + } +} + +/** + * Handle languageicons settings form submit + * + * Before saving icon path and size, set path to custompath if custom, + * otherwise retrieve size from flagsetinfo. + */ +function languageicons_admin_settings_submit($form, &$form_state) { + $languageicons_path =& $form_state['values']['languageicons_path']; + if ($languageicons_path == 'custom') { + $languageicons_path = $form_state['values']['languageicons_custompath']; + } + $flagsetsizes = _languageicons_flagsets('dimensions'); + if (isset($flagsetsizes[$languageicons_path])) { + $form_state['values']['languageicons_size'] = $flagsetsizes[$languageicons_path]; + } +} + +/** + * Searches for .flagsetinfo-files within the modules flags directory. + * @param $query + * + * @return + * array with requested flagset info keyed on path. + */ +function _languageicons_flagsets($query) { + $flagsets = array(); + $flagspath = drupal_get_path('module', 'languageicons') .'/flags/'; + $setinfofiles = file_scan_directory($flagspath, '.*flagsetinfo'); + foreach ($setinfofiles as $setinfofile) { + $setinfo = drupal_parse_info_file($setinfofile->filename); + $setpath = $flagspath . $setinfo['name'] .'/*.'. $setinfo['format']; + switch ($query) { + case 'dimensions': + $flagsets[$setpath] = $setinfo['dimensions']; + break; + case 'options': + $flagsets[$setpath] = $setinfo['name'] .' theme ('. $setinfo['dimensions'] .'px): '. $setinfo['description']; + } + } + return $flagsets; +}