LMSouq
moodle-core Open

js_init_call gives Cannot call method 'init' of undefined in Moodle block plugin

ZX
Zxaos
1 month ago
3 views
Problem Description
I have a moodle block plugin where I'm trying to include javascript in the page. No matter how I try, the javascript called by the js_init_call always throws an error instead of running I've tried passing a full array to js_init_call, and I've tried the shorthand form that assumes the file is called module.js If the javascript file *doesn't* exist, I get a moodle error complaining about the missing file. If it *does* exist, I get a javascript error as above. What am I doing wrong? block_foo.php: <?php class block_foo extends block_base { public function init() { $this->title = get_string('foo', 'block_foo'); } public function applicable_formats(){ return array('course-view' => true); } public function get_content() { global $PAGE; if ($this->content !== null) { return $this->content; } if(!isloggedin()){ //if(!is_enrolled()){ return false; } $this->content = new stdClass; $this->content->text = 'asdf'; $this->content->footer = ''; $jsmodule = array( 'name' => 'block_foo', 'fullpath' => '/blocks/foo/foo.js', 'requires' => array(), 'strings' => array() ); $PAGE->requires->js_init_call('M.block_foo.init', null, false, $jsmodule); //This style of call doesn't work either... (if the js is named module.js) //$PAGE->requires->js_init_call('M.block_foo.init', null); return $this->content; } function hide_header(){ return true; } } module.js or foo.js: M.block_foo = {}; M.block_foo.init = function(){ alert("I was called, yay"); $var = 1234; $var = $var + 3; };

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base