0

I am trying to pragmatically set the revision log message after values of certain fields are changed (thus if a user has edits certain fields). More information is in the code comments

 function home_hub1_form_alter(&$form, FormStateInterface $form_state, $form_id){


//    Hide Revisions log message text box
$form['revision_log_message']['#access']= FALSE;

//set revision check box to false since we are creating a revision progeammatically
$form['new_revision'] = array(
    '#type' => 'hidden',
    '#value' => FALSE
);

// monitor every field if its from edit
if ($form_id == 'device_entity_edit_form'){

    $form['actions']['submit']['#submit'][] = 'home_hub1_form_device_entity_edit_submit';

  }
} 

function home_hub1_form_device_entity_edit_submit(array &$form, FormStateInterface $form_state){

$form_state->setTemporaryValue('entity_validated', TRUE);

//check if a field has changed and if it has write a revision log message
if ($form['field_city']['widget']['0']['value']['#value'] != $form['field_city']['widget']['0']['value']['#default_value']) {

    $message = 'CITY changed from: ' . $form['field_city']['widget']['0']['value']['#default_value'] . ' to: ' . $form['field_city']['widget']['0']['value']['#value'];

   $form_state->setValue('revision_log_message', $message);


}

I am getting the following error: Error: Cannot unset string offsets in Drupal\Core\Field\WidgetBase->extractFormValues()

I also tried to get the node object so that i create a revision directly from it like below:

function home_hub1_form_device_entity_edit_submit(array &$form, FormStateInterface $form_state){

$node = $form_state->getFormObject()->getEntity();

//check if a field has changed and if it has write a revision log message
if ($form['field_city']['widget']['0']['value']['#value'] != $form['field_city']['widget']['0']['value']['#default_value']) {

$message = 'CITY changed from: ' . $form['field_city']['widget']['0']['value']['#default_value'] . ' to: ' . $form['field_city']['widget']['0']['value']['#value'];

  $node->setRevisionUserId(\Drupal::currentUser()->id());
  $node->setRevisionCreationTime(REQUEST_TIME);
  $node->setNewRevision(TRUE); // enabling revision for the entity save.
  $node->setRevisionLogMessage($message);

}

How can I create a revision after form submission?

0

I found the solution. I should have used hook_form_BASE_FORM_ID_alter() instead of hook_form_FORM_ID_alter

Also the call back function should be home_hub1_my_entity_builder

0

I would just go with this module: Try this module: https://www.drupal.org/project/revision_log_default

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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