Daneel
Daneel

Hi all,

when I create a new page (using the static pages plugin), I have some basic properties for the page, like setting the layout, configuring whether it is hidden or not, hidden in navigation or not, and so on.

I would like to extend these properties with custom ones (checkboxes, radio boxes, dropdowns or even input fields), which the user can set when creating a page and later on modify if they change. These custom properties should be saved with the page.

What would be the best way to accomplish this? I'd really appreciate some pointers.

Many thanks and all the best, Michael

Daneel
Daneel

Hi again,

I have further investigated this and it seems that the right approach to extending the page properties would be to create a plugin which extends the static pages plugin, similar to how it is shown in this video for the user plugin - right?

https://vimeo.com/108040919

Many thanks and all the best, Michael

philipptempel
philipptempel

That is sort of correct. You do not need to create a new plugin that literally extends the static pages plugin. That is, you don't need to create a model MyStaticPagesModel extends StaticPagesModel because that won't file/class will never be used anyway.

What you would want to do is read the docs and extend the static pages model (i.e., RainLab\Pages\Classes\Page) by means that are exemplified in the docs.

In case of further questions, please, feel free to ask!

alxy
alxy

If you use the Static Pages plugin, you don't even need what philipptempel proposed. The plugin has a feature called Custom page fields and sounds like what you want: http://octobercms.com/plugin/rainlab-pages (In the Documentation tab, scroll down to the bottom.)

Daneel
Daneel

Hi guys!

Just a short update. Making some good progress here. Extending the available fields by listening for the "backend.form.extendFields" and then using addTabFields for extending works great.

What I'm now trying to figure out is how to get it to save my new fields. I looked around a bit and found the "onSave" method in the Pages IndexController. There it seems that a "pages.object.save" and a "object.save" Event is fired when fields are saved. I tried listening for these events in my plugin and dump()ing their parameters, but that somehow did not work.

If anyone could give me some pointers on how I get it to save my values, that would be really appreciated. I assume I'll need to write a migration to extend the Pages table (like in the Video above for the Users table), but what do I need to do to actually get the Pages pluging to save my new values together with the other values in this table?

Any pointers are really appreciated and many thanks for your help so far.

All the best, Michael

Daneel
Daneel

Got it!

To make the fields persistant, I just had to make sure to add them to the viewBag.

Works great, like this:

        $widget->addTabFields([
            'viewBag[headline]' => [
                'label' => 'Headline',
                'tab' => 'Additional Info',
            ], ...

Much easier than expected. Awesome.

Cheers, Michael

chocolata
chocolata

Hi, did you finally figure out how to make a static page variable option dynamic? I've been struggling with this. Could you maybe have a look at the issue I've posted here? https://github.com/rainlab/pages-plugin/issues/376

Thanks in advance!

pavsid
pavsid

For anyone wanting to extend the CMS Page properties, here's now (assuming you already have a custom plugin)...

    public function boot()
    {
        // Extend all backend form usage
        Event::listen('backend.form.extendFields', function($widget) {

            // Only for the CMS Index controller
            if (!$widget->getController() instanceof \Cms\Controllers\Index) {
                return;
            }

            // Only for the Page model
            if (!$widget->model instanceof \Cms\Classes\Page) {
                return;
            }

            // Add custom fields...
            $widget->addTabFields([
                'viewBag[canonical_url]' => [
                    'label'   => 'Canonical Url',
                    'type'    => 'text',
                    'tab'     => 'cms::lang.editor.meta'
                ],
                'viewBag[robots_noindex]' => [
                    'label'   => 'No Index?',
                    'type'    => 'checkbox',
                    'tab'     => 'cms::lang.editor.meta'
                ],
            ]);
        });
    }

You can then access these properties your layout/template with...

{{ this.page.canonical_url }}
{{ this.page.robots_noindex }}

Thanks @Daneel for the heads up about the viewBag ;)

Last updated

phplee
phplee

Hi, when i use this

 // Only for the CMS Index controller
        if (!$widget->getController() instanceof \Cms\Controllers\Index) {
            return;
        }

        // Only for the Page model
        if (!$widget->model instanceof \Cms\Classes\Page) {
            return;
        }

the fields don't load. if i remove it they do but causes error when loading menu items. i only want to extend form fields on the page not the menu.

thanks

BennoThommo
BennoThommo

@phplee If you're trying to load the fields to the Static Pages form, you would want to use the following:

        if (!$widget->getController() instanceof \RainLab\Pages\Controllers\Index) {
            return;
        }

        // Only for the Page model
        if (!$widget->model instanceof \RainLab\Pages\Classes\Page) {
            return;
        }
phplee
phplee

Thanks. that worked!

1-11 of 11