Allows to populate drop-down, radio- or checkbox list fields, which have been added with
cms:onExtendPageForm event.
Usually you do not need to use this event for fields which represent
data relations. But if you want a standard
field (corresponding an integer-typed database column, for example), to be rendered as a drop-down list, you should
handle this event.
The event handler should accept 2 parameters - the field name and a current field value. If the current
field value is -1, the handler should return an array containing a list of options. If the current
field value is not -1, the handler should return a string (label), corresponding the value.
public function subscribeEvents()
{
Backend::$events->addEvent('cms:onExtendPageModel', $this, 'extend_page_model');
Backend::$events->addEvent('cms:onExtendPageForm', $this, 'extend_page_form');
Backend::$events->addEvent('cms:onGetPageFieldOptions', $this, 'get_page_field_options');
}
public function extend_page_model($page, $context)
{
$page->define_column('x_color', 'Color');
}
public function extend_page_form($page, $context)
{
if ($context != 'content')
$page->add_form_field('x_color')->renderAs(frm_dropdown);
}
public function get_page_field_options($field_name, $current_key_value)
{
if ($field_name == 'x_color')
{
$options = array(
0 => 'Red',
1 => 'Green',
2 => 'Blue'
);
if ($current_key_value == -1)
return $options;
if (array_key_exists($current_key_value, $options))
return $options[$current_key_value];
}
}