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;
$form_state->setRebuild();
}
}