Skip to content

Commit

Permalink
Merge branch 'MDL-71336' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Dec 5, 2023
2 parents 1d9f2de + b7205c5 commit 4319d6f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lib/form/dateselector.php
Expand Up @@ -130,11 +130,12 @@ function _createElements() {
// If optional we add a checkbox which the user can use to turn if on.
if ($this->_options['optional']) {
$this->_elements[] = $this->createFormElement('checkbox', 'enabled', null,
get_string('enable'), $this->getAttributes(), true);
get_string('enable'), $this->getAttributesForFormElement(), true);
}
foreach ($dateformat as $key => $value) {
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $value, $this->getAttributes(), true);
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $value,
$this->getAttributesForFormElement(), true);
}
// The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
if ($calendartype->get_name() === 'gregorian') {
Expand Down
13 changes: 7 additions & 6 deletions lib/form/datetimeselector.php
Expand Up @@ -134,26 +134,27 @@ function _createElements() {
// If optional we add a checkbox which the user can use to turn if on.
if ($this->_options['optional']) {
$this->_elements[] = $this->createFormElement('checkbox', 'enabled', null,
get_string('enable'), $this->getAttributes(), true);
get_string('enable'), $this->getAttributesForFormElement(), true);
}
$dateformat = $calendartype->get_date_order($this->_options['startyear'], $this->_options['stopyear']);
if (right_to_left()) { // Display time to the right of date, in RTL mode.
$this->_elements[] = $this->createFormElement('select', 'minute', get_string('minute', 'form'),
$minutes, $this->getAttributes(), true);
$minutes, $this->getAttributesForFormElement(), true);
$this->_elements[] = $this->createFormElement('select', 'hour', get_string('hour', 'form'),
$hours, $this->getAttributes(), true);
$hours, $this->getAttributesForFormElement(), true);
// Reverse date element (Should be: Day, Month, Year), in RTL mode.
$dateformat = array_reverse($dateformat);
}
foreach ($dateformat as $key => $date) {
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true);
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $date,
$this->getAttributesForFormElement(), true);
}
if (!right_to_left()) { // Display time to the left of date, in LTR mode.
$this->_elements[] = $this->createFormElement('select', 'hour', get_string('hour', 'form'), $hours,
$this->getAttributes(), true);
$this->getAttributesForFormElement(), true);
$this->_elements[] = $this->createFormElement('select', 'minute', get_string('minute', 'form'), $minutes,
$this->getAttributes(), true);
$this->getAttributesForFormElement(), true);
}
// The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
if ($calendartype->get_name() === 'gregorian') {
Expand Down
7 changes: 2 additions & 5 deletions lib/form/duration.php
Expand Up @@ -172,10 +172,7 @@ public function seconds_to_unit($seconds) {
* Override of standard quickforms method to create this element.
*/
function _createElements() {
$attributes = $this->getAttributes();
if (is_null($attributes)) {
$attributes = [];
}
$attributes = $this->getAttributesForFormElement();
if (!isset($attributes['size'])) {
$attributes['size'] = 3;
}
Expand All @@ -191,7 +188,7 @@ function _createElements() {
// If optional we add a checkbox which the user can use to turn if on
if($this->_options['optional']) {
$this->_elements[] = $this->createFormElement('checkbox', 'enabled', null,
get_string('enable'), $this->getAttributes(), true);
get_string('enable'), $attributes, true);
}
foreach ($this->_elements as $element){
if (method_exists($element, 'setHiddenLabel')){
Expand Down
10 changes: 10 additions & 0 deletions lib/form/group.php
Expand Up @@ -164,6 +164,16 @@ public function createFormElement() {
return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
}

/**
* Return attributes suitable for passing to {@see createFormElement}, comprised of all group attributes without ID in
* order to ensure uniqueness of that value within the group
*
* @return array
*/
public function getAttributesForFormElement(): array {
return array_diff_key((array) $this->getAttributes(), array_flip(['id']));
}

public function export_for_template(renderer_base $output) {
global $OUTPUT;

Expand Down
2 changes: 2 additions & 0 deletions lib/form/upgrade.txt
Expand Up @@ -7,6 +7,8 @@ information provided here is intended especially for developers.
Alongside with that new ".suggestions-heading" class was added to easily generate suggestion headings
* The `core/form-autocomplete` module now exports an `enhanceField` method to return native promise (of
which the previous `enhance` is now a wrapper of, while preserving jQuery promise return)
* The group element has a new method `getAttributesForFormElement` which should be used in conjunction
with `createFormElement` to ensure that all elements within the group have unique IDs

=== 4.3 ===

Expand Down

0 comments on commit 4319d6f

Please sign in to comment.