From a91a4dd77b48c0dd84464c03704d01e2b42f4acf Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 7 Aug 2017 15:53:39 +0800 Subject: [PATCH] MDL-59740 core_form: add ability to specify if a button is primary --- lib/form/submit.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/form/submit.php b/lib/form/submit.php index 692188e9233..e40f457567c 100644 --- a/lib/form/submit.php +++ b/lib/form/submit.php @@ -43,15 +43,28 @@ class MoodleQuickForm_submit extends HTML_QuickForm_submit implements templatabl export_for_template as export_for_template_base; } + /** + * @var bool $primary Is this button a primary button? + */ + protected $primary; + /** * constructor * * @param string $elementName (optional) name of the field * @param string $value (optional) field label * @param string $attributes (optional) Either a typical HTML attribute string or an associative array + * @param bool|null $primary Is this button a primary button? */ - public function __construct($elementName=null, $value=null, $attributes=null) { + public function __construct($elementName=null, $value=null, $attributes=null, $primary = null) { parent::__construct($elementName, $value, $attributes); + + // Fallback to legacy behaviour if no value specified. + if (is_null($primary)) { + $this->primary = $this->getName() != 'cancel'; + } else { + $this->primary = $primary; + } } /** @@ -59,9 +72,9 @@ class MoodleQuickForm_submit extends HTML_QuickForm_submit implements templatabl * * @deprecated since Moodle 3.1 */ - public function MoodleQuickForm_submit($elementName=null, $value=null, $attributes=null) { + public function MoodleQuickForm_submit($elementName=null, $value=null, $attributes=null, $primary = null) { debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); - self::__construct($elementName, $value, $attributes); + self::__construct($elementName, $value, $attributes, $primary); } /** @@ -115,7 +128,7 @@ class MoodleQuickForm_submit extends HTML_QuickForm_submit implements templatabl public function export_for_template(renderer_base $output) { $context = $this->export_for_template_base($output); - if ($this->getName() == 'cancel') { + if (!$this->primary) { $context['secondary'] = true; } return $context;