I'm using Drupal 8

There are field_a and field_b. I'd like to field_b have the same value with field_a but different format after user submit his user edit page.


 $data = $account->field_a->value; // There is no value, since this user did not save yet. This is the problem.
 $account->field_b->value=format($account->field_a);//format function will change the format of field_a value, and this is not a problem

Since the user did not save yet, so there is no value for field_a. So I am trying to use drupal_register_shutdown_function as below.



 $data = $account->field_a->value;
 $account->field_b->value=format($account->field_a); //format will change the format of field_a value.

Now the field_a got the value. But field_b value will not be saved since drupal_register_shutdown_function.

What should I do?


  • The first version of the presave hook should work. You can access the field values before the user is saved. What field type is field_a? – 4k4 Feb 23 '17 at 12:46
  • "Since the user did not save yet, so there is no value for field_a". This makes no sense to me, why wouldn't it? in case of user, even the ID is available then. – Berdir Feb 23 '17 at 19:45
  • Hey @4k4 and Berdir. Thanks for your reply, guys! 4k4, text field for both of field_a and field_b. I'm pretty sure it does not work. Trust me I have tried a lot of time. It only can access the old value. For example, the old value of field_a is 2016, I'm trying to change it to 2017 and after I tap save, the field_b will get the 2016 (old value) rather than 2017 (new value). It does make sense. The value will be saved after presave executing over, but field_b tried to access the value when presave executing, so it gets the old value for sure (the new value not save yet). – DHH Feb 28 '17 at 3:49
  • @Berdir please see the above – DHH Feb 28 '17 at 3:50
  • No, that makes no sense. The entity in presave is what is being saved, not the old values. The hook is "Thing is the thing I am saving now, do you want to change something". What @4k4 wrote should definitely work and I've done things like that many times. – Berdir Feb 28 '17 at 10:24

To change a field according to another field:

function mymodule_user_presave($account) {

 $data = $account->field_a->value;
 $account->field_b->value = strtoupper($data);

 drupal_set_message($account->original->field_a->value . ' > ' . $account->field_a->value);
 drupal_set_message($account->original->field_b->value . ' > ' . $account->field_b->value);


To get the original values you would have to use $account->original. Otherwise you get the new values. To prove that it's working this is the output in messages from the code above after I entered abcdef in field_a:

abc > abcdef


The changes have been saved.

  • Sorry @4k4 I made a mistake and you guys were totally right, I was wrong! The value did not save because a function in my code. I have fixed it. Thanks for help a lot! – DHH Mar 13 '17 at 8:47

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.