Problem/Motivation

The following code is not allowing me to flag the user after saving the user account:

<?php

/**
 * @param \Drupal\Core\Entity\EntityInterface $user
 */
function mymodule_user_presave(Drupal\Core\Entity\EntityInterface $user) {

  $flag_id = 'my_flag';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $flagging = $flag->isFlagged($user);
  if (!$flagging) {
    $flag_service->flag($flag, $user, $user);
  }
}

It reaches inside the IF statement with $flag and $user correctly set.
After saving the user account no errors are shown. However the flagging is not added to the database.
When i manually flag the user, the flagging is correctly added to the database.

Not sure what i am doing wrong?

Regards,

Thomas

Comments

thomas.dutch created an issue. See original summary.

thomas.dutch’s picture

Title: Programmatically triggered flag() not working » Programmatically triggered flag() not saved
ivnish’s picture

Status: Active » Closed (cannot reproduce)