I spent about 1/2 hour last night in a failed attempt to limit the timezone options on a test site I'm building. It's going to be dealing with events across the U.S., so I want each event's date field to do per-date timezone support. However, it's *only* for events in the U.S., so I need about 4 (or, if you count all the crazy options in Indiana, maybe 10) distinct options, not the 120 or so in there now. ;) End users trying to submit events on this site are going to be overwhelmed and confused by the options, not to mention the usability hassle of accurately finding the timezone you're looking for in a select list with 100+ choices.

So I say to myself: "no problem, I'll just hook_form_alter() that and reset the #options array..."

Heh, except by the time hook_form_alter() is invoked, there are still no options, since this is a magic form element where the options are injected later via the element processing. :(

So I say to myself: "well, in that case, I guess I'll inject my own #process callback to the element and alter it from there..."

Heh, except that by doing so, I seem to have broken the default processing for the element, and in that case, my "combo" date field (with From, To, and Timezone) is rendered without a timezone field at all. :( I'm appending to the #process array, not clobbering, but it seems like somewhere in the call chain if anything tried to process that element already, the default processing doesn't happen. I'm not sure how to say "add this #process callback *and* do the default processing...".

I can see all the choices via an #after_build callback, but of course, that's after the form is built, so I can't actually chance the #options at that point. :(

So, I'm slightly at a loss as to how I'd go about doing this. :( Out of despair, I'm considering hacking my copy of date_timezone_names() to just hard-code the list I care about, but that's obviously evil and wrong for all sorts of reasons...

I searched the issue queue here and saw no reference to someone trying to do this, apologies if I missed something and this is duplicate. Assuming this is a new request (which seems a bit surprising, but hey, I know I'm weird sometimes), a few questions:

A) Is there a viable, recommended way to alter the #options in the 'timezone' field in a date field element?

B) Is there any interest in a feature in Date* (not sure if it should be DateAPI or the Date field itself) to provide a UI to limit the available timezone choices? Think of it like the "Granularity" and/or "Time increment" settings -- by default, you should be able to specify a date as exactly as possible, but not everyone needs 60 options for the minutes, sometimes 4 are plenty. ;)

Thanks!
-Derek

Comments

jason.fisher’s picture

Also looking for insight on this.

codevoice’s picture

Subscribe

gzveri’s picture

Assigned: Unassigned » gzveri

I have inserted this function in template.php , where blueprint is my theme name .

function blueprint_content_multiple_values($element) {

if($element['#field_name'] == 'field_date') {
//echo "

";print_r($element[0]['timezone']['timezone']['#options']);echo "

";//die('gde ia');
$myoptions = array(
0 => '',
'America/New_York' => 'EST',
'America/Chicago' => 'CST',
'America/Denver' => 'MST',
'America/Los_Angeles' => 'PST',
);
$element[0]['timezone']['timezone']['#options'] = $myoptions;
}
$field_name = $element['#field_name'];
$field = content_fields($field_name);
$output = '';

if ($field['multiple'] >= 1) {
$table_id = $element['#field_name'] .'_values';
$order_class = $element['#field_name'] .'-delta-order';
$required = !empty($element['#required']) ? '*' : '';

$header = array(
array(
'data' => t('!title: !required', array('!title' => $element['#title'], '!required' => $required)),
'colspan' => 2
),
t('Order'),
);
$rows = array();

// Sort items according to '_weight' (needed when the form comes back after
// preview or failed validation)
$items = array();
foreach (element_children($element) as $key) {
if ($key !== $element['#field_name'] .'_add_more') {
$items[] = &$element[$key];
}
}
usort($items, '_content_sort_items_value_helper');

// Add the items as table rows.
foreach ($items as $key => $item) {
$item['_weight']['#attributes']['class'] = $order_class;
$delta_element = drupal_render($item['_weight']);
$cells = array(
array('data' => '', 'class' => 'content-multiple-drag'),
drupal_render($item),
array('data' => $delta_element, 'class' => 'delta-order'),
);
$rows[] = array(
'data' => $cells,
'class' => 'draggable',
);
}

$output .= theme('table', $header, $rows, array('id' => $table_id, 'class' => 'content-multiple-table'));
$output .= $element['#description'] ? '

'. $element['#description'] .'

' : '';
$output .= drupal_render($element[$element['#field_name'] .'_add_more']);

drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
}
else {
foreach (element_children($element) as $key) {
$output .= drupal_render($element[$key]);
}
}

return $output;
}

hobo’s picture

yup i'm running into the same problem.. has anyone tried gzveris code?

gobbles’s picture

Try implementing the theme_date_select_element function in your theme. The html for the select options are created in that theme function.

Anonymous’s picture

I've actually written a module for this if anyone is still having any issues. It can be found on my github

chadhester’s picture

Your module worked like a charm (for my D7 use-case)! Thanks!

damienmckenna’s picture

Status: Active » Closed (outdated)

We're sorry but the D6 release of Date module is no longer being supported. You are encouraged to update to Drupal 7 or 8, or direct questions to Drupal Answers.