1

I am trying to insert a value into a custom field(field_custom_referral) which is created in the backend.When a user creating a account in our application, this particular custom field should be updated with custom value. Can anyone help me out here.

I tried below code in .module file but not working out:

function module_user_insert(Drupal\Core\Entity\EntityTypeInterface $entity) {
  $entity->set('field_custom_referral', '3');
  $entity->save();
}

Any suggestion will be appreciated.

1
  • What is the type of your field field_custom_referral ?
    – berramou
    Commented Apr 13, 2020 at 12:39

1 Answer 1

1

The hook_ENTITY_TYPE_insert in case of entity user expecting Drupal\user\UserInterface instance not Drupal\Core\Entity\EntityTypeInterface

change your hook to:

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function MYMODULE_user_insert(Drupal\user\UserInterface $user) {
  $user->set('field_custom_referral', '3');
  $user->save();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.