How to change user field value after node form is submitted? I currently have a user field called field_reputation with a default integer value of 0 (if a user registeres).



The idea is that this field increases when certain actions are executed. For example, when a user publishes a new article, field_reputation should be its current value + 10. 

I am trying to achieve this using a form hook:

function MYTHEME_form_alter( &amp;$form, &amp;$form_state, $form_id ) {
  if ( TRUE === in_array( $form_id, array( 'node_article_form') ) ) {
    foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' &amp;&amp; isset($form['actions'][$action]['#type']) &amp;&amp; $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = '_increase_reputation';
        }
      }
} 


And then this function is called:

function _increase_reputation(array &amp;$form, FormStateInterface $form_state) {
  // Do something here
}


However I can't seem to figure out what code should be written in this function.
As @Clive says, form submit handler is a bit too late. Do it in hook_node_presave or hook_entity_presave. If you do it in hook_node_presave:


Check the node type first: if ($node-&gt;bundle() === 'article')
Get the current user:
$user = User::load(\Drupal::currentUser()-&gt;id());
Then set the field value: $value = (int) $user-&gt;get('field_reputation')-&gt;getString() + 10; $user-&gt;set('field_reputation', $value); (I'm not 100% sure on this code, but I'm sure there are better ways to do it when you have more than 5 minutes :D. Feels dirty to use getString() and then type-cast it into int)
Save the user entity with $user-&gt;save();


If you use hook_entity_presave then you just need to check for the entity type additionally: $entity-&gt;getEntityType-&gt;Id() === 'node'