Call to undefined method MoodleQuickForm::get_data()

Call to undefined method MoodleQuickForm::get_data()

by Andreas Ragen Ayal -
Number of replies: 6

I'm developing a course activity.

For this I need to allow instructors upload a bunch of files. I'm using the 'filemanager' form element to handle this. To get an understanding of what's going on and to try stuff out, I'm using the "Loading existing files into draft area" and the "Store updated set of files" example code as is.

But while testing, I keep getting the error that the methods 'get_data' and 'set_data' don't exist. Which is weird, because my form extends 'moodleform_mod', just like in the examples, which (I checked) does contain 'get_data'.

Any insights?
I'm using version 2020061502.04 (3.9.2+ STABLE) of Moodle.

Thanks. smile

Average of ratings: -
In reply to Andreas Ragen Ayal

Re: Call to undefined method MoodleQuickForm::get_data()

by Céline Perves -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Hello,
can you give us a few line of your code and maybe error trace

Céline
In reply to Céline Perves

Re: Call to undefined method MoodleQuickForm::get_data()

by Andreas Ragen Ayal -

An other comment seems to have fixed my problem.
My source code, now that it's "fixed" is:

function definition() {
  $mform =& $this->_form;
  $mform->addElement('text', 'name', 'Name');
  $mform->setType('name', PARAM_TEXT);
  $mform->addRule('name', null, 'required', null, 'client');
  $mform->addElement('filemanager', 'attachments', get_string('attachment', 'adoc'), null, array('subdirs' => 1));
  if (empty($entry->id)) {
    $entry = new stdClass;
    $entry->id = null;
  }
  $draftitemid = file_get_submitted_draft_itemid('attachments');
  file_prepare_draft_area($draftitemid, $this->context->id, 'mod_glossary', 'attachment', $entry->id, array('subdirs' => 1));
  $entry->attachments = $draftitemid;
  $this->set_data($entry);
  //$mform->set_data($entry);
  //if ($data = $mform->get_data()) {
  if ($data = $this->get_data()) {
  // ... store or update $entry
    file_save_draft_area_files($data->attachments, $this->context->id, 'mod_glossary', 'attachment', $entry->id, array('subdirs' => 1));
  }
  $this->standard_coursemodule_elements();
  $this->add_action_buttons();
}
In reply to Andreas Ragen Ayal

Re: Call to undefined method MoodleQuickForm::get_data()

by Andreas Ragen Ayal -
Any extra insights are welcome, I'm not a particularly good PHP dev.
In reply to Andreas Ragen Ayal

Re: Call to undefined method MoodleQuickForm::get_data()

by Demir Agovic -
Try this: $this->set_data($entry);
Average of ratings: Useful (1)
In reply to Demir Agovic

Re: Call to undefined method MoodleQuickForm::get_data()

by Andreas Ragen Ayal -
This seems to have worked.
Could you please explain why this works and $mform doesn't?
In reply to Andreas Ragen Ayal

Re: Call to undefined method MoodleQuickForm::get_data()

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

"moodleform_mod" is a class that inherits from class "moodleform".

The "moodleform" class, has a member variable called $_form, that is an instance of the "MoodleQuickForm" class, which inherits from "HTMLQuickForm".

The outer "moodleform" class handles general functionality such as the get_data() and set_data() calls, whereas the "MoodleQuickForm" class handles the internal details of each of the elements within the form.

Inside the "definition()" function, you generally use $mform as a reference to the internal $_form member (i.e. $mform is an instance of "MoodleQuickForm").

So, inside the "definition()" function, you can call $this->set_data(), as that is calling the function defined in the "moodleform" class - you can't call $mform->set_data(), because the "MoodlQuickForm" class has no "set_data()" function defined.

Average of ratings: Useful (1)