2

I have a content type with name hotel it have entity reference field that referenced to users entity, I want when admin create a node automatically created a user according to hotel email field and then set user entity reference field with that new created user.How can I do it automatically?

I create a rule with below structure but not work

{ "rules_create_user_based_on_hotel" : {
    "LABEL" : "create user based on hotel",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--hotel" : { "bundle" : "hotel" } },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "user",
            "param_name" : "[node:field_hotel_users]",
            "param_mail" : "[node:field_mail_hotel]"
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : { "data" : [ "node:field-user-ref" ], "value" : [ "entity-created" ] } }
    ]
  }
}
1
  • your mistake is in "data_set" : { "data" : [ "node:field-user-ref" ], "value" : [ "entity-created" ] , use [ "entity-created:uid" ] instead of it
    – Yuseferi
    Commented Dec 13, 2015 at 21:08

2 Answers 2

3

You can also handle it programmatically in your custom module.

function yourmodule_form_alter(&$form,$form_state,$form_id){
     global $user;

     if($form_id=='hotel_node_form'  && !isset($form['nid']['#value'])){
         $form['actions']['submit']['#submit'][] = '_hotel_user_register_submit';
     }
}

function _hotel_user_register_submit($form,$form_state){
    $uname=$form_state['values']['field_hotel_users']['und'][0]['value'];
    $uemail=$form_state['values']['field_mail_hotel']['und'][0]['email'];

    $new_user = array(
     'name' => $uname,
     'pass' => rand(1000000,9999999), // note: do not md5 the password
     'mail' => $uemail,
     'status' => 1,
     'init' => $uemail,
     'roles' => array(
        DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    ),
);
    $nuser=user_save('', $new_user);
    $node=node_load($form_state['values']['nid']);
    $node->field_user_ref['und'][0]['target_id']=$nuser->uid;
    node_submit($node);
    node_save($node);
}
4
  • Please, don't copy what already said in another answer; it is still visible, even if it is not the accepted one.
    – avpaderno
    Commented Dec 14, 2015 at 5:59
  • @kiamlaluno I tried to create comprehensive answer, I think doing this job with rules is better method but when he had problem with doing it ( or maybe because his settings and configuration) I try to suggest another method that I believe is not better solution, So try create comprehensive answer .
    – Yuseferi
    Commented Dec 14, 2015 at 6:24
  • 2
    @zhilevan no need to " create comprehensive answer". On the contrary, keeping solutions in separate answers helps people to vote for them separately, allowing future users to see community's opinion (good thing) and rewarding one who actually wrote particular solution first (really, really good thing).
    – Mołot
    Commented Dec 14, 2015 at 7:49
  • @Mołot ok my friend, tnx for your notice.
    – Yuseferi
    Commented Dec 14, 2015 at 8:09
4
  • Download the Rules module
  • Enable Rules and Rules UI

Add a new rule

  • React on event: After Saving New Content (it's listed under NODE)
  • Restrict by type: Hotel

Under Action

  • Add Action of Create New Entity >> ENTITY TYPE: USER >> For your EMAIL field, Ex: If you called your field "Email" then it's node:field-email. For user name, I guess you will also need to create a field in the node for that too. Then do same steps as e-mail

Note: You can always click on "DATA SELECTORS" to see all the tokens you can use.

  • Add Action of Set data value >> select your entity ref field (Ex: If you called your field "Test" then it's node:field-test) >> for Value use entity-created:uid
  • Add action of Send Account Email (listed under USER) >> Account: entity-created

You can modify the e-mail templates here /admin/config/people/accounts use tokens for your needs.

Let me know how this goes, no time to test this Rule.

0

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.