How can I use $mform->addElement('file'...

How can I use $mform->addElement('file'...

by Daniele Cordella -
Number of replies: 6
Picture of Core developers Picture of Plugin developers

Hello moodlers.

I read in https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#file

$mform->addElement('file', 'attachment', get_string('attachment', 'forum'));

but I can not find any occurrence of "Element('file'" in the last MOODLE_404_STABLE code

as you can see from a grep on my local code.

m404[MOODLE_404_STABLE|✚1…26] % grep -rn "Element('file'" * 
local/codechecker/classes/report.php:65:        $out->startElement('file');
local/codechecker/vendor/squizlabs/php_codesniffer/src/Reports/Xml.php:46:        $out->startElement('file');
local/codechecker/vendor/squizlabs/php_codesniffer/src/Reports/Checkstyle.php:44:        $out->startElement('file');
vendor/phpunit/php-code-coverage/src/Report/Clover.php:62:            $xmlFile = $xmlDocument->createElement('file');
vendor/phpunit/php-code-coverage/src/Report/Crap4j.php:70:            $file = $document->createElement('file');
vendor/symfony/translation/Dumper/XliffFileDumper.php:71:        $xliffFile = $xliff->appendChild($dom->createElement('file'));
vendor/symfony/translation/Dumper/XliffFileDumper.php:151:        $xliffFile = $xliff->appendChild($dom->createElement('file'));
vendor/symfony/dependency-injection/Dumper/XmlDumper.php:156:            $file = $this->document->createElement('file')

I am not sure about how to use it in the frame of an mform.

Any suggestion?

As usual, thanks in advance.

Average of ratings: -
In reply to Daniele Cordella

Re: How can I use $mform->addElement('file'...

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers
It looks like all uses of 'file' in core Moodle were replaced with either 'filepicker' or 'filemanager' in Moodle 2.0. Presumably you should use one of these elements instead.
Average of ratings: Useful (1)
In reply to Leon Stringer

Re: How can I use $mform->addElement('file'...

by Davo Smith -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Just to back up what Leon said - I can't think of a situation now where 'file' should be used instead of 'filepicker' or 'filemanager'.

If you're uploading a file that needs processing and then discarding immediately afterwards (fairly rare case), then 'filepicker' is appropriate.

In almost every case you should be looking at using 'filemanager' (which means you need to get to grips with the Moodle Files API, that can take a little getting used to, but is pretty powerful once you understand it).
Average of ratings: Useful (1)
In reply to Davo Smith

Ri: Re: How can I use $mform->addElement('file'...

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Thank you Leon and thank you Davo.
I agree with you. Presumably we all should use 'fielpicker' or 'filemanager' instead.
Given it is listed among mform elements in the documentation I wonder how to use it in the frame of an mform and the reasons that moved moodle HQ to introduce it.
 
I tried to add it to a test mform in my local moodle installation but I get the error:
Fatal error: Declaration of HTML_QuickForm_file::_findValue() must be compatible with HTML_QuickForm_element::_findValue(&$values) in /m404/lib/pear/HTML/QuickForm/file.php on line 328
 
There are details I am not understanding.
In reply to Daniele Cordella

Re: Ri: Re: How can I use $mform->addElement('file'...

by Davo Smith -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

The 'form' element was introduced long before 'filepicker' and 'filemanager' existed.

It still exists because, at a guess, no one has ever got around to actually removing it.

I would suggest simply ignoring its continued existence and use 'filemanager' instead (or 'filepicker', for the rare cases where it is more appropriate).

Average of ratings: Useful (1)
In reply to Davo Smith

Ri: Re: Ri: Re: How can I use $mform->addElement('file'...

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Cool!
This answer finally provide the history of that so strange item.
Thanks Davo.

Ok, I consider it a missed deletion from documentation.
Thank you.
In reply to Davo Smith

Re: Ri: Re: How can I use $mform->addElement('file'...

by Davo Smith -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Of course, I meant, "'file' element was introduced long before ..." - too late to edit my post now ...