I’m looking to try and add some sort of record finder as a static page variable.
My plugin has a Group model, and each group has a relation to another Event model.
On my static page layout I’d like to be able to choose a Group, so I can list the events of this group on the page.
I’ve added this as a page variable to my layout.
{variable name="group" tab="Events" type="dropdown"}{/variable}
But when I try and edit the page in the admin area I see this error.

Is there an event that I can listen for that allows me to populate this dropdown with the available Groups?
I figured it out with this bit of a code in my plugin’s Plugin.php
boot method.
function boot()
{
/**
* Adds options to "scheduleGroup" variable on static page layouts
*/
\RainLab\Pages\Classes\Page::extend(function($model) {
$model->addDynamicMethod('getScheduleGroupOptions', function() {
return \My\Plugin\Models\Group::landingPageGroupsDropdownOptions();
});
});
}
Then in My\Plugin\Models\Group
model I have this method.
/**
* Gets a list of groups as options for a dropdown
* Used primarily for staticPage dropdown
* variables on page layouts
*
* @return array<string>
*/
public static function landingPageGroupsDropdownOptions(): array
{
$groups = self::select('id', 'title')->get();
$return = [];
foreach($groups as $group) {
$return[$group->id] = $group->title;
}
return $return;
}
My variable definition in my layout file looks like this…
{variable name="scheduleGroup" tab="Content" label="Events and Training" type="dropdown" emptyOption="None" commentAbove="Choose a schedule group to display on the page"}{/variable}
And in the php section of the same layout file, I use the selected group id to get the list of events from the group that I want to display to make them available in the markup.
<?php
function onEnd() {
if ($this->staticPage->scheduleGroup) {
$groupId = $this->staticPage->scheduleGroup;
$group = \My\Plugin\Models\Group::with(['events'])->find($groupId);
$this['groupItems'] = $group->events;
}
}
?>
And in the markup
{% if groupItems %}
<div class="container mx-auto px-30 py-70">
<div class="md:grid md:grid-cols-2 lg:grid-cols-3 gap-40">
{% for item in groupItems %}
{% partial 'groupItem' item=item %}
{% endfor %}
</div>
</div>
{% endif %}
There’s probably a more graceful way of me getting the events to display on the page but this works for me so I’m going with it. 
4 Likes