diff --git a/lib/form/form.js b/lib/form/form.js index d1a49667afe..0ef899f64f3 100644 --- a/lib/form/form.js +++ b/lib/form/form.js @@ -16,6 +16,8 @@ if (typeof M.form.dependencyManager === 'undefined') { _dirty: null, _nameCollections: null, _fileinputs: null, + _editors: null, + _editorNameSuffix: '[text]', initializer: function() { // Setup initial values for complex properties. @@ -87,8 +89,15 @@ if (typeof M.form.dependencyManager === 'undefined') { if (({}).hasOwnProperty.call(names, name)) { names[name].push(node); allnames[name].push(node); + } else if (this.isEditor(name)) { + // If this is an editor, we need to remove the suffix. + name = name.replace(this._editorNameSuffix, ''); + if (({}).hasOwnProperty.call(names, name)) { + names[name].push(node); + allnames[name].push(node); + } } - }); + }, this); // Locate any groups with the given name. this.get('form').all('.fitem').each(function(node) { var name = node.getData('groupname'); @@ -586,7 +595,26 @@ if (typeof M.form.dependencyManager === 'undefined') { lock: lock, hide: isHide ? lock : false }; - } + }, + /** + * Is the form element an editor? + * + * @param {String} el The form element name. + * @return {Boolean} + */ + isEditor: function(el) { + if (!this._editors) { + let editors = {}; + const selector = '.fitem [data-fieldtype="editor"] textarea'; + const els = this.get('form').all(selector); + els.each(function(node) { + editors[node.getAttribute('name')] = true; + }); + this._editors = editors; + } + + return this._editors[el] || false; + }, }, { NAME: 'mform-dependency-manager', ATTRS: { diff --git a/lib/form/tests/behat/fixtures/editor_hideif_disabledif_form.php b/lib/form/tests/behat/fixtures/editor_hideif_disabledif_form.php new file mode 100644 index 00000000000..25b9f896da3 --- /dev/null +++ b/lib/form/tests/behat/fixtures/editor_hideif_disabledif_form.php @@ -0,0 +1,66 @@ +. + +require_once(__DIR__ . '/../../../../../config.php'); + +//defined('BEHAT_SITE_RUNNING') || die(); + +global $CFG, $PAGE, $OUTPUT; +require_once($CFG->libdir . '/formslib.php'); +$PAGE->set_url('/lib/form/tests/behat/fixtures/editor_hideif_disabledif_form.php'); +$PAGE->add_body_class('limitedwidth'); +require_login(); +$PAGE->set_context(core\context\system::instance()); + +/** + * Test class for disabling and hiding a text area using an editor. + * + * @package core_form + * @copyright 2024 David Woloszyn + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class test_editor_hideif_disabledif_form extends moodleform { + + /** + * Form definition. + */ + public function definition(): void { + $mform = $this->_form; + + // Radio buttons. + $radiogroup = [ + $mform->createElement('radio', 'some_radios', '', 'Enable', '1'), + $mform->createElement('radio', 'some_radios', '', 'Disable', '2'), + $mform->createElement('radio', 'some_radios', '', 'Hide', '3'), + ]; + + $mform->addGroup($radiogroup, 'some_radios_group', 'Enable/Disable/Hide', ' ', false); + $mform->setDefault('some_radios', 1); + + // Editor with conditions. + $mform->addElement('editor', 'some_editor', 'My test editor'); + $mform->disabledIf('some_editor', 'some_radios', 'eq', '2'); + $mform->hideIf('some_editor', 'some_radios', 'eq', '3'); + + $this->add_action_buttons(); + } +} + +$form = new test_editor_hideif_disabledif_form(); + +echo $OUTPUT->header(); +$form->display(); +echo $OUTPUT->footer(); diff --git a/lib/form/tests/behat/hideif.feature b/lib/form/tests/behat/hideif.feature index 0ef1509e6c3..4fd4c5772f0 100644 --- a/lib/form/tests/behat/hideif.feature +++ b/lib/form/tests/behat/hideif.feature @@ -25,3 +25,9 @@ Feature: hideIf functionality in forms Then I should not see "Require group to make submission" And I should not see "Require all group members to submit" And I should not see "Grouping for student groups" + + Scenario: The editor is hidden when 'eq' hideIf conditions are met + Given I am on fixture page "/lib/form/tests/behat/fixtures/editor_hideif_disabledif_form.php" + And I should see "My test editor" + When I click on "Hide" "radio" + Then I should not see "My test editor"