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;
/**
* @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') {

View file

@ -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);
}
}

View file

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