Skip to content

Commit

Permalink
MDL-78527 forms: New parentclass attribute to wrapper element only
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Sep 12, 2023
1 parent b4c6ed3 commit 0122c2c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/form/group.php
Expand Up @@ -59,9 +59,18 @@ class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable
* @param array $elements (optional) array of HTML_QuickForm_element elements to group
* @param string $separator (optional) string to seperate elements.
* @param string $appendName (optional) string to appened to grouped elements.
* @param mixed $attributes (optional) Either a typical HTML attribute string
* or an associative array
*/
public function __construct($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
public function __construct(
$elementName = null,
$elementLabel = null,
$elements = null,
$separator = null,
$appendName = true,
$attributes = null
) {
parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName, $attributes);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/form/templatable_form_element.php
Expand Up @@ -66,6 +66,7 @@ public function export_for_template(renderer_base $output) {
$context[strtolower($propname)] = isset($this->$classpropname) ? $this->$classpropname : false;
}
$extraclasses = $this->getAttribute('class');
$parentonlyclasses = $this->getAttribute('parentclass');

// Special wierd named property.
$context['frozen'] = !empty($this->_flagFrozen);
Expand All @@ -74,11 +75,12 @@ public function export_for_template(renderer_base $output) {
// Other attributes.
$otherattributes = [];
foreach ($this->getAttributes() as $attr => $value) {
if (!in_array($attr, $standardattributes) && $attr != 'class' && !is_object($value)) {
if (!in_array($attr, $standardattributes) && $attr != 'class' && $attr != 'parentclass' && !is_object($value)) {
$otherattributes[] = $attr . '="' . s($value) . '"';
}
}
$context['extraclasses'] = $extraclasses;
$context['parentclasses'] = $parentonlyclasses;
$context['type'] = $this->getType();
$context['attributes'] = implode(' ', $otherattributes);
$context['emptylabel'] = ($this->getLabel() === '');
Expand Down
2 changes: 1 addition & 1 deletion lib/form/templates/element-template.mustache
Expand Up @@ -42,7 +42,7 @@
}
}
}}
<div id="{{element.wrapperid}}" class="form-group row {{#error}}has-danger{{/error}} fitem {{#element.emptylabel}}femptylabel{{/element.emptylabel}} {{#advanced}}advanced{{/advanced}} {{{element.extraclasses}}}" {{#element.groupname}}data-groupname="{{.}}"{{/element.groupname}}>
<div id="{{element.wrapperid}}" class="form-group row {{#error}}has-danger{{/error}} fitem {{#element.emptylabel}}femptylabel{{/element.emptylabel}} {{#advanced}}advanced{{/advanced}} {{{element.extraclasses}}} {{{element.parentclasses}}}" {{#element.groupname}}data-groupname="{{.}}"{{/element.groupname}}>
<div class="col-md-3 col-form-label d-flex pb-0 pr-md-0">
{{# label}}{{$ label }}
{{^element.staticlabel}}
Expand Down
4 changes: 4 additions & 0 deletions lib/form/upgrade.txt
@@ -1,6 +1,10 @@
This files describes API changes in core_form libraries and APIs,
information provided here is intended especially for developers.

=== 4.3 ===

* Added a new parameter to allow Groups to add attributes.

=== 4.2 ===

* The moodle-form-passwordunmask module has been removed. It was deprecated in Moodle 3.2.
4 changes: 3 additions & 1 deletion lib/formslib.php
Expand Up @@ -3370,7 +3370,9 @@ function startGroup(&$group, $required, $error){
$groupid = 'fgroup_' . $group->getAttribute('id');

// Update the ID.
$group->updateAttributes(array('id' => $groupid));
$attributes = $group->getAttributes();
$attributes['id'] = $groupid;
$group->updateAttributes($attributes);
$advanced = isset($this->_advancedElements[$group->getName()]);

$html = $OUTPUT->mform_element($group, $required, $advanced, $error, false);
Expand Down

0 comments on commit 0122c2c

Please sign in to comment.