4
votes

I have an editing node form. When user enters new value and clicks on submit to edit the node, I first want to get the old node back, manipulate the value and then just save/update the node.

Below is my solution, but it does not work.

function custom_module_form_node_form_alter(&$form, FormStateInterface $form_state) {
    $editing_entity = $form_state->getFormObject()->getEntity();

    if (!$editing_entity->isNew()) {
        $form['actions']['submit']['#submit'][] = 'custom_module_node_form_submit';
    }
}

function custom_module_node_form_submit($form, FormStateInterface $form_state) {
   $editing_entity = $form_state->getFormObject()->getEntity();

   $entity = Drupal::entityTypeManager()->getStorage('node')->load($editing_entity->id());
}

In the form_submit hook, I tried to get the old node back but it is already too late and the node is already updated/saved. How can I get the old node back and manipulate the value before updating/saving the node in Drupal 8?

3

3 Answers

4
votes

Try using hook_entity_presave():

/**
 * Implements hook_entity_presave().
 */
function YOUR_MODULE_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  switch ($entity->bundle()) {
    // Here you modify only your day content type
    case 'day':
      // Setting the title with the value of field_date.
      $entity->setTitle($entity->get('field_date')->value);
     break;
  }
}

Solution taken from here: https://drupal.stackexchange.com/questions/194456/how-to-use-presave-hook-to-save-a-field-value-as-node-title

Also you can get old value like: $entity->original. Check it out here:

https://drupal.stackexchange.com/questions/219559/how-to-get-the-original-entity-on-hook-entity-presave

3
votes

I decide to manipulate the values in the form validate hook as following.

function custom_module_form_node_form_alter(&$form, FormStateInterface $form_state) {
    $editing_entity = $form_state->getFormObject()->getEntity();

    if (!$editing_entity->isNew()) {
        $form['#validate'][] = 'custom_module_node_form_validate';
    }
}

function custom_module_node_form_validate(array &$form, FormStateInterface $form_state) {
    $old_entity = $form_state->getFormObject()->getEntity();
    $old_values = $old_entity->get('field_name')->getValue()

    $new_values = $form_state->getValue('field_name');

    // Manipulate and store desired values to be save here.
    $to_save_value = ['a', 'b', 'c'];
    $form_state->setValue('field_name', $to_save_value);
}
1
votes

Use hook_ENTITY_TYPE_presave, like so:

function yourmodulename_node_presave(Drupal\node\NodeInterface $entity) {
    if ($entity->getType() == 'your_content_type') {
       $entity->setTitle('Hello');
       $entity->set('body', 'this is body');
    } 
}

This is the best solution, because with hook_form_alter like MilanG you will be changing the value only when the node is saved from the particular form you are altering! If the node is saved programmatically from within the code or by some other method your hook_form_alter will not kick in.