Below you could find my code. It works fine until below the comment. I also tried countless combination of $form_state->saveValue(), but nothing works. Any ideas?

    function myforms_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      if ($form_id == 'commerce_product_default_edit_form' || $form_id == 'commerce_product_default_add_form') {
        $form['#submit'][] = 'myforms_extra_submit_handler';

    function myforms_extra_submit_handler($form, FormStateInterface $form_state) {
      $events_dates = $form_state->getFormObject()->getEntity()->get('event_date')->getValue();
      $count = count($events_dates);
      if (empty($events_dates[$count-1])){
        $count = $count - 1;
      if ($count > 0) {
        $start_date = date("j/n/y", strtotime($events_dates[0]['value']));
        $end_date = date("j/n/y", strtotime($events_dates[$count-1]['end_value']));
        if ($count == 1) {
          $count_suffix = "zjazd";
        } else if ($count == 2 || $count == 3) {
          $count_suffix = "zjazdy";
        } else {
          $count_suffix = "zjazdów";
        $date = $start_date.' - '.$end_date.' ('.$count.' '.$count_suffix.')';

        $title = $form_state->getFormObject()->getEntity()->get('title')->getValue()[0]['value'];
        /* code above works, below is the code which doesn't */
        $form_state->set('title', $title.$date);
        $form['elements']['title']['#value'] = $title.$date;
        $form['elements']['title']['#default_value'] = $title.$date;

2 Answers 2


I assume with code works you mean you get the updated title value. Then another submit handler has already transferred the values to the entity and your changes to $form_state are too late.

So you can try to position your submit handler first:

array_unshift($form['#submit'], 'myforms_extra_submit_handler');

or update the entity instead of $form_state:

$entity = $form_state->getFormObject()->getEntity(); 
$title = $entity->get('title')->value;
$entity->set('title', $title . $date);

Caveat: Normally submit handlers in entity forms are attached to submit buttons. So you might want to check first all submit handlers which are active in the form before you decide where to place your submit handler.

Different approach

You are asking why your submit handler is not working and there is definitely a solution for that, but there is probably a much easier solution outside of the form when the form is trying to save the entity. See Programmatically update a field's value


It's not clear what "doesn't work" means to you - what do you expect to happen, and what actually happens? We can only guess at what you mean, so perhaps I'm guessing wrong here ...

Because $form is passed to your submit handler by a value, not by reference, the changes you make to $form after your comment are confined to the scope of the submit handler and won't affect anything outside your function. If you want to change the elements of the $form array you should do that inside your hook_form_alter(), or perhaps in an Ajax callback, not in a submit function. A submit function should only be processing the form data, not trying to alter what the form looks like to the end user. But if you really must, then you can change the form values in your submit handler by set the values in $form_state instead of changing $form.

Also confusing is your $form['elements']['title']['#value'] - most form elements have a #title to hold the value of the title, or if this were a markup #type then you would have a #markup to hold the value. I'm not sure why you're trying to set #value here. It would help if you would show at least part of the structure of the $form you're trying to alter.

  • Hi, what I mean is I am not able to save the updated title. I also tried that in hook_entity_presave(), but with no luck. The code until saving part works.
    – rukya
    Dec 16, 2020 at 9:29

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.