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

program.es
program.es

How do I set default image URL or default file URL for File upload widget? Please advice.

daftspunky
daftspunky

One way to approach this is to create a method on your model that looks for the image, or returns a default.

In model acme/blog/models/MyModel.php:

public $attachOne = [
    'logo' => \System\Models\File::class
];

public function getLogo()
{
    if ($settings->logo) {
        return $settings->logo->getPath();
    }

    return $this->getDefaultLogo();
}

public function getDefaultLogo()
{
    $logoPath = File::symbolizePath(Config::get('acme.blog::default_logo'));

    if ($logoPath && File::exists($logoPath)) {
        return Url::asset(File::localToPublic($logoPath));
    }

    return null;
}

In config acme/blog/config/config.php

<?php

return [
    // Some path relative to the directory
    'default_logo' => '~/themes/bonjour/assets/images/october.png',
];

Hope this helps

program.es
program.es

Hi @daftspunk,

Thank you for your reply. Your solution look good on Frontend. Is it possible to set default image in backend when create item at first time? Content Manager would like to see default image preset when create content.

program.es
program.es

I work around with mediafinder.

    main_background:
        label: Main Background
        type: mediafinder
        mode: image
        default: '/default/bg.png'

The widget in backend will show default image from URL: https://{MY_DOMAIN}/storage/app/media/default/bg.png

Last updated

daftspunky
daftspunky

Media finder is another good option!

program.es
program.es

daisykale6864639 said:

program.es said: scribble io

I work around with mediafinder.

   main_background:
       label: Main Background
       type: mediafinder
       mode: image
       default: '/default/bg.png'

The widget in backend will show default image from URL: https://{MY_DOMAIN}/storage/app/media/default/bg.png

As I know, this class I can use only for listing page titles and urls? Am I able to list variable of page? If yes, how? Your can able to list variable of page

I'm not quite sure about your question. But if you want to get image URL from your mediafinder type field. You could use {{ yourmodel.main_background |media }}.

Please find more information from https://octobercms.com/docs/markup/filter-media.

1-6 of 6

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