How do I change a value, in this case the title field of a node object form, in the preprocess function of the form_alter hook.
$form['actions']['publish']['#submit'][] = MODULE_submit_preprocess';
function MODULE_submit_preprocess($form, FormStateInterface $form_state) {
$form_state->setValue('title', 'Updated Title'); //not working...
$fstate = $form_state->getFormObject()->getEntity();
dsm($fstate);
}
dsm result
stdClass Object ( [__CLASS__] => Drupal\node\Entity\Node [in_preview] => [values] => Array ( [title] => Array ( [x-default] => Array ( [0] => Array ( [value] => Old Title
MODULE_ENTITY_presave()
function. After submit and before save in db, Drupal call this function then you can change something in your entity. If you want to change a node, then useMODULE_node_presave()
. And will be called when you create and update.