4

How can I override the system_time_zones() function without hacking the Drupal 7 core?

I would like to modify how the dropdown list is presented, but cannot seem to get at it with hook_form_alter().

My end goal here is to reformat the timezone dropdown list to include only the location and offset by removing the date/time from the <select> lists <options> text.

For example,

'Pacific/Honolulu: Friday, June 1, 2012 - 6:43pm -1000' would change to 'Pacific/Honolulu: -1000'

This would be a simple edit to system_time_zones() in system.module, but hacking core is not an option for me.

If I were to hack core, this is what I would like to do.

Before Edits (for reference):

function system_time_zones($blank = NULL) {
  $zonelist = timezone_identifiers_list();
  $zones = $blank ? array('' => t('- None selected -')) : array();
  foreach ($zonelist as $zone) {
    // Because many time zones exist in PHP only for backward compatibility
    // reasons and should not be used, the list is filtered by a regular
    // expression.
    if (preg_match ('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
      $zones[$zone] = t('@zone: @date', array('@zone' => t(str_replace ('_', ' ', $zone)), '@date' => format_date(REQUEST_TIME, 'custom', variable_get('date_format_long', 'l, F j, Y - H:i') . ' O', $zone)));
    }
  }
  // Sort the translated time zones alphabetically.
  asort ($zones);
  return $zones;
}

After Edits (Changes are to $zones[$zone] in @zone string):

function system_time_zones($blank = NULL) {
  $zonelist = timezone_identifiers_list();
  $zones = $blank ? array('' => t('- None selected -')) : array();
  foreach ($zonelist as $zone) {
    // Because many time zones exist in PHP only for backward compatibility
    // reasons and should not be used, the list is filtered by a regular
    // expression.
    if (preg_match ('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
      $zones[$zone] = t('@zone: @date', array('@zone' => t(str_replace ('_', ' ', $zone)), '@date' => format_date(REQUEST_TIME, 'custom', ' O', $zone)));
    }
  }
  // Sort the translated time zones alphabetically.
  asort ($zones);
  return $zones;
}

How can I accomplish this without messing with core?

5

I was able to access the timezone dropdown list by implementing hook_form_alter() with an #after_build function. You can filter, clean and modify the array here.

function MYMODULE_form_user_profile_form_alter(&$form, &$form_state) {
   $form['#after_build'][] = 'MYMODULE_timezone_modify';
}

function MYMODULE_timezone_modify($form, &$form_state) {
   $form['timezone']['timezone']['#options'] = array(/*Your New or Modified Array Here*/);
   return $form;
}
  • This works great for the user profile page but any idea how to implement this for a date field with a time zone selection? I can't seem to find where the time zone options are set in the $form variable. – John81 Oct 23 '13 at 13:42
  • See answer to my question at drupal.stackexchange.com/questions/89949/… – John81 Oct 24 '13 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.