Here's how to do it with an entity builder callback.
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
//Implements hook_form_FORM_ID_alter()
function agenda_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['title']['#access'] = FALSE;
$form['#entity_builders'][] = 'agenda_node_builder';
}
function agenda_node_builder($entity_type, NodeInterface $node, $form, FormStateInterface $form_state)
{
$date_of_meeting = $node->field_date_of_meeting->date->format('Y-m-d');
$node->setTitle('Agenda for meeting on '.$date_of_meeting);
}
Notes:
I found that the callback had to be set up in hook_form_BASE_FORM_ID_alter()
rather than hook_form_FORM_ID_alter
in which case it didn't get called.
Didn't get to the bottom of this but it's probably something to do with the fact that alter hooks are called in the following order: hook_form_alter()
, hook_form_BASE_FORM_ID_alter()
, hook_form_FORM_ID_alter()
.
Node
s #entity_builders
offer a Node
entity object for modification before being stored when the form is eventually submitted.
Thanks to @Berdir for the pointers.