October CMS - extend page to have an associated image

- 1 answer

Ad

What are the best and clean possible ways to extend functionality for including an associated image to each page object (CMS page or StaticPage), adding a corresponding upload widget to the page's configuration in the OctoberCMS backend?

(Additionally, and just as an aside: In what way can one add an associated image to a site's theme?)

Ad

Answer

Ad

For CMS pages, add a register() method to your Plugin.php file:

public function register()
{
    Event::listen('backend.form.extendFields', function($widget) {
        if (!$widget->model instanceof \Cms\Classes\Page) return;

        $widget->addTabFields([
            'settings[image_filet]' => [
                'label' => 'Page image',
                'tab' => 'Image',
                'field' => 'settings[image_file]',
                'type' => 'mediafinder',
                'comment' => 'Image url is available via the page settings under the name of image_file'
            ],
        ], 'primary');
    });
}

And the value is available via this.page.image_file Twig value

Ad
source: stackoverflow.com
Ad