lmscloud-io / mdlcode-docs

5 stars 3 forks source link

Form elements registered in included files are not found #5

Closed FMCorz closed 6 months ago

FMCorz commented 7 months ago

When declaring a new form element, and registering it in its own file, MDLCode does not find its definition.

// itemspertime.php
// [...]
MoodleQuickForm::registerElementType('block_xp_form_itemspertime', __FILE__, 'block_xp_form_itemspertime');

Source: https://github.com/FMCorz/moodle-block_xp/blob/master/classes/form/itemspertime.php#L298

// Form file.
require_once(__DIR__ . '/itemspertime.php');
// [...]
        $mform->addElement('block_xp_form_itemspertime', 'maxactionspertime', get_string('maxactionspertime', 'block_xp'), [
            'maxunit' => 60,
            'itemlabel' => get_string('actions', 'block_xp'),
        ]);
// [...]

Source: https://github.com/FMCorz/moodle-block_xp/blob/master/classes/form/config.php#L145

The error displayed for the addElement call is:

Form element 'block_xp_form_itemspertime' does not existMDLCode(unknown-form-element)

lmscloud-io commented 6 months ago

Hi Fred, thanks for the report

This was actually due to the fact that MDLCode could not parse the magic __FILE__ constant. If you have used "$CFG->dirroot/blocks/xp/classes/form/itemspertime.php" instead, it would work. But I will fix it in the next release to understand the __FILE__ as well

lmscloud-io commented 6 months ago

This is implemented in version 1.0.32 (released today)