0

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.

2
  • I need to check my list, but I am nearly positive there is an open issue to add an alter hook for this.
    – mpdonadio
    Apr 1 at 15:47
  • 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.
    – liquidcms
    Apr 1 at 21:02

1 Answer 1

-1

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;
      }
    }
  }
}
5
  • 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
    Apr 3 at 14:21
  • 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.
    – liquidcms
    Apr 3 at 18:16
  • 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? Apr 4 at 14:10
  • 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
    Apr 4 at 19:41
  • 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.
    – liquidcms
    Apr 7 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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