MDL-62191 output: New extra attributes for single_button

Useful for data- attributes.
This commit is contained in:
David Monllaó 2019-09-03 15:30:06 +08:00
parent 9528b1ff5b
commit ca81e906e0
4 changed files with 36 additions and 3 deletions

View file

@ -867,17 +867,24 @@ class single_button implements renderable {
*/ */
public $actionid; public $actionid;
/**
* @var array
*/
protected $attributes = [];
/** /**
* Constructor * Constructor
* @param moodle_url $url * @param moodle_url $url
* @param string $label button text * @param string $label button text
* @param string $method get or post submit method * @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->url = clone($url);
$this->label = $label; $this->label = $label;
$this->method = $method; $this->method = $method;
$this->primary = $primary; $this->primary = $primary;
$this->attributes = $attributes;
} }
/** /**
@ -898,6 +905,17 @@ class single_button implements renderable {
$this->actions[] = $action; $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. * Export data.
* *
@ -918,6 +936,11 @@ class single_button implements renderable {
$data->tooltip = $this->tooltip; $data->tooltip = $this->tooltip;
$data->primary = $this->primary; $data->primary = $this->primary;
$data->attributes = [];
foreach ($this->attributes as $key => $value) {
$data->attributes[] = ['name' => $key, 'value' => $value];
}
// Form parameters. // Form parameters.
$params = $this->url->params(); $params = $this->url->params();
if ($this->method === 'post') { if ($this->method === 'post') {

View file

@ -1998,6 +1998,8 @@ class core_renderer extends renderer_base {
foreach ((array)$options as $key=>$value) { foreach ((array)$options as $key=>$value) {
if (array_key_exists($key, $button)) { if (array_key_exists($key, $button)) {
$button->$key = $value; $button->$key = $value;
} else {
$button->set_attribute($key, $value);
} }
} }

View file

@ -42,7 +42,13 @@
"url" : "#", "url" : "#",
"primary" : true, "primary" : true,
"tooltip" : "This is a tooltip", "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"
}
]
} }
}} }}
<div class="{{classes}}"> <div class="{{classes}}">
@ -53,7 +59,8 @@
<button type="submit" class="btn {{#primary}}btn-primary{{/primary}}{{^primary}}btn-secondary{{/primary}}" <button type="submit" class="btn {{#primary}}btn-primary{{/primary}}{{^primary}}btn-secondary{{/primary}}"
id="{{id}}" id="{{id}}"
title={{#quote}}{{tooltip}}{{/quote}} title={{#quote}}{{tooltip}}{{/quote}}
{{#disabled}}disabled{{/disabled}}>{{label}}</button> {{#disabled}}disabled{{/disabled}}
{{#attributes}} {{name}}={{#quote}}{{value}}{{/quote}} {{/attributes}}>{{label}}</button>
</form> </form>
</div> </div>
{{#hasactions}} {{#hasactions}}

View file

@ -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 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. 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. * 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 === === 3.7 ===