Skip to content

Commit d7c6575

Browse files
Frederic Massartdanpoltawski
authored andcommitted
MDL-55479 output: Single button uses templates
Part of MDL-55071
1 parent f1b3466 commit d7c6575

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

lib/outputcomponents.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,49 @@ public function add_confirm_action($confirmmessage) {
713713
public function add_action(component_action $action) {
714714
$this->actions[] = $action;
715715
}
716+
717+
/**
718+
* Export data.
719+
*
720+
* @param renderer_base $output Renderer.
721+
* @return stdClass
722+
*/
723+
public function export_for_template(renderer_base $output) {
724+
$url = $this->method === 'get' ? $this->url->out_omit_querystring(true) : $this->url->out_omit_querystring();
725+
726+
$data = new stdClass();
727+
$data->id = html_writer::random_id('single_button');
728+
$data->formid = $this->formid;
729+
$data->method = $this->method;
730+
$data->url = $url === '' ? '#' : $url;
731+
$data->label = $this->label;
732+
$data->classes = $this->class;
733+
$data->disabled = $this->disabled;
734+
$data->tooltip = $this->tooltip;
735+
736+
// Form parameters.
737+
$params = $this->url->params();
738+
if ($this->method === 'post') {
739+
$params['sesskey'] = sesskey();
740+
}
741+
$data->params = array_map(function($key) use ($params) {
742+
return ['name' => $key, 'value' => $params[$key]];
743+
}, array_keys($params));
744+
745+
// Button actions.
746+
$actions = $this->actions;
747+
$data->actions = array_map(function($key) use ($actions) {
748+
$args = !empty($actions[$key]->jsfunctionargs) ? json_encode($actions[$key]->jsfunctionargs) : false;
749+
return [
750+
'event' => $actions[$key]->event,
751+
'jsfunction' => $actions[$key]->jsfunction,
752+
'jsfunctionargs' => $args,
753+
];
754+
}, array_keys($actions));
755+
$data->hasactions = !empty($data->actions);
756+
757+
return $data;
758+
}
716759
}
717760

718761

theme/noname/classes/output/core_renderer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use preferences_groups;
2929
use action_menu;
3030
use help_icon;
31+
use single_button;
3132
use single_select;
3233
use paging_bar;
3334
use url_select;
@@ -276,6 +277,18 @@ protected function render_help_icon(help_icon $helpicon) {
276277
return $this->render_from_template('core/help_icon', $context);
277278
}
278279

280+
/**
281+
* Renders a single button widget.
282+
*
283+
* This will return HTML to display a form containing a single button.
284+
*
285+
* @param single_button $button
286+
* @return string HTML fragment
287+
*/
288+
protected function render_single_button(single_button $button) {
289+
return $this->render_from_template('core/single_button', $button->export_for_template($this));
290+
}
291+
279292
/**
280293
* Renders a single select.
281294
*
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<div class="{{classes}}">
2+
<form method="{{method}}" action="{{url}}" id="{{formid}}">
3+
{{#params}}
4+
<input type="hidden" name="{{name}}" value="{{value}}">
5+
{{/params}}
6+
<button type="submit" class="btn btn-secondary"
7+
id="{{id}}"
8+
title={{#quote}}{{tooltip}}{{/quote}}
9+
{{#disabled}}disabled{{/disabled}}>{{label}}</button>
10+
</form>
11+
</div>
12+
{{#hasactions}}
13+
{{#js}}
14+
require(['core/yui'], function(Y) {
15+
{{#actions}}
16+
Y.on('{{event}}', {{{jsfunction}}}, '#{{id}}', null{{#jsfunctionargs}}, {{{jsfunctionargs}}}{{/jsfunctionargs}});
17+
{{/actions}}
18+
});
19+
{{/js}}
20+
{{/hasactions}}

0 commit comments

Comments
 (0)