HTML_QuickForm -> addElement -> defaultcustom

HTML_QuickForm -> addElement -> defaultcustom

by Yuval Jacobi -
Number of replies: 5

Hi there smile

Moodle-3.3 has a new HTML_QuickForm element type called defaultcustom, which displays a checkbox right above the text input field, to indicate if the input field value should remain with it's default value, or can accept customized value.

php implementation for this element type is in ./lib/form/defaultcustom.php


It is used in course/editsection_form.php line 27, 

so when creating a new section in a course, which gets a sequential name by default (e.g. Section-123), can be overridden with a custom name.

By default this checkbox is not checked - meaning that the user can save the section without changing the section name.  (see the attached screenshot).

We would like to have this checkbox checked by default, in order to encourage (or actually force) the user to fill in a proper name, and not leave the default name as is.

I couldn't find any documentation on this new element type,  not could I figure out how to force this when creating the element.

Any suggestions?

Attachment custom_section_name.jpg
Average of ratings: -
In reply to Yuval Jacobi

Re: HTML_QuickForm -> addElement -> defaultcustom

by Olumuyiwa Taiwo -
Picture of Plugin developers

Have you tried:

$mform->setDefault('name', true);


In reply to Olumuyiwa Taiwo

Re: HTML_QuickForm -> addElement -> defaultcustom

by Yuval Jacobi -

Yes,  

 $mform->setDefault('name', true);

 is obviously the first thing I've tried - but no impact...

I hate to go and make a core change inside the defaultcustom element, as it will impact all instances of this element, and I need this feature turned on only within this location.

Should I open a tracker issue on it ?

In reply to Yuval Jacobi

Re: HTML_QuickForm -> addElement -> defaultcustom

by Darko Miletić -

This is a barebone example of form with that element and it is checked by default:

require(__DIR__.'/config.php');
require_once($CFG->libdir.'/formslib.php');

require_login();


class tform extends moodleform {

    protected function definition() {
        $form = $this->_form;

        $form->addElement('defaultcustom', 'dfcel', 'Label text', ['type' => 'text', 'defaultvalue' => 'default text']);

        $this->add_action_buttons();
    }

}

$PAGE->set_context(context_system::instance());
$PAGE->set_url('/frt2.php');
$PAGE->set_title('Page title');

/** @var core_renderer $OUTPUT */
$OUTPUT;

$form = new tform($PAGE->url);

echo $OUTPUT->header();

$form->display();

echo $OUTPUT->footer();

I tested this with stock Moodle 3.3.7+


In reply to Darko Miletić

Re: HTML_QuickForm -> addElement -> defaultcustom

by Yuval Jacobi -

Thanks Darko!

Your code example works here too. 

This should give me a hint for putting that to work right. 

smile

Will publish the change later on...

YJ.

In reply to Yuval Jacobi

Re: HTML_QuickForm -> addElement -> defaultcustom

by Yuval Jacobi -

Figured it out...


in course/editsection_form.php, comment out line 91 


        if (strval($default_values->name) === '') {

            // $default_values->name = false; // custom checkbox needs to be CHECKED in order to encourage user to fill-in a proper name

        }