Using hook_form_alter

Created on 2 June 2023, about 1 year ago
Updated 19 June 2023, about 1 year ago

Could someone point me to how to alter a profile field in the checkout pane? I want to set the DOB field to be a year range, and this code works for creating and editing the profile directly, but I haven't been able to target the field in the checkout pane. I've tried to use kint. Thank you for any help.

      $max_age = date('Y') - 80; // 80 years ago
      $min_age = date('Y') - 5; // 5 years ago

switch ($form_id) {
   case 'commerce_checkout_flow_multistep_default':
      if ($form['#step_id'] === 'order_information') {

/* this doesn't work */
        $form['profile_form:registrant']['edit-profile-formregistrant']['profile']['#default_value']['field_date_of_birth']['widget'][0]['value']['#date_year_range'] = $max_age.':'.$min_age;

/* this works */
        $form['profile_form:registrant']['#title'] = "Enter Registration Information Below";
      }
      break;

/* this works */
    case 'profile_registrant_edit_form':
    case 'profile_registrant_add_form':
      $form['field_date_of_birth']['widget'][0]['value']['#date_year_range'] = $max_age.':'.$min_age;
  }

πŸ’¬ Support request
Status

Active

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mygumbo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mygumbo
  • πŸ‡¬πŸ‡§United Kingdom joachim

    I'd say use the hook that modifies the widget instead.

  • πŸ‡ΊπŸ‡ΈUnited States mygumbo

    Would that be hook_field_widget_complete_form_alter? Unsure how to target that specific field that uses the select widget for datetime.

  • πŸ‡ΊπŸ‡ΈUnited States mygumbo

    Does anyone have an example of modifying (any kind of) a profile field in the checkout pane? Thank you.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    It actually sounds like you want the date field to so something fairly different to what it does OOTB.
    What about a custom formatter? You can then set it up on a custom view mode, and IIRC you can select the view mode to use in the commerce checkout pane.

  • πŸ‡ΊπŸ‡ΈUnited States mygumbo

    Thanks... I'm looking for something super simple - this works on the profile form itself:

    case 'profile_registrant_edit_form':
        case 'profile_registrant_add_form':
          $form['field_date_of_birth']['widget'][0]['value']['#date_year_range'] = $max_age.':'.$min_age;
Production build 0.69.0 2024