Ad

Add Fileupload Field To OctoberCMS Backend Using $widget->addFields?

Trying to add a form field with type fileupload and mode image to a certain page using a plugin in OctoberCMS backend but getting an error. Text, dropdown, etc types work fine.

When I set the field's name to viewBag[photo] I get the error "Call to a member function hasRelation() on array" on line 81 of [path]/public/modules/backend/traits/FormModelWidget.php".

And when I set the name to just photo I get "Call to undefined method October\Rain\Halcyon\Builder::hasRelation()" on line 786 of [path]/public/vendor/october/rain/src/Halcyon/Builder.php".

use System\Classes\PluginBase;
use Event;

class Plugin extends PluginBase
{
    public function boot()
    {
        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;
            }

            switch ($widget->model->fileName) {
                case 'about.htm':
                    $widget->addFields([
                        'viewBag[photo]' => [
                            'label' => 'Photo',
                            'mode' => 'image',
                            'imageWidth' => '200',
                            'imageHeight' => '300',
                            'useCaption' => true,
                            'thumbOptions' => [
                                'mode' => 'crop',
                                'extension' => 'auto',
                            ],
                            'span' => 'auto',
                            'type' => 'fileupload',
                        ],
                    ], 'primary');
                    break;
            }

        });
    }
}
Ad

Answer

The fileupload type can't be added to a Static Page via addFields at the moment. The mediafinder type must be used for image uploads instead.

$widget->addFields([
    'viewBag[photo]' => [
        'label' => 'Photo',
        'mode' => 'image',
        'imageWidth' => '200',
        'imageHeight' => '300',
        'useCaption' => true,
        'thumbOptions' => [
            'mode' => 'crop',
            'extension' => 'auto',
        ],
        'span' => 'auto',
        'type' => 'mediafinder',
    ],
], 'primary');
Ad
source: stackoverflow.com
Ad