0

Using drupal 8.7.x

I'm trying to create a vocabulary that references the user that creates the term

The settings of the user reference field are the following.

User reference field on taxonomy

user refernce field settings The taxonomy term is created using a form in a custom module.

public function submitForm(array &$form, FormStateInterface $form_state) {
     $ingredient = $form_state->getValue('ingredient');


     $current_user = \Drupal::currentUser();
     $current_user_id = $current_user->id();

     $ingredient_term = \Drupal\taxonomy\Entity\Term::create([
       'vid' => 'ingredient',
       'name' => $ingredient,
       'field_user' => $current_user_id,
     ]);

     $ingredient_term->enforceIsNew();
     $ingredient_term->save();
   }
}

The ingredient is saved but the user is just saved as NULL. Am I referencing the user the wrong way by using the ID? I already tried by using the $current_user variable and by using the getAccountName and getDisplayName methods instead of id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.