Joomla 4 Create a new user

448 views
Skip to first unread message

Paul Swingewood

unread,
May 20, 2022, 10:32:31 AM5/20/22
to Joomla! General Development
Hi. I have a form that I want to use to create a new Joomla user.

Explanation:
User arrives at a form applying for a market stall. Here, they enter all the information required which just happens to include their name and email address. I'd like to use this information to then create them as a user in Joomla (rather than having to use the built in form).

So I was thinking I could use com_users and use the registration method. Joomla would then handle everything for me :)

The code.

All sitting in the FormModel

class MarketHelper
{
    /**
    *   PAUL get the component model
    **/
    public static function getModel($name, $path = JPATH_COMPONENT_ADMINISTRATOR, $component = 'com_market')
    {
        // load some joomla helpers
        JLoader::import('joomla.application.component.model');
        // load the model file
        JLoader::import( $name, $path . '/model' );
        // return instance
        return JModelLegacy::getInstance( $name, $component.'models' );
    }

    /**
    * PAUL Create the user array
    */
    public static function createUser($new)
    {
        // load the user regestration model
        $model = self::getModel('registration', JPATH_ROOT. '/components/com_users', 'Users');

    // JLoader::import('registration', JPATH_ROOT. '/components/com_users', 'Users');
        $password = ApplicationHelper::getHash(UserHelper::genRandomPassword());
        $config = ComponentHelper::getParams('com_users');
        $defaultUserGroup = $config->get('new_usertype', 2);
        $userData = array(
            "name" => $data['forename'].' '.$data['surname'],
            "username" => $data['forename'].' '.$data['surname'],
            "password" => $password,
            "password2" => $password, //confirm password
            "email" => $data['emailaddress'],
            "sendEmail" => 1,
            "groups" => array($defaultUserGroup));
        // register the new user
        $userId = $model->register($data);

        // if user is created
        if ($userId > 0)
        {
                return $userId;
        }
        return $model->getError();
    }
}

and further down in the model when the form is saved

public function save($data)
    {
       blah blah blah ......
       if ($table->save($data) === true)
        {

          // setup new user - This is using the market helper methods!
            // JLoader::register('MarketHelper',dirname(JPATH_COMPONENT.DS.'helper'.DS.'MarketHelper.php'));
      $newUser = array(
      'username' => $validData['username'],
      'name' => $validData['name'],
      'email' => $validData['email']);
       $userId = MarketHelper::createUser($newUser); // PAUL Why can't it find this class??
       if (!is_int($userId))
       {
         $this->setMessage($userId, 'error');
       }
       
blah blah blah ....

I'm sure there's lots wrong here (am I putting this in the right place etc) but at this stage I get the following error :

Call to a member function register() on bool

Can anyone help me please.
I realise I could just get the $newUser array and spit it to the tables bt I want Joomla to process the registration 'properly'

Cheers - Paul

MarkRS

unread,
May 20, 2022, 12:38:17 PM5/20/22
to Joomla! General Development
I haven't tried this, but it looks much easier these days, I'd say.
I don't understand your line "All sitting in the FormModel". The helper should go in

/components/{yourComponent}/src/Helper

You're error message is because the model isn't being created, but you probably knew that.
I think you can get a user object in your helper with :

use Joomla\CMS\User;

class MarketHelper {

   public static createUser($detailsArray)
   {
       $newUser = new User;
(then eg)
       $newUser->name = $detailsArray['name'];
(&tc)
       $newUser

(d*mn. This thing really has no "code" formatting!)

Possibly with some of the other things you've got there, like setting a random password, presumably in collaboration with marking it as needing changing by the user.
As I say, I haven't tested this.
Reply all
Reply to author
Forward
0 new messages