lmscloud-io/mdlcode-docs

Form elements registered in included files are not found

Closed this issue · 2 comments

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)

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

This is implemented in version 1.0.32 (released today)