From f533f30d3b3838f80bddd406d8c2b8411198b408 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 7 Sep 2022 16:53:06 +0100 Subject: [PATCH] MDL-29421 forms: return formatted frozen content for editor element. This was partly addressed in 82d491ca, however formatted content and embedded images were rendered incorrectly. --- public/course/edit_form.php | 2 - .../grade/grading/form/rubric/edit_form.php | 3 +- public/lib/form/editor.php | 41 +++++++++++++- public/lib/form/tests/editor_test.php | 54 +++++++++++++++++++ 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 public/lib/form/tests/editor_test.php diff --git a/public/course/edit_form.php b/public/course/edit_form.php index 81679feb48b..4b33c3cacb0 100644 --- a/public/course/edit_form.php +++ b/public/course/edit_form.php @@ -217,8 +217,6 @@ class course_edit_form extends moodleform { } if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) { - // Remove the description header it does not contain anything any more. - $mform->removeElement('descriptionhdr'); $mform->hardFreeze($summaryfields); } diff --git a/public/grade/grading/form/rubric/edit_form.php b/public/grade/grading/form/rubric/edit_form.php index 5ba5c9fd238..07da99e39e9 100644 --- a/public/grade/grading/form/rubric/edit_form.php +++ b/public/grade/grading/form/rubric/edit_form.php @@ -177,9 +177,8 @@ class gradingform_rubric_editrubric extends moodleform { } // freeze form elements and pass the values in hidden fields - // TODO MDL-29421 description_editor does not freeze the normal way, uncomment below when fixed $form = $this->_form; - foreach (array('rubric', 'name'/*, 'description_editor'*/) as $fieldname) { + foreach (['rubric', 'name', 'description_editor'] as $fieldname) { $el =& $form->getElement($fieldname); $el->freeze(); $el->setPersistantFreeze(true); diff --git a/public/lib/form/editor.php b/public/lib/form/editor.php index 8b178997322..5b85e8e0c43 100644 --- a/public/lib/form/editor.php +++ b/public/lib/form/editor.php @@ -41,7 +41,6 @@ require_once('templatable_form_element.php'); * @category form * @copyright 2009 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @todo MDL-29421 element Freezing * @todo MDL-29426 ajax format conversion */ class MoodleQuickForm_editor extends HTML_QuickForm_element implements templatable { @@ -495,13 +494,51 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element implements templatab return $context; } + /** + * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on + * + * @return string + */ + public function _getPersistantData() { + if (!$this->_persistantFreeze) { + return ''; + } else { + $id = $this->getAttribute('id'); + if (isset($id)) { + // Id of persistant input is different then the actual input. + $id = ['id' => $id . '_persistant']; + } else { + $id = []; + } + + $str = ''; + foreach ($this->getValue() as $key => $value) { + $str .= html_writer::empty_tag('input', [ + 'type' => 'hidden', + 'name' => $this->getName() . "[{$key}]", + 'value' => $value, + ] + $id); + } + return $str; + } + } + /** * Returns the formatted value. The return from parent class is not acceptable. * * @return string */ public function getFrozenHtml(): string { - return format_text($this->get_text(), $this->getFormat()) . $this->_getPersistantData(); + global $CFG; + + ['text' => $text, 'format' => $format] = $this->getValue(); + + // In post-formatted content, draftfiles are never expected to exist. However, in this case we do need to show + // embedded draft files, because they are expected to exist in the editor element. + $content = format_text($text, $format, ['context' => $this->_options['context'], 'overflowdiv' => true]); + $content = str_replace("\"$CFG->wwwroot/brokenfile.php#", "\"$CFG->wwwroot/draftfile.php", $content); + + return $content . $this->_getPersistantData(); } /** diff --git a/public/lib/form/tests/editor_test.php b/public/lib/form/tests/editor_test.php new file mode 100644 index 00000000000..3805118006e --- /dev/null +++ b/public/lib/form/tests/editor_test.php @@ -0,0 +1,54 @@ +. + +namespace core_form; + +use advanced_testcase; +use MoodleQuickForm_editor; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once("{$CFG->libdir}/form/editor.php"); + +/** + * Tests for the editor form element + * + * @package core_form + * @covers \MoodleQuickForm_editor + * @copyright 2026 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class editor_test extends advanced_testcase { + /** + * Test retrieving frozen HTML + */ + public function test_get_frozen_html(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Ensure "URL" filter is active. + filter_set_global_state('urltolink', TEXTFILTER_ON); + + $element = new MoodleQuickForm_editor('description_editor', 'Description'); + $element->setValue(['text' => 'http://example.com', 'format' => FORMAT_HTML]); + + $this->assertStringContainsString( + 'http://example.com', + $element->getFrozenHtml(), + ); + } +}