Ad

Extends User Plugin By Adding A Profile Does Not Render Tab Either New Added Fields In OctoberCMS

- 1 answer

I've follow all the steps on the Extending User plugin screencast but for some reason I can not see "Profile" tab and either new added fields. Since I used the second approach, the easy one, this is what I've done:

  • Create the plugin and models and so on under Alomicuba namespace
  • Create and make the needed changes to the files as explained in video:

    Plugin.php
    
    <?php namespace Alomicuba\Profile;
    
    use System\Classes\PluginBase;
    use RainLab\User\Models\User as UserModel;
    use RainLab\User\Controllers\Users as UsersController;
    
    /**
     * Profile Plugin Information File
     */
    class Plugin extends PluginBase
    {
        public $requires = ['RainLab.User'];
    
        /**
         * Returns information about this plugin.
         *
         * @return array
         */
        public function pluginDetails()
        {
            return [
                'name'        => 'Profile',
                'description' => 'Add extra functionalities for Alomicuba WS by extends RainLab User',
                'author'      => 'DTS',
                'icon'        => 'icon-users'
            ];
        }
    
        public function boot()
        {
            UserModel::extend(function($model){
                $model->hasOne['profile'] = ['Alomicuba\Profile\Models\Profile'];
            });
    
            UsersController::extendFormFields(function ($form, $model, $context){
                if ($model instanceof UserModel)
                    return;
    
                $form->addTabFields([
                    'pinCode' => [
                        'label' => 'PIN',
                        'tab' => 'Profile'
                    ],
                    'phone2' => [
                        'label' => 'Teléfono (2)',
                        'tab' => 'Profile'
                    ],
                    'phone3' => [
                        'label' => 'Teléfono (3)',
                        'tab' => 'Profile'
                    ],
                    'phone4' => [
                        'label' => 'Teléfono (4)',
                        'tab' => 'Profile'
                    ]
                ]);
            });
        }
    }
    
    add_profiles_fields_to_user_table.php
    
    <?php namespace Alomicuba\Profile\Updates;
    
    use Schema;
    use October\Rain\Database\Updates\Migration;
    
    class AddProfilesFieldsToUserTable extends Migration
    {
    
        public function up()
        {
            Schema::table('users', function($table)
            {
                $table->integer('pinCode')->unsigned();
                $table->dateTime('pinCodeDateTime');
                $table->integer('phone2')->unsigned()->nullable();
                $table->integer('phone3')->unsigned()->nullable();
                $table->integer('phone4')->unsigned()->nullable();
            });
        }
    
        public function down()
        {
            $table->dropDown([
                'pinCode',
                'pinCodeDateTime',
                'phone2',
                'phone3',
                'phone4'
            ]);
        }
    }
    
    version.yaml
    1.0.1: First version of Profile
    1.0.2:
        - Created profiles table
        - create_profiles_table.php
        - add_profiles_fields_to_user_table.php
    
    Profile.php (Model)
    <?php namespace Alomicuba\Profile\Models;
    
    use Model;
    
    /**
     * Profile Model
     */
    class Profile extends Model
    {
        /**
         * @var string The database table used by the model.
         */
        public $table = 'alomicuba_profile_profiles';
    
        /**
         * @var array Relations
         */
        public $belongsTo = [
            'user' => ['RainLab\User\Models\User']
        ];
    
    
        // This method is not need anymore since I'll use the second approach
        public static function getFromUser($user)
        {
            if ($user->profile)
                return $user->profile;
    
            $profile = new static;
            $profile->user = $user;
            $profile->save();
    
            $user->profile = $profile;
    
            return $profile;
        }
    }
    

But when I edit a existent user I didn't see the 'Profile' tab and also didn't see any new added field. See image below:

enter image description here

Any advice around this? Did I miss something?

Also I have a few question around plugin extends:

  1. How do I add a required field to the register form?
  2. How do I display each new added field on the account form?
Ad

Answer

I haved tested your code on my machine you need to write

$require instead of $requires in plugin.php

please check documentation

http://octobercms.com/docs/plugin/registration#dependency-definitions

and when extendFormFields() method called for UserController you need to specify that you only want to extends fields for UserModel not for other

if (!$model instanceof UserModel)
    return;

so plugin.php code look like this

<?php namespace Alomicuba\Profile;

use System\Classes\PluginBase;
use RainLab\User\Models\User as UserModel;
use RainLab\User\Controllers\Users as UsersController;

/**
 * Profile Plugin Information File
 */
class Plugin extends PluginBase
{
    public $require = ['RainLab.User'];

    /**
     * Returns information about this plugin.
     *
     * @return array
     */
    public function pluginDetails()
    {
        return [
            'name'        => 'Profile',
            'description' => 'Add extra functionalities for Alomicuba WS by extends RainLab User',
            'author'      => 'DTS',
            'icon'        => 'icon-users'
        ];
    }

    public function boot()
    {
        UserModel::extend(function($model){
            $model->hasOne['profile'] = ['Alomicuba\Profile\Models\Profile'];

        });

        UsersController::extendFormFields(function ($form, $model, $context){
            if (!$model instanceof UserModel)
                return;

            $form->addTabFields([
                'pinCode' => [
                    'label' => 'PIN',
                    'tab' => 'Profile'
                ],
                'phone2' => [
                    'label' => 'Teléfono (2)',
                    'tab' => 'Profile'
                ],
                'phone3' => [
                    'label' => 'Teléfono (3)',
                    'tab' => 'Profile'
                ],
                'phone4' => [
                    'label' => 'Teléfono (4)',
                    'tab' => 'Profile'
                ]
            ]);
        });
    }
}

enter image description here

and in add_profiles_fields_to_user_table.php

for dropping column write following code

Schema::table('users', function($table)
{
        $table->dropDown([
            'pinCode',
            'pinCodeDateTime',
            'phone2',
            'phone3',
            'phone4'
        ]);
}
Ad
source: stackoverflow.com
Ad