From 246f847a2c65cfd9e067e54e648d7ec64f45e202 Mon Sep 17 00:00:00 2001 From: Stephan Robotta Date: Wed, 20 Nov 2024 17:16:48 +0100 Subject: [PATCH] MDL-83772 mod_data: Fixed autosave bug in text areas with same name --- .../mod/data/field/textarea/field.class.php | 66 ++++++++++++++--- .../mod/data/tests/behat/edit_entries.feature | 70 +++++++++++++++++++ 2 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 public/mod/data/tests/behat/edit_entries.feature diff --git a/public/mod/data/field/textarea/field.class.php b/public/mod/data/field/textarea/field.class.php index 2b69334884c..fd7238ffd08 100644 --- a/public/mod/data/field/textarea/field.class.php +++ b/public/mod/data/field/textarea/field.class.php @@ -31,6 +31,13 @@ require_once($CFG->dirroot.'/repository/lib.php'); class data_field_textarea extends data_field_base { var $type = 'textarea'; + + /** + * Prefix for the field name to split field id and the random part. + * @var string + */ + protected const RND_PREFIX = 'xZx'; + /** * priority for globalsearch indexing * @@ -38,6 +45,40 @@ class data_field_textarea extends data_field_base { */ protected static $priority = self::LOW_PRIORITY; + /** + * The field name for the content field. + * + * @var string|null + */ + protected $fieldname = null; + + /** + * Returns a random field name for the content field. + * + * @return string + */ + protected function get_content_field_name(): string { + if ($this->fieldname === null) { + $this->fieldname = sprintf( + 'field_%s_%s%s', + $this->field->id, + self::RND_PREFIX, + bin2hex(random_bytes(5)) + ); + } + return $this->fieldname; + } + + /** + * Check if the given field name is the content field that contains the actual data. + * + * @param string $name + * @return bool + */ + protected function is_content_field(string $name): bool { + return (strlen($name) === 13 && substr($name, 0, 3) === self::RND_PREFIX); + } + public function supports_preview(): bool { return true; } @@ -82,7 +123,7 @@ class data_field_textarea extends data_field_base { $text = ''; $format = 0; $str = '
'; - $str .= '