From 5253834d031e8ddcf7d0c737e08251600ad2e1a7 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 24 Jan 2020 08:22:08 +0000 Subject: [PATCH] MDL-67532 badges: better default for new badge language. --- badges/classes/form/badge.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/badges/classes/form/badge.php b/badges/classes/form/badge.php index f6d5e09d501df..faa64a651e93f 100644 --- a/badges/classes/form/badge.php +++ b/badges/classes/form/badge.php @@ -49,7 +49,6 @@ public function definition() { $mform = $this->_form; $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false; $action = $this->_customdata['action']; - $languages = get_string_manager()->get_list_of_languages(); $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges')); $mform->addElement('text', 'name', get_string('name'), array('size' => '70')); @@ -61,6 +60,8 @@ public function definition() { $mform->addElement('text', 'version', get_string('version', 'badges'), array('size' => '70')); $mform->setType('version', PARAM_TEXT); $mform->addHelpButton('version', 'version', 'badges'); + + $languages = get_string_manager()->get_list_of_languages(); $mform->addElement('select', 'language', get_string('language'), $languages); $mform->addHelpButton('language', 'language', 'badges'); @@ -157,7 +158,16 @@ public function definition() { $mform->setType('action', PARAM_TEXT); if ($action == 'new') { - $mform->setDefault('language', $CFG->lang); + // Try to set default badge language to that of current language, or it's parent. + $language = current_language(); + if (isset($languages[$language])) { + $defaultlanguage = $language; + } else { + // Calling get_parent_language returns an empty string instead of 'en'. + $defaultlanguage = get_parent_language($language) ?: 'en'; + } + + $mform->setDefault('language', $defaultlanguage); $this->add_action_buttons(true, get_string('createbutton', 'badges')); } else { // Add hidden fields.