Issue when creating user programatically

Hi Drupal community,

I’m developping my own module to do the following:

  1. Anonymous user visit website and submit a form (with Webform)
  2. When submitting the form, it creates a new user
  3. When the user is created, it creates a new node type “ticket_support” which is linked to the Support Ticketing system module of Drupal

Currently, it works perfectly with logged user and it creates perfectly the new ticket.

But now, because I want my anonymous user able to create some tickets and able to track the issues they created, I need to create a user linked to the given email address in the form they submitted.

It should theorically work but it isn’t. I receive the following error when submitting my form:

EntityMetadataWrapperException : Unable to get the data property profile_id as the parent data structure is not set. dans EntityStructureWrapper->getPropertyValue() (ligne 457 dans C:wamp64wwwtechnimaniasitesallmodulesentityincludesentity.wrapper.inc).

I’m also using Drupal Commerce and I have Drupal Commerce Addressbook enabled, even if I disable the module the error keep showing.

It seems that I need to create the profile programatically BEFORE creating the user BUT to create a profile, I need a user ID (uid).

How can I manage that ? Am I doing it using the right way ? If I post here it’s because I spent much hours and I’m a little bit lost.

Here’s the main part of my code called in the “Webform result” which is in the “PHP type”(maybe it will be helpful):

function support_webform_create($webform, $nid, $sid) { 
	include_once drupal_get_path('module','webform') . '/includes/webform.submissions.inc';
	// Get the submission form_key and value
	$submission = webform_get_submission($nid, $sid);
	$data = $submission->data;
	$node = node_load($nid);
	
	$components = $node->webform['components'];
	$labels = array();
	foreach ($components as $key => $component) {
		$labels[$key] = $component['form_key'];	  
	}

	$fields = array();
	foreach ($data as $key => $values) {
	  $fields[$labels[$key]] = $values[0];
	}

  // Set the email value in variable
  $email = $fields['e_mail'];
  // We validate the email
  if(user_load_by_mail($email)) {
    // User exists, use the data available.
  } else {
   // User doesn't exist, create a new user
  
  	// Create a new password
    $password = user_password(8);

    // build the new user object
    $newuser = array(
		'name' => $email,
		'mail' => $email,
		'pass' => $password,
		'status' => 1,
		'init' => $email,
		'roles' => array(
		  DRUPAL_AUTHENTICATED_RID => 'authenticated user',
		),
	);
	
    // save the new user ===> HERE'S THE ERROR !!!!
    $account = user_save('', $newuser);
	
	// manually set password to display it in email
	$account->password = $newuser['pass'];
	
    // Send the e-mail through the user module.
	drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', '[email protected]'));	
  }
	

    if($webform == 'itservices') { 
		$ticket_title="Demande de devis pour services informatiques";
		$ticket_projectId = 3;
		$ticket_category = 'itservices';
		$ticket_type = 'quote';
    } elseif($webform == 'mobile') {
		$ticket_title="Demande de devis pour services mobiles";
		$ticket_projectId = 3;
		$ticket_category = 'mobile';
		$ticket_type = 'quote';
	} elseif($webform == 'website') {
		$ticket_title="Demande de devis pour site Web";
		$ticket_projectId = 3;
		$ticket_category = 'website';
		$ticket_type = 'quote';
	}
	
	$ticket_link = $GLOBALS['base_url'] . $GLOBALS['base_path'] . 'node/' . $nid . '/submission/' . $sid;

	// Construct the ticket content
	$ticket_content='Nouvelle demande de devis (' . $ticket_title . ') soumise par ' . $fields['firstname'] . ' ' . $fields['lastname'] . ' (' . $fields['e_mail'] . ')
Pour consulter le résumé du devis, visitez la page suivante: ' . $ticket_link;


	$user = user_load_by_mail($email);

	$node = new stdClass();
	$node->title = $ticket_title;
	$node->type = "support_ticket";
	node_object_prepare($node); 
	$node->language = LANGUAGE_NONE; 
	$node->uid = $user->uid;
	$node->name= $user->name;
	$node->status = 1; //(1 or 0): published or not
	$node->promote = 0; //(1 or 0): promoted to front page
	$node->comment = 2; // 0 = comments disabled, 1 = read only, 2 = read/write
	$node->state = 1; // 1 == Open
	$node->priority = 2; // 2 == Normal
	$node->client= 2; // Get the proper ID for your client
	$node->assigned = 'florian'; // A user's name can go here if you want to auto-assign
	$node->project = 3; // Get the proper ID for your project
	//$node->field_referrer['und'][0]['value'] =  $data['components']['referrer']['value'][0];
	$node->field_project_number['und'][0]['value'] = $fields['project_number'];
	$node->field_emailer['und'][0]['value'] =  $fields['e_mail'];
	$node->field_name1['und'][0]['value'] =  $fields['name'];
	$node->field_category['und'][0]['value'] = $ticket_category;
	$node->field_type['und'][0]['value'] = $ticket_type;
	$node->body['und'][0]['value'] =  $ticket_content;
	$node = node_submit($node);
	node_save($node);
}

THanks a lot for your much appreciated help.

If you need more info, I stay available.

Kind regards
Florian

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed