Moodle

Allow developers to create their own templates on the fly for formslib forms

Details

  • Type: Bug Bug
  • Status: Reopened Reopened
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.8.3, 1.9
  • Fix Version/s: 2.0.8
  • Component/s: Forms Library
  • Labels:
    None

Description

Some patches from Guido Vega are described here http://moodle.org/mod/forum/discuss.php?d=80328#p356291

Can these please be reviewed to make sure they don't affect normal functionality and checked in HEAD and 1.8?

Activity

Hide
Jamie Pratt added a comment -

Committed a patch to 1.8 Stable and HEAD. Commit comment : "MDL-11318 formslib now respects a template for an element if it is set. But since formslib uses the templates to insert help buttons for setAdvanced functionality etc. if you use templates you will lose this functionality."

Show
Jamie Pratt added a comment - Committed a patch to 1.8 Stable and HEAD. Commit comment : "MDL-11318 formslib now respects a template for an element if it is set. But since formslib uses the templates to insert help buttons for setAdvanced functionality etc. if you use templates you will lose this functionality."
Hide
Guido Vega added a comment -

It may worth to mention that the fix in the forum entry is only a wrapper for the default template. This approach has positive and negative aspects. In the positive, it is convening because the developer can treat all substitutions (advanced, label, req, advancedimg, help, type, error and element) as a single unit without having to place the substitutions around div or span tags and makes it easy to manipulate and move around. On the other hand it does not allow full manipulation of the substitutions, e.g. trying to place the label above the element.

Perhaps a better approach is to include a function that allows easy wrapping (i.e. what is shown in the forum entry) and modify renderElement() so it does what it is suppose to do, rendering the element with the supplied template.

I have included a diff file for the formslib modify as described here. I hope you find it useful.

I have never submitted a "patch" (well a "wanna be" patch) so any positive criticism is welcome.

Regards,
GV

Show
Guido Vega added a comment - It may worth to mention that the fix in the forum entry is only a wrapper for the default template. This approach has positive and negative aspects. In the positive, it is convening because the developer can treat all substitutions (advanced, label, req, advancedimg, help, type, error and element) as a single unit without having to place the substitutions around div or span tags and makes it easy to manipulate and move around. On the other hand it does not allow full manipulation of the substitutions, e.g. trying to place the label above the element. Perhaps a better approach is to include a function that allows easy wrapping (i.e. what is shown in the forum entry) and modify renderElement() so it does what it is suppose to do, rendering the element with the supplied template. I have included a diff file for the formslib modify as described here. I hope you find it useful. I have never submitted a "patch" (well a "wanna be" patch) so any positive criticism is welcome. Regards, GV
Hide
Guido Vega added a comment -

Here is the file...

Show
Guido Vega added a comment - Here is the file...
Hide
Martin Dougiamas added a comment -

Thanks, Guido. Can you give us an example of how this would be called/used?

Show
Martin Dougiamas added a comment - Thanks, Guido. Can you give us an example of how this would be called/used?
Hide
Guido Vega added a comment -

/******************** CODE ********************/

$renderer =& $mform->defaultRenderer();

$template = <<<EOT
<table border="0">
<tr>
<td>
<div class="fitem {advanced}<Unable to render embedded object: File (-- BEGIN required --> required<) not found.-- END required -->"><div class="fitemtitle" style="text-align:left; width:80%"><label>{label} <!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</label></div> {help}</div>
</td>
</tr>
<tr>
<td>
<div style="width:100%;" class="felement {type}<Unable to render embedded object: File (-- BEGIN error --> error<) not found.-- END error ->"><!- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</div>
</td>
</tr>
</table>
EOT;

$templateleft = '<table width="100%" border="0"><tr><td align="center">{content}</td>';
$templateright = '<td align="center">{content}</td></tr></table>';

// Change the element's template
$mform->addElement('text', 'element1', 'Label 1', 'size="40"');
$mform->addRule('element1', $strrequired, 'required', null, 'client');
$mform->setHelpButton('element1', array('helpwithelement1', get_string('element1')), true);
$mform->setType('element1', PARAM_TEXT);
$renderer->setElementTemplate($template, 'element1');

// Wrap the element with supplied html
$mform->addElement('text', 'element2', 'Label 2', 'size="20"');
$mform->addRule('element2', $strrequired, 'required', null, 'client');
$mform->setType('element2', PARAM_TEXT);
$renderer->setElementTemplateWrap($templateleft, 'element2');

$mform->addElement('text', 'element3', 'Label 3', 'size="20"');
$mform->addRule('element3', $strrequired, 'required', null, 'client');
$mform->setType('element3', PARAM_TEXT);
$renderer->setElementTemplateWrap($templateright, 'element3');

/**************************************************/

From the code above, element1 will display the Label 1 above the text box and the help button to top right, i.e. a call to setElementTemplate() function will change the default template for an element. On the other hand, for element2 and elemtent3 the template does not change, instead it wraps the element's template with either $templateleft or $templateright.

Also, given that {req} substitution gets replace in MoodleQuikFrom_Renderer.startFrom() for _elementTemplates, then the following line should be included to also updated any submitted templates:

$this->_templates = str_replace('{req}', $this->_reqHTML, $this->_templates);

Show
Guido Vega added a comment - /******************** CODE ********************/ $renderer =& $mform->defaultRenderer(); $template = <<<EOT <table border="0"> <tr> <td> <div class="fitem {advanced}<Unable to render embedded object: File (-- BEGIN required --> required<) not found.-- END required -->"><div class="fitemtitle" style="text-align:left; width:80%"><label>{label} <!-- BEGIN required -->{req}<!-- END required -->{advancedimg}</label></div> {help}</div> </td> </tr> <tr> <td> <div style="width:100%;" class="felement {type}<Unable to render embedded object: File (-- BEGIN error --> error<) not found.-- END error ->"><!- BEGIN error --><span class="error">{error}</span><br /><!-- END error -->{element}</div> </td> </tr> </table> EOT; $templateleft = '<table width="100%" border="0"><tr><td align="center">{content}</td>'; $templateright = '<td align="center">{content}</td></tr></table>'; // Change the element's template $mform->addElement('text', 'element1', 'Label 1', 'size="40"'); $mform->addRule('element1', $strrequired, 'required', null, 'client'); $mform->setHelpButton('element1', array('helpwithelement1', get_string('element1')), true); $mform->setType('element1', PARAM_TEXT); $renderer->setElementTemplate($template, 'element1'); // Wrap the element with supplied html $mform->addElement('text', 'element2', 'Label 2', 'size="20"'); $mform->addRule('element2', $strrequired, 'required', null, 'client'); $mform->setType('element2', PARAM_TEXT); $renderer->setElementTemplateWrap($templateleft, 'element2'); $mform->addElement('text', 'element3', 'Label 3', 'size="20"'); $mform->addRule('element3', $strrequired, 'required', null, 'client'); $mform->setType('element3', PARAM_TEXT); $renderer->setElementTemplateWrap($templateright, 'element3'); /**************************************************/ From the code above, element1 will display the Label 1 above the text box and the help button to top right, i.e. a call to setElementTemplate() function will change the default template for an element. On the other hand, for element2 and elemtent3 the template does not change, instead it wraps the element's template with either $templateleft or $templateright. Also, given that {req} substitution gets replace in MoodleQuikFrom_Renderer.startFrom() for _elementTemplates, then the following line should be included to also updated any submitted templates: $this->_templates = str_replace('{req}', $this->_reqHTML, $this->_templates);

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated: