Extends a custom plugin by adding a profile that does not display a tab on newly added fields in OctoberCMS

I followed all the steps on Extending User Plugin , but for some reason I can't see the Profile tab and the newly added fields. Since I used the second approach, lightweight, this is what I did:

  • Create plugin and models, etc. under the namespace Alomicuba
  • Create and make the necessary changes to the files, as described in the 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 an existing user, I didn't see the Profile tab, nor did I see any new added field. See image below:

enter image description here

Are there any tips on this? Did I miss something?

Also I have a few questions about plugin extension:

  • How do I add a required field to the registration form?
  • How to display each new added field in the account form?
+3


source to share


1 answer


I checked your code on my machine, you need to write

$require

instead of $requires

in plugin.php file

check the documentation

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

and when the extendFormFields () method is called for the UserController, you need to indicate that you only want to extend the fields for the UserModel and not others

if (!$model instanceof UserModel)
    return;

      



so the plugin.php code looks 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

to output the column write the following code

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

      

+3


source







All Articles