Add custom properties to uploaded file

Hi guys,
I am creating custom plugin for managing galleries for my client and I need to achieve one thing.

Basically I would like to use FileUpload form widget for single gallery, to upload the iamges. It works perfectly fine, I just need to extend it a little bit. Right now for single image I can set only “Title” and “Description”. I need to set some additional metadata for every picture. Or maybe just turn description into repeater. That would be totally fine too.

I am just not sure if this is doable in some easy way. From what I know I would probably need to copy whole form widget and also create new database table. Is there any easier way to achieve it?

Thank you in advance

what kind of “extension” u talking about? something like tags for seo or something?
i had in my head a one idea regarding to file uploads and images at all, to create an pictureset output for srcsets etc.

can u please share more about your idea what you will extend? maybe it will turn out as a nice feature request :wink:


from mine point of view, there are some ways… one way is fileupload, where is used system table for storing information, another is media library (there are mine older plugin for Oc.1) but… u know…


Quick info for you

  • Create migration for system_files with all fields, what u want to add
  • then, extend form widget like this
Event::listen('backend.form.extendFields', function($widget) {
    if($widget->model instanceof \System\Models\File) {                
        $widget->addFields([
            'lightbox' => [
                'label' => 'Open in lightbox',
                'comment' => 'Image will be wrapped in link and open in lightbox window.',
                'type' => 'checkbox'
            ],
            'resize' => [
                'label' => 'Resize',
                'comment' => 'Image will be resized',
                'type' => 'checkbox',
                'span' => 'auto'
            ],
            'crop' => [
                'label' => 'Crop',
                'comment' => 'Use "crop" mode while resizing',
                'type' => 'checkbox',
                'span' => 'auto'
            ],
            'target_width' => [
                'label' => 'Target width',
                'comment' => 'Size in pixels',
                'type' => 'number',
                'trigger' => [
                    'action' => 'show',
                    'field' => 'resize',
                    'condition' => 'checked'
                ],
                'span' => 'auto'
            ],
            'target_height' => [
                'label' => 'Target height',
                'comment' => 'Size in pixels',
                'type' => 'number',
                'trigger' => [
                    'action' => 'show',
                    'field' => 'resize',
                    'condition' => 'checked'
                ],
                'span' => 'auto'
            ]
        ]);
    }
});

hope, will help. this example will create a form with additional items for controlling image…
image

2 Likes

Hi @snipi,
well, something very similar to your example to be honest.

Basically I need richtext “Description” and some variable number of properties.


Actually, I was already able to do it with the info you provided. I am just adding complete code here for the seek of documentation:


I extended system_files table with new columns:

<?php namespace Silence\Galleries\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class UpdateSystemModelsFile extends Migration
{
    public function up()
    {
        Schema::table('system_files', function($table)
        {
            $table->text('properties')->nullable();
            $table->text('rich_description')->nullable();
        });
    }

    public function down()
    {
        Schema::table('system_files', function($table)
        {
            $table->dropColumn('properties');
            $table->dropColumn('rich_description');
        });
    }
}

After that I basically used your code, just extended it little bit as I needed new property to be jsonable:

public function boot()
    {
        File::extend(function($model){
            $model->addJsonable('properties');
        });

        Event::listen('backend.form.extendFields', function($widget) {
            if($widget->model instanceof File) {                
                $widget->removeField('description');

                // neccessary condition to avoid duplicating other then repeater fields
                if($widget->isNested === false) {
                    $widget->addFields([
                        'properties' => [
                            'label'     => 'Properties',
                            'type'      => 'repeater',
                            'form'      => [
                                'fields'    => [
                                    'property' => [
                                        'label' => 'Property',
                                        'type'  => 'text',
                                    ],
                                    'value' => [
                                        'label' => 'Value',
                                        'type'  => 'text'
                                    ],
                                ]
                            ]
                        ],
                        'rich_description' => [
                            'label'     => 'Description',
                            'type'      => 'richeditor'
                        ],
                    ]);
                }
            }
        });
    }

There was also small problem with repeater. By default, when I was creating new item in repeater, it copied also fields outside of the repeater. I had to to add addFields function inside if($widget->isNested === false condition in order to avoid this behaviour.

Solution looks like this:

Thank you for your fast help :slight_smile:

1 Like

only one thing… never delete columns in core tables dude…
then, remove that drop column from up() and create text in down() to revert…

and of course, change description from remove, to merge setting with "hidden" => true

2 Likes

Okey,
I was thinking about it if I need to remove it, if it is good practise, to be honest. Gonna change it and update also in previous message. Thank you for advice.

best practice is, not to change tables that is in use of core system. you can extend it, but not change defaults…

Yeah, that actually make sense :slight_smile:

1 Like

u must accept that fact, that you are not fully aware, where original core will reach for that removed field at all… but, nice work… only one question in my head, why repeater for properties? this makes things little bit dangerous for you in future

Thank you.

Well, it’s plugin for gallery of paintings. Those are not like programming properties but properties of those paintings which are basically text and can be different for every item. Therefor I chose repeater. Should I go some other way?

there are a question about, how many times you need to type-in these properties… if you need to repeat whole set of properties for 50 images, then… omg. repeater will kill you. imagine it. each image, for example you need for each one approx. 5 properties to fill in. and these are still same… (price, used technology, used paints, format of painting, etc.) … these are still same for each time and you need to type it in… overkill bro.