1

I'm using static page plugin. I need a way to get from a custom field all the pages of the octobercms. I don't care how this will be done (official page url selector - dropdown list ...), I want only to be user friendly so can be used from a client.

There is a workaround but it works only with repeater. If I use it with one other field {variable name="page" label="Page" type="dropdown"}{/variable} I got an error:

get_class() expects parameter 1 to be object, array given
Panagiotis Koursaris
  • 3,794
  • 4
  • 23
  • 46
  • I have checked and error is occurring, its weekend so little busy with family, will surely solve this tomorrow and post solution. – Hardik Satasiya Jan 14 '18 at 09:01

1 Answers1

3

Wow! this question really tighten my brain's nuts and bolts,

Thanks for the Question, I really enjoyed it to solve.

As if we use normal variable outside of the repeater October field parse will treat it as name as viewBag[page] and it is not working with fieldParser and fieldOptions getter methods. (its little technical so I would like to skip this and jump to real solution)

any how at last I found little hack/kind of extension/functionality (extensive nature of October Cms) and use it

you need any plugin so you can write extension code inside boot method.

public function boot() {
    $alias = \Illuminate\Foundation\AliasLoader::getInstance()
              ->alias('MyPluginOptionAlias','October\\Demo\\plugin');
}

here you can correct alias-name and namespace we are making our plugin object globally available using this alias (MyPluginOptionAlias)

and inside plugin.php we add two static methods so we can access them later, this will be just logic of getting page-list and return it as an array.

public static function getPageOptions() {

    // different lists // or even you can merge them
    $result =  self::getTypeInfo('cms-page');
    // $result =  self::getTypeInfo('static-page');
    // $result =  self::getTypeInfo('blog-post');
    return $result['references'];
}


public static function getTypeInfo($type)
{
    $result = [];
    $apiResult = \Event::fire('pages.menuitem.getTypeInfo', [$type]);

    if (is_array($apiResult)) {
        foreach ($apiResult as $typeInfo) {
            if (!is_array($typeInfo)) {
                continue;
            }

            foreach ($typeInfo as $name => $value) {
                if ($name == 'cmsPages') {
                    $cmsPages = [];

                    foreach ($value as $page) {
                        $baseName = $page->getBaseFileName();
                        $pos = strrpos($baseName, '/');

                        $dir = $pos !== false ? substr($baseName, 0, $pos).' / ' : null;
                        $cmsPages[$baseName] = strlen($page->title)
                            ? $dir.$page->title
                            : $baseName;
                    }

                    $value = $cmsPages;
                }

                $result[$name] = $value;
            }
        }
    }

    return $result;
}

Now the main thing

{variable name="page" label="Page" type="dropdown" options="MyPluginOptionAlias|getPageOptions" tab="link"}{/variable}

you define options exactly like this MyPluginOptionAlias|getPageOptions

Logic here is that now it will fetch option list from our MyPluginOptionAlias instance's method: getPageOptions

what ever array you return from it will be listed as options for this drop-down.

please comment if it creates any issue, we will correct it.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40