diff --git a/docs/apis/subsystems/form/index.md b/docs/apis/subsystems/form/index.md index 60180426c7..a40aeeea70 100644 --- a/docs/apis/subsystems/form/index.md +++ b/docs/apis/subsystems/form/index.md @@ -300,6 +300,49 @@ public function definition() { } ``` +### filter_shown_headers() + + + +This method adds values to `_shownonlyelements` array to decide whether a header should be shown or hidden. +Only header names would be accepted and added to `_shownonlyelements` array. +Headers included in `_shownonlyelements` will be shown expanded in the form. The rest of the headers will be hidden. + +```php +public function filter_shown_headers(array $shownonly): void { + $this->_shownonlyelements = []; + if (empty($shownonly)) { + return; + } + foreach ($shownonly as $headername) { + $element = $this->getElement($headername); + if ($element->getType() == 'header') { + $this->_shownonlyelements[] = $headername; + $this->setExpanded($headername); + } + } +} +``` + +Empty `_shownonlyelements` array doesn't affect header's status or visibility. + +```php title="/course/editsection.php" +$showonly = optional_param('showonly', 0, PARAM_TAGLIST); + +[...] + +$mform = $courseformat->editsection_form($PAGE->url, $customdata); + +$initialdata = convert_to_array($sectioninfo); +if (!empty($CFG->enableavailability)) { + $initialdata['availabilityconditionsjson'] = $sectioninfo->availability; +} +$mform->set_data($initialdata); +if (!empty($showonly)) { + $mform->filter_shown_headers(explode(',', $showonly)); +} +``` + ### Other features In some cases you may want to [group elements](https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#Use_Fieldsets_to_group_Form_Elements) into collections.