From ca81e906e0055682672e3688ecf4c67a1a6418aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Tue, 3 Sep 2019 15:30:06 +0800 Subject: [PATCH] MDL-62191 output: New extra attributes for single_button Useful for data- attributes. --- lib/outputcomponents.php | 25 ++++++++++++++++++++++++- lib/outputrenderers.php | 2 ++ lib/templates/single_button.mustache | 11 +++++++++-- lib/upgrade.txt | 1 + 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 8f1acefdb2d..dd0834b3a31 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -867,17 +867,24 @@ class single_button implements renderable { */ public $actionid; + /** + * @var array + */ + protected $attributes = []; + /** * Constructor * @param moodle_url $url * @param string $label button text * @param string $method get or post submit method + * @param array $attributes Attributes for the HTML button tag */ - public function __construct(moodle_url $url, $label, $method='post', $primary=false) { + public function __construct(moodle_url $url, $label, $method='post', $primary=false, $attributes = []) { $this->url = clone($url); $this->label = $label; $this->method = $method; $this->primary = $primary; + $this->attributes = $attributes; } /** @@ -898,6 +905,17 @@ class single_button implements renderable { $this->actions[] = $action; } + /** + * Sets an attribute for the HTML button tag. + * + * @param string $name The attribute name + * @param mixed $value The value + * @return null + */ + public function set_attribute($name, $value) { + $this->attributes[$name] = $value; + } + /** * Export data. * @@ -918,6 +936,11 @@ class single_button implements renderable { $data->tooltip = $this->tooltip; $data->primary = $this->primary; + $data->attributes = []; + foreach ($this->attributes as $key => $value) { + $data->attributes[] = ['name' => $key, 'value' => $value]; + } + // Form parameters. $params = $this->url->params(); if ($this->method === 'post') { diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 88bf930e180..113f5e35b56 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -1998,6 +1998,8 @@ class core_renderer extends renderer_base { foreach ((array)$options as $key=>$value) { if (array_key_exists($key, $button)) { $button->$key = $value; + } else { + $button->set_attribute($key, $value); } } diff --git a/lib/templates/single_button.mustache b/lib/templates/single_button.mustache index 265cc1dd07c..c15af6c5a41 100644 --- a/lib/templates/single_button.mustache +++ b/lib/templates/single_button.mustache @@ -42,7 +42,13 @@ "url" : "#", "primary" : true, "tooltip" : "This is a tooltip", - "label" : "This is a the button text" + "label" : "This is a the button text", + "attributes": [ + { + "name": "data-attribute", + "value": "yeah" + } + ] } }}
@@ -53,7 +59,8 @@ + {{#disabled}}disabled{{/disabled}} + {{#attributes}} {{name}}={{#quote}}{{value}}{{/quote}} {{/attributes}}>{{label}}
{{#hasactions}} diff --git a/lib/upgrade.txt b/lib/upgrade.txt index b9360cae1a2..88d411a13af 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -65,6 +65,7 @@ validation against and defaults to null (so, no user needed) if not provided. This setting can be set via the UI or by defining $CFG->cachetemplates in your config.php file. It is a boolean and should be set to either false or true. Developers will probably want to set this to false. * The core_enrol_edit_user_enrolment webservice has been deprecated. Please use core_enrol_submit_user_enrolment_form instead. +* \single_button constructor has a new attributes param to add attributes to the button HTML tag. === 3.7 ===