如何将图像字段添加到Custom Prestashop 1.6模块

I'm trying to add a custom field in prestashop that will handle an image upload and store the filename in the db in column 'logo'.

I've created the tables and added a column VARCHAR(255).

So far I've added to my model class the folllowing relevant code regarding this field.

<?php
class MyModel extends ObjectModel
{

    public static $definition = array(
        ....

        'multilang' => true,
        'multilang_shop' => false,
        'fields' => array(
           'logo' => [
                'type' => self::TYPE_STRING,
                'lang' => false,
                'validate' =>  null,
                'size' => 255,
            ],              
        ),
    );

    ...
    public $logo;
}

I've added to my admin controller the folllowing.

public function __construct()
{

    $this->fields_list = array(
    'logo' => array(
            'title' => $this->l('logo'),
            'width' => 'auto',
            'orderby' => false
        ),
    );
    parent::__construct();
}

public function init()
{
    parent::init();
}

public function initContent()
{
    return parent::initContent(); // TODO: Change the autogenerated stub
}

public function processAdd()
{
    return parent::processAdd(); // TODO: Change the autogenerated stub
}

public function processSave()
{
    return parent::processSave(); // TODO: Change the autogenerated stub
}


public function renderForm()
{
    if (!($obj = $this->loadObject(true)))
        return;



    $this->fields_form = array(
        'legend' => array(
            'title' => $this->l('Stores'),
            'icon' => 'icon-home',
        ),
        'input' => array(
            ...
            array(
                'type' => 'file',
                'label' => $this->l('Logo'),
                'name' => 'logo',
                'display_image' => true,
                'image' => $image_url ? $image_url : false,
                'size' => $image_size,
                'hint' => $this->l('Storefront picture.'),
            ),
            ...
        ),
        'submit' => array(
            'title' => $this->l('Save'),
        ),
    );

    $this->fields_form['buttons'] = array(
        'save-and-stay' => array(
            'title' => $this->l('Save and stay'),
            'name' => 'submitAdd' . $this->table . 'AndStay',
            'type' => 'submit',
            'class' => 'btn btn-default pull-right',
            'icon' => 'process-icon-save',
        ),
    );

    return parent::renderForm();
}
}

The image selector pops up and I can select an image in the admin area when I try to add a new MyModel Item. The item is also saved correctly and all the other fields go to their respective tables and columns

The column that's supposed to hold the image name stays empty though and no file is uploaded anywhere.

The documentation is severely lacking in handling image uploads in modules so any idea what I am missing?

Do I need to handle upload manually or is there an interface I could use?

--

I was able to find a way to upload the images in my case.

In case anyone else is looking for this

Step 1.

In controller declare the following variable and make sure the directory exists.

    $this->fieldImageSettings = [];
    $this->fieldImageSettings['logo'] = array(
        'name' => 'logo',
        'dir' => self::IMG_DIR_NAME,
    );

Step 2.

In the controller override the function so that it handles file extension and saves the value in the correct model property.

        protected function uploadImage($id, $name, $dir, $ext = false, $width = null, $height = null)
    {
        $pathInfo = pathinfo($_FILES[$name]['name']);
        $ext = $pathInfo['extension'];
        $this->imageType = $ext;
        parent::uploadImage($id, $name, $dir, $ext, $width, $height); // TODO: Change the autogenerated stub
        $this->object->logo = $this->id_object . '.' . $this->imageType;
        if (!(count($this->errors) > 0)) {
            $this->object->save();
            return true;
        }
    }

Step 3

In order to show the image in the form add the following to renderForm function

        $image = self::IMG_DIR . $obj->logo;
        $image_url = ImageManager::thumbnail($image, $this->table . '_' . (int)$obj->id . '.' . $this->imageType, 150,
            $this->imageType, true, true);
        $image_size = file_exists($image) ? filesize($image) / 1000 : false;
        $this->fields_form = array(
            ...
            'input' => array(
                array(
                    'type' => 'file',
                    'label' => $this->l('logo'),
                    'name' => 'logo',
                    'display_image' => true,
                    'image' => $image_url ? $image_url : false,
                    'size' => $image_size,
                    'hint' => $this->l('Storefront picture.'),
                    'delete' => true,
                ),

I'd appreciate any input as to whether this is the proper way or a safe way or if you can spot any problems with this implementation.

PS: So far i wasn't able to find a way to be able to delete current image in the admin form.