dudheit
dudheit

I try to extend the Rainlab.Pages plugin with a fileupload functionality:

use RainLab\Pages\Classes\Page as PageController;

public function boot() {

    Event::listen('backend.form.extendFields', function ($widget) {

        if (
            !$widget->getController() instanceof \RainLab\Pages\Controllers\Index ||
            !$widget->model instanceof PageController
        ) {
            return;
        }

        $widget->model->attachMany = [
            'attachments' => 'System\Models\File', 'order' => 'sort_order'
        ];

        $widget->addTabFields([
            'attachments' => [
                'tab' => 'Attachments',
                'label' => 'Files',
                'type' => 'fileupload'
            ]
        ]);
    });
}

But the model seems not to have the methods which are required by the fileupload: Call to undefined method RainLab\Pages\Classes\Page::hasRelation()

How can I add file attachment functionality to the Pages plugin?

Surahman
Surahman

Basically the Rainlab.Pages have Custom page fields. Have you try it before?

dudheit
dudheit

Thanks, yes, I tried that option before, but the upload field simply didn't show up. But maybe it was only a wrong syntax.

adc_91
adc_91

Have you managed to make it work? I'm with the same inconvenience as you and I can not solve it.

jan-vince
jan-vince

You cannot use fileupload with Custom page fields as it requires a database relation.

But you can write plugin to extend Page model.

In my Small Extensions plugin I have extended Blog Post model to add relation so you can grab the code and edit it to your needs.

So you have to:

  1. Extend Page model in your plugins's boot() method like this:

    $attachMany = ['photos' => 'System\Models\File', 'delete' => true];
  2. Extend Page form with this fileupload field like this:

    Event::listen('backend.form.extendFields', function($widget) {
    
    if (!$widget->getController() instanceof \RainLab\Pages\Controllers\Index) {
        return;
    }
    
    if (!$widget->model instanceof \RainLab\Pages\Classes\Page) {
        return;
    }
    
    $widget->addSecondaryTabFields([
        'photos' => [
            'label' => 'images',
            'type' => 'fileupload',
            'span' => 'full',
            'deferredBinding' => 'true',
            'tab' => 'Images'
            ]
        ]);
    }

This is just out of my head so please do not copy/paste this code. Look inside of my (or other) plugin and find your best solution!

webworx.studio
webworx.studio

Hi, did you manage to get this working? I don't know the proper syntax here's my code in Plugin.php

public function boot(){
        RoomModel::extend(function ($model){
            $model->attachMany = [
                'room_gallery' => 'System\Models\File'
            ];
            $model->addFillable([
                'room_gallery',
            ]);
        });
        RoomsController::extendFormFields(function($form, $model, $context){
            $form->addTabFields([
                'room_gallery' => [
                    'label' => 'Images',
                    'type' => 'fileupload',
                    'mode' => 'image',
                    'tab' => 'Gallery'
                ],
            ]);
        });
    }

I'm not sure if this is the right syntax of declaring $attachMany

$model->attachMany = [
    'room_gallery' => 'System\Models\File'
];

Thanks in advance.

1-6 of 6