OctoberCMS Static Page插件自定义字段获取全站页面URL及报错咨询
Got it, let's tackle this OctoberCMS Static Pages plugin issue you're facing. That get_class() expects parameter 1 to be object, array given error pops up because the dropdown variable field isn't receiving the proper data format when used outside a Repeater. The Repeater works because its context automatically handles the data structure, but standalone needs a bit of tweaking. Here are a few user-friendly solutions to let your clients easily select pages:
Option 1: Use the Official Page Picker (Most User-Friendly)
If your Static Pages plugin version supports it, skip the dropdown entirely and use the built-in page field type. This is designed specifically for selecting pages, and it renders an intuitive searchable interface that your clients will love:
{variable name="selected_page" label="Choose a Page" type="page"}{/variable}
No extra code needed—this field lets users browse, search, and select any page in your system with zero hassle.
Option 2: Fix the Dropdown Field with Custom Data Source
If you prefer using a dropdown, you just need to explicitly pass it the right page data in a format it understands. Here's how:
- Go to your Static Page's Code tab and add this PHP snippet to fetch all pages as a key-value array:
function onInit() { // Grab all static pages, format them for dropdown (ID as key, title as label) $this['pageOptions'] = \RainLab\Pages\Models\Page::all()->pluck('title', 'id')->toArray(); } - Update your custom field to use this prepped data source:
{variable name="selected_page" label="Choose a Page" type="dropdown" options="{{ pageOptions }}"}{/variable}
Now the dropdown will load all page titles, and selecting one will store the corresponding page ID for you to use in your template.
Option 3: Stick with the Repeater Workaround (Quick Fix)
Since you already have a working Repeater-based solution, you can repurpose it for single-page selection by limiting the Repeater to one entry:
{repeater name="page_selector" label="Select a Page" prompt="Add Page" maxItems="1"} {variable name="page" label="Page" type="dropdown"}{/variable} {/repeater}
In your template, just pull the first (and only) entry like this:
{{ page_selector[0].page }}
It's a bit of a hack, but it gets the job done quickly if you don't want to mess with new fields or code.
I'd recommend Option 1 first—it's the most polished and user-friendly for your clients.
内容的提问来源于stack exchange,提问作者Panagiotis Koursaris

