Skip to content

Commit

Permalink
MDL-78288 formslib: Document new filter_shown_headers function in For…
Browse files Browse the repository at this point in the history
…ms API
  • Loading branch information
aanabit authored and Amaia Anabitarte committed Jul 12, 2023
1 parent 3f69582 commit b79a54c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/apis/subsystems/form/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,45 @@ public function definition() {
}
```

### filter_shown_headers()

<Since version="4.3" issueNumber="MDL-78288" />

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"
// set current value, make an editable copy of section_info object
// this will retrieve all format-specific options as well
$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.
Expand Down

0 comments on commit b79a54c

Please sign in to comment.