I am working on a Drupal site that models a Family content type with entity references to Children and Parent content types. Families are entered in a single form, and I am using the inline entity form module (IEF) to enable entry of parent and child information in the same form. My goal is to allow anonymous users to create a user account by filling out this form. Currently, I have a custom module that implements hook_form_FORM_ID_alter and adds a function to the submit action that creates a new user account and sets the owner of the outer form node (the Family node in this case):
function myModule_form_node_family_form_alter(&$form, &$form_state, $form_id){
#register function on submit as that's when the node id of the created entity is available
$form['actions']['submit']['#submit'][] = 'signup_submit';
}
function signup_submit($form, &$form_state){
#get node id of newly created entity
$nid = $form_state->getValue('nid');
#create user
$email = $form_state->getValue('field_email_address')[0]['value'];
$user = \Drupal\user\Entity\User::create();
$user->setEmail($email);
$user->enforceIsNew();
$user->setPassword(getRandomPassword());
$user->setUsername(genUsername($nid));
if($res){
#set user to be owner of newly created node
dpm("User ".$user->id()."created successfully!");
$node = node_load($nid);
$node->setOwner($user);
$node->save();
}
}
I was hoping to do the same thing for the nodes created with IEF using a similar pattern:
function myModule_inline_entity_form_entity_form_alter(&$entity_form, &$form_state){
$entity_form['actions']['submit']['#submit'][] = 'inline_submit';
}
function inline_submit(&$entity_form, &$form_state){
$nid = $form_state->getValue('nid');
dpm("nid of inline entity: ".$nid);
}
... But it doesn't seem to work. If anyone knows when the nodes are actually created and how I could access the node id's so I can change the ownership, I would be greatly appreciative!
I ended up finding a solution. For inline forms, the content is not actually submitted until the full form is, so I can just grab the nids from the referenced entities on the form submit hook for the outer form: