0

My goal:

  • If a person select a start date the end date is 1 hour later, without the need of selecting the end date (2)

Normally the Form provides 2 selections of dates, start date (1) and end date (2).

enter image description here

I am able to succesfully hide end date (2), in bee_add_reservation_form, with hook_form_alter().

With hook_form_alter() in mymodule.module I modified a form render array.

$end_date is defined in class AddReservationForm of the bee module.

How to alter or override the $end_date in class AddreservationForm? I would like to modify it like so:

    $end_date = clone($start_date);
    $end_date->modify('+1 hour');

Although While doing this within the class this Error occurs:

Error: Call to protected method Drupal\bee\Form\AddReservationForm::getAvailableUnits() from global scope

mymodule.module:

<?php

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'bee_add_reservation_form') {
 /**
   * Extends the AddReservationForm class.
   *
   */

    $form['end_date'] = [
        // hides end_date
      '#type' => 'hidden',
        ];
    $form['repeat'] = [
        // hides repeat option
        '#type' => 'hidden',
          ];
    // start date from protected method
   // $start_date = new \DateTime($start_date->format('Y-m-d H:i')); 
    $start_date = AddReservationForm::getAvailableUnits($start_date);

    // end date should be always 1 hour later
    $end_date = clone($start_date);
    $end_date->modify('+1 hour');
      

    
  }
5
  • mymodule_alter needs to be mymodule_form_alter. And you need to target the form id so you dont touch any other forms.
    – Kevin
    Commented Aug 24, 2022 at 15:55
  • Just to clarify, and apologies if I've misunderstood, it might just be the way I'm reading it - are you expecting this line: $end_date = clone($start_date); to somehow override a similar line in a method within the AddReservationForm form class? If so I'd strongly recommend reading up on some PHP OO basics, that just isn't how it works
    – Clive
    Commented Aug 24, 2022 at 16:00
  • I was not expecting it. Within the class $end_date is declared in different methods as a new \Datetime ... and a input field. I would like to hide end_date and is automatically + 1 hour
    – Joehoe
    Commented Aug 24, 2022 at 16:15
  • Can you please add your human-friendly use-case to the topic? You've explained how you are trying to do something that isn't working, but without knowing what the end goal is, we cannot tell you how to fix your issue.
    – Jaypan
    Commented Aug 24, 2022 at 17:52
  • @Jaypan I have added my goal
    – Joehoe
    Commented Aug 24, 2022 at 18:08

1 Answer 1

1

I think hook_form_alter is not the correct place for this. hook_form_alter is called everytime the form is built/created - at this moment your user haven't filled in any start date. So this hook is only helpful to hide the field as you have done.

Instead you should add a custom submit handler in your hook_form_alter

$form['#submit'][] = 'your_custom_submit_handler';

and there in your submit handler you can change the values as you need.

function your_custom_submit_handler(array &$form, FormStateInterface $form_state) {
  $input = $form_state->getUserInput();
  $start_date = $input['start_date'];
   ...
}

Your Answer

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

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