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

bmcwilliams
bmcwilliams

When choosing the layout for a static page, the order I see is not sorted. Is it possible to change this so layout names or displayed on the pulldown menu in sorted order? I'm referring to where the user can choose the select for the viewBag[layout] variable. Makes everyone in my working group nuts!

thanks

mjauvin
mjauvin

I didn't find an easy way to do that. The ordering is done by layout fileName

bmcwilliams
bmcwilliams

I didn't find that to be true. The ordering for me is based on the date the layout file was created. I have these layouts:

cookie-policy.htm default.htm page-1col.htm page-2col.htm search.htm sitemap.htm thanks-info-request.htm

They display in this order on the layout menu: default.htm page-2col.htm sitemap.htm search.htm page-1col.htm thanks-info-request.htm cookie-policy.htm

which was the order they were created... I wish this would be improved to sort by your layout description.

bmcwilliams
bmcwilliams

I did some digging, don't know if i'm on the right track. viewBag[layout] seems to control the layout chosen for a page and in plugins/rainlab/pages/classes/page/fields.yaml we see this:

fields:
    viewBag[layout]:
        tab: cms::lang.editor.settings
        label: rainlab.pages::lang.page.layout
        type: dropdown
        options: getLayoutOptions

Is there a way to sort the options? If so, how?

mjauvin
mjauvin

You're right, it is by creation date...

The only way to change this is to add this to your plugin's boot method:

public function boot()
{
    \Event::listen('backend.form.extendFieldsBefore', function($formWidget) {
        if (!$formWidget->model instanceof \RainLab\Pages\Classes\Page) {
            return;
        }   
        $formWidget->tabs['fields']['viewBag[layout]']['options'] = 'getSortedLayoutOptions';
    }); 

    \RainLab\Pages\Classes\Page::extend(function($model) {
        $model->addDynamicMethod('getSortedLayoutOptions', function() use ($model) {
            $result = []; 
            $layouts = \Cms\Classes\Layout::listInTheme($model->theme, true)->sortBy('description');

            foreach ($layouts as $layout) {
                if (!$layout->hasComponent('staticPage')) {
                    continue;
                }   

                $baseName = $layout->getBaseFileName();
                $result[$baseName] = strlen($layout->description) ? $layout->description : $baseName;
            }   

            if (!$result) {
                $result[null] = \Lang::get('rainlab.pages::lang.page.layouts_not_found');
            }   

            return $result;
        }); 
    }); 
}

1-5 of 5

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