Jump to content

ModuleJS won't append my script AFTER jquery core


Rob
 Share

Recommended Posts

Hi all.

I've had this problem before, and after reading a bunch of forum posts, I'm still no clearer on what is the correct procedure to load a JS script AFTER jquery core.  There is some conflicting information and I'm starting to get frustrated over here!

The following code doesn't even seem to execute the __execute() method so it neither adds the jquery cookie script nor the hook.

<?php

class PageEditFoldStatus extends ModuleJS {

	public static function getModuleInfo() {
		return array(
			'title'		=> 'Page Edit Fold Status',		// printable name/title of module
			'version'	=> 0.1, 	// version number of module
			'summary'	=> 'Uses cookie to remember fold status of fields on page edit', 	// 1 sentence summary of module
			'href'		=> '', 		// URL to more information (optional)
			'permanent' => false, 	// true if module is permanent and thus not uninstallable
			'singular' => true,
			'autoload' => true
		); 
	}
	
	/*public function init() {
		parent::init();
	}*/
	
	public function ___execute() {
		$class = $this->className();
		//echo $this->config->urls->$class . "js/jquery-cookie-master/jquery.cookie.js"; exit;
		if(is_file($this->config->paths->$class . "js/jquery-cookie-master/jquery.cookie.js")) 
			$this->config->scripts->append($this->config->urls->$class . "js/jquery-cookie-master/jquery.cookie.js");
		$this->addHookAfter('ProcessPageEdit::execute', $this, 'triggerScript');
	}
	
	public function triggerScript(HookEvent $event) {
		$script = <<< _END
<script type="text/javascript">
	...JS code...
</script>
_END;
		$event->return .= $script;
	}
	
	public function isAutoload() { return true; }
	
}

It seems like I would want this to auto-load because it isn't a Process module and I need it to add the ProcessPageEdit hook.

Any thoughts?!

Link to comment
Share on other sites

<?php

class PageEditFoldStatus extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title'     => 'Page Edit Fold Status',     // printable name/title of module
            'version'   => 0.1,     // version number of module
            'summary'   => 'Uses cookie to remember fold status of fields on page edit',    // 1 sentence summary of module
            'href'      => '',      // URL to more information (optional)
            'singular' => true,
            'autoload' => true
        ); 
    }
    
    public function init() {
        $this->addHookAfter('ProcessPageEdit::execute', $this, 'hookPageEdit');
        
        // make sure we the request is from backend as ProcessPageView is used for all page views
        if(strpos($_SERVER['REQUEST_URI'], $this->config->urls->admin) !== 0) return;
        $this->addHookAfter('ProcessPageView::execute', $this, 'triggerScript');
    }
    
    public function hookPageEdit() {
        $class = $this->className();
        $this->config->scripts->add($this->config->urls->$class . "js/jquery-cookie-master/jquery.cookie.js");
    }
    
    public function triggerScript(HookEvent $event) {
        // only go further if we are on page edit process 
        if($event->process != "ProcessPageEdit") return; 
        $script = <<< _END
<script type="text/javascript">
    //...JS code...
</script>
_END;
        $event->return = str_replace("</body>", $script . "</body>" $event->return);
    }
}

This should do it.

Forget about ModuleJS. Jquery core script is added in the admin.php process controller, so if you use ModuleJS or an init function of a Wire Module it will always get added before jquery core.

So only way to add is through an autoload module with a hook on a other process or inputfield module.

To render a script into the html, you need a hook to something that returns some rendered markup. Then inject it with a str_replace. Or you could hook into the ProcessPageEdit::buildForm and append a InputfieldMarkup with the script as the content, that will then get rendered with the form.

Edit: changed Wire to WireData and added check to only add hook if on a backend url

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...