This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Komak5740251
Komak5740251

I'm working on adding an additional image called offline_image to the User object by Rainlab. Rather than coding it with duct tape to upload the image to a directory and referencing the path, I would like to imitate the Avatar attribute as closely as possible.

public function boot()
{
    $this->extendUserModel();
    $this->extendUsersController();
}

protected function extendUserModel()
{
    UserModel::extend(function($model) {
        $model->hasOne = [
            'offline_image' => [\System\Models\File::class, 'key' => 'attachment_id', 'otherKey' => 'field']
        ];
    });
}
protected function extendUsersController()
{
    UsersController::extendFormFields(function($widget) {
        // Prevent extending of related form instead of the intended User form
        if (!$widget->model instanceof UserModel) {
            return;
        }

        $configFile = plugins_path('revurb/userdata/config/text_fields.yaml');
        $config = Yaml::parse(File::get($configFile));
        $widget->addTabFields($config);

        // Broken
        $configFile2 = plugins_path('revurb/userdata/config/image_fields.yaml');
        $config2 = Yaml::parse(File::get($configFile2));
        // $widget->addSecondaryTabFields($config2)
    });
}

text_fields.yaml

# ===================================
#  Extended Profile Field Definitions
# ===================================

offlineImage:
    label: revurb.userdata::lang.user.offlineImage
    type: fileupload
    mode: image
    imageHeight: 1920
    imageWidth: 1080

If I don't set the 'otherKey' to 'field', I get the Avatar image reference. With the field, I'm getting an error when uploading an image: Call to undefined method October\Rain\Database\QueryBuilder::isPublic()

How do I extend and process the upload process to ensure all fields are properly set?

mjauvin
mjauvin

shouldn't "offlineImage:" be "offline_image:" in text_fields.yaml ?

mjauvin
mjauvin

And I don't think "key" and "otherKey" are needed in your relation config.

Komak5740251
Komak5740251

mjauvin said:

shouldn't "offlineImage:" be "offline_image:" in text_fields.yaml ?

offlineImage was an old naming which was converted to offline_image. That's a paste error on my end and does not relate to the current state of the code, or the error message.

mjauvin said:

And I don't think "key" and "otherKey" are needed in your relation config.

Without "key", I get a SQL error for system_files.user_id column not found.

mjauvin
mjauvin

Sorry, I missed the actual issue here... use attachOne relation instead of hasOne

Komak5740251
Komak5740251

It looks like that was a partial fix.

$model->attachOne = [
    'offline_image' => 'System\Models\File'
];

I got an error: Model 'RainLab\User\Models\User' does not contain a definition for 'avatar'. Amending to the following seems to work, but needs to be adjusted to append, instead of replacing.

$model->attachOne = [
    'avatar' => 'System\Models\File',
    'offline_image' => 'System\Models\File'
];
Komak5740251
Komak5740251

Corrected, appending an attachOne is performed via:

$model->attachOne['offline_image'] = 'System\Models\File';

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.