Score:0

How to modify the timezone options list?

us flag

I have a date range field in a paragraph which is set to allow setting the timezone. Can someone suggest how I can alter the list of options used to select the TZ?

For the user profile form a simple form_alter exposes the timezone field options but when trying to alter the paragraph form I only see the timezone value already set as #date_timezone.

Possibly I can override the class used to build this list?

I could do this with JS without too much effort; but not the correct approach.

my flag
I need to check my list, but I am nearly positive there is an open issue to add an alter hook for this.
liquidcms avatar
us flag
I couldn't find anything to help this but certainly could use a little helper function to do this globally. I have 2 places it shows up: 1. user profile (was easy to set with a form alter), 2: on date field in a paragraph - no answer yet. Ideally could do one alter to globally change this so was thinking override class; but can't even tell where that it.
Score:-1
eg flag

You can do that by using HOOK_form_alter and altering the widget of the field:

HOOK_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Check for your target entity form.
  // Replace 'your_entity_type' and 'your_entity_bundle' with your entity type and bundle.
  if (strpos($form_id, 'your_entity_type_your_entity_bundle') !== FALSE) {

    // Define the custom timezone options.
    $customTimezones = [
      'America/New_York' => 'America/New_York',
      'America/Los_Angeles' => 'America/Los_Angeles',
      'Europe/London' => 'Europe/London',
    ];

    foreach (array_keys($form['your_paragraph_field']['widget']) as $index) {

      // Check if the index is numeric, which indicates a paragraph item.
      if (is_numeric($index)) {

        // Alter the timezone options of the date range field.
        $form['PARAGRAPH_FIELD']['widget'][$index]['subform']['DATE_RANGE_FIELD']['timezone']['#options'] = $custom_timezones;
      }
    }
  }
}
liquidcms avatar
us flag
This does not work. Possibly it is a function of the paragraph widget which is used? I am using Paragraphs (stable) and there is no paragraph IEF on the node form when the form is first loaded as it gets added later with ajax. So the node form alter is run multiple times but in none of them is there a timezone #options.
liquidcms avatar
us flag
No luck finding a right way to do this so thought i would be smart and hack the system.module to add an alter to allow modifying the $zones (as should be there in the first place). All i do in my alter is remove all the non-America optgroups. It works perfectly but form submit complains it is not a valid option. This isn't standard Drupal form processing is it? I should be able to remove items from a select list without issue.
Hodba Khalaf avatar
eg flag
Sorry this is not working for you. How are you requesting the timezone field with the date range in the paragraphs you're using? What modules are you using?
liquidcms avatar
us flag
Modules? Core plus paragraphs. This is just a basic core daterange field with timezone collection enabled. But, less specifically, this is every field in/out of a paragraph (such as the core user entity) that uses a TZ selection. Yes, on simple entity forms, it is possible to form alter; but ideally there would be an alter to just globally change this list everywhere it is used. Which i added, but for some reason throws an "illegal option" error when form is submitted.
liquidcms avatar
us flag
Adding an alter in system_time_zones() does work as long as not when submitting the form. Not the proper way to do this but suggest this is due to a bug in core which might be being worked on for D10.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.