1

I'm trying to programatically create a user (once the user submits a form through Sprout Forms). The user doesn't seem to appear in the Users section of the CP.

I've ensured the user is being added correctly - I can even see a row in the craft_users database table. Going directly to admin/users/349 (the ID of the newly added user) shows all the user details.

It's been suggested that I need to index the new element data - but not sure how to do this.

$content = $entry->getContent();

$user = new UserModel();
$user->username         = $content['email'];
$user->email            = $content['email'];
$user->unverifiedEmail  = $content['email'];
$user->firstName        = $content['firstName'];
$user->lastName         = $content['lastName'];
$user->newPassword      = $content['password'];
$user->pending          = true;

$success = craft()->users->saveUser($user);

if ($success) {
    craft()->search->indexElementAttributes($user);

    // Assign them to the default user group
    craft()->userGroups->assignUserToDefaultGroup($user);

    // Send the activation email
    craft()->users->sendActivationEmail($user);

    // Update the Element Index
    craft()->search->indexElementAttributes($user);
}
8
  • FWIW, craft()->search->indexElementAttributes() shouldn't be necessary here. If you look at the id column for the user in the craft_users table, do you have a matching elementId in craft_elements_i18n and id in craft_elements? – Brad Bell May 5 '15 at 19:16
  • Hey Brad - yeah I didn't think I'd need it. I have a matching row in both craft_elements_i18n and craft_elements. Both have enabled set to 1. – crawf May 5 '15 at 23:13
  • Probably worth glancing through craft\app\controllers\UsersController->actionSaveUser() just to make sure you're not missing a piece of info before craft()->users->saveUser() gets called. – Brad Bell May 5 '15 at 23:44
  • @BradBell, yep, thats where I started. Will have another look through! – crawf May 5 '15 at 23:58
  • @BradBell I've just copy/pasted the code from actionSaveUser(), even adding the private methods, and still the same result. Very strange. Are there any caches to be emptied or something? – crawf May 6 '15 at 8:38
2

For anyone's future benefit - this happens due to the fact I'm adding the user in the sproutForms.saveEntry hook. That happens after it messes around with the fieldContext and contentTable options for the Entry. As such, the user is getting saved to the wrong context, so it won't appear...

A quick workaround for this is to move the following lines in SproutForms_EntriesService.php:133-135 to lines 120. This resets the field context back before the hook is called.

if ($transaction !== null)
{
    $transaction->commit();

    SproutFormsPlugin::log('Transaction committed');
}

// Reset our field context and content table to what they were previously
craft()->content->fieldContext = $oldFieldContext;
craft()->content->contentTable = $oldContentTable;

Craft::import('plugins.sproutforms.events.SproutForms_OnSaveEntryEvent');

$event = new SproutForms_OnSaveEntryEvent(
    $this, array(
        'entry'      => $entry,
        'isNewEntry' => $isNewEntry,
        'event'      => 'saveEntry',
        'entity'     => $entry,
    )
);

craft()->sproutForms->onSaveEntry($event);
1
  • This has been fixed in Sprout Forms v1.0.1 – Ben Parizek Jun 28 '15 at 17:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.