How to compare before and after state in whatever_submit($form, &$form_state)

drupal.stackexchange https://drupal.stackexchange.com/questions/273900

  •  06-02-2021
  •  | 
  •  

سؤال

In a custom module, I have a form allowing some Create/Update/Delete data in a specific table of the database.
In the submit part of the code, I'd like to compare 'before' state (what was displayed to the user) to 'after' state (what he has actually entered) in order to properly update the database: create what has been added, update what has been changed and delete what has been removed. How can I do that?
By the way, the site is running Drupal 6.37.

هل كانت مفيدة؟

المحلول

First of all: You should migrate to Drupal 7 or 8. Drupal 6 is no longer supported as of Feb 24th, 2016. If migration is not possible, perform security updates. The latest version of Drupal 6 is 6.38 released on 24 February 2016. Have you also heard about the Drupal 6 LTS initiative? I highly recommend reading the details https://www.drupal.org/project/d6lts, https://github.com/d6lts/drupal.

Answering your question, the simplest thing would be to pass the object with the data in the form:

$form['my_object'] = array(
  '#value' => $my_object,
  '#type' => 'value',
);

$form['some_field'] = array(
  '#default_value' => $my_object->some_field,
  '#type' => 'textfield',
);

Then in the submit handler you can compare values:

if ($form_state['values']['my_object']->some_field != $form_state['values']['some_field']) {
  // Value of some_field has changed!
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى drupal.stackexchange
scroll top