Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By cofaure on
Hi !
I'm trying to create user who has more than 1 team. And the field "team" is referenced to a content "team".
My code works execpt for the field_team, who is the referenced field. So... I d'ont know how to do it :
$user = User::create();
$user->setPassword("");
$user->enforceIsNew();
$user->setEmail($row['mail']);
$user->setUsername(($row['login']));
$user->addRole($role);
if(!empty($row['adress']))
{$user->field_adresse = $row['adress'];}
if(!empty($row['name']))
{$user->field_nom = $row['name'];}
if(!empty($row['firstname']))
{$user->field_prenom = $row['firstname'];}
if(!empty($row['phone1']))
{$user->field_telephone = $row['phone1'];}
if(!empty($row['staff_corps']))
{$user->field_statut = $row['staff_corps'];}
if(!empty($equipesdumembre))
{$user->field_team = $team;}
$user->status = 1;
$user->save();
$team is an array of team's like [0]->team1 [1]->team5 ... And the user has to get those teams