We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Extension - Add JS to publish page - which hook

Development and Programming

Neil Evans's avatar
Neil Evans
1,403 posts
15 years ago
Neil Evans's avatar Neil Evans

If from within an extension i want to add some JS to the end of a Channel Publish (or edit) page within the control panel… what would be the best hook to use?

I am looking to modify the behaviour of the pages tab, and i cannot see any hooks that allow me to do this directly. Therefore i am doing this via JS and adding it to the end of the publish page.

Currently i am triggering this via the “publish_form_channel_preferences” hook, and just returning exactly the same settings as are passed in. But not sure if this is the best way - advice or thoughts appreciated.

N

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
15 years ago
Rob Sanchez's avatar Rob Sanchez

Take a look at this hook (added in 2.1.2, I believe): http://ellislab.com/expressionengine/user-guide/development/extension_hooks/cp/javascript/index.html#cp_js_end

       
Neil Evans's avatar
Neil Evans
1,403 posts
15 years ago
Neil Evans's avatar Neil Evans

thanks for the post - it actually might be good fit - but my worry is that i am only doing this on the publish page, hence i would hate to continually trigger this hook when it is not needed. But i guess i could get triggered by the hook and then run my own tests - which would still be quite efficient… hmmmm

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
15 years ago
Rob Sanchez's avatar Rob Sanchez

Here’s a trick I pulled to only have it activate on the publish page:

public function cp_js_end()
    {
        $this->EE->load->helper('array');
        $this->EE->load->library('security');
        
        //get $_GET from the referring page
        parse_str(parse_url(@$_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $get);
        
        $output = $this->EE->extensions->last_call;
        
        //check if we're on the publish page
        if (element('D', $get) == 'cp' && element('C', $get) == 'content_publish' && element('M', $get) == 'entry_form' && element('channel_id', $get))
        {
            //do your business here
            //$output .= 'alert("blah");';

        }
        
        return $output;
    }
       
Neil Evans's avatar
Neil Evans
1,403 posts
15 years ago
Neil Evans's avatar Neil Evans

Hey, This is pretty much what i am doing:

if ( $this->EE->input->get('C')=="content_publish" && $this->EE->input->get('M')=="entry_form" )
{
    // STUFF
}

What is the overhead of running a hook, but then not doing anything? I am probably being overly anal!!!

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
15 years ago
Rob Sanchez's avatar Rob Sanchez

The thing about the cp_js_end hook is that it doesn’t actually occur on the same page load as the cp, it’s loaded as an external script:

< script type="text/javascript" src="index.php?S=0&D=cp&C=javascript&M=load&file=ext_scripts" >

So you don’t actually have access to the originating GET variables, because it’s technically a separate request.

As for the overhead, I would personally only worry about it on the front end. It doesn’t bother me to add a few milliseconds to the CP.

       
Neil Evans's avatar
Neil Evans
1,403 posts
15 years ago
Neil Evans's avatar Neil Evans

ah okay, that makes more sense… i’ll have a play then and update my scripts. Thanks, N

       
Brian Litzinger's avatar
Brian Litzinger
711 posts
15 years ago
Brian Litzinger's avatar Brian Litzinger

Official response from EllisLab is $this->EE->router->class

It looks like you’d use it this way….

if($this->EE->router->class == ‘content_publish’) { do something }

       
Nicolas Bottari's avatar
Nicolas Bottari
143 posts
15 years ago
Nicolas Bottari's avatar Nicolas Bottari

I think I need more information here. Using $this->EE->router->class within the cp_js_end() hook returns the ‘javascript’ class. Am I doing something wrong?

       
Nate Iler's avatar
Nate Iler
29 posts
15 years ago
Nate Iler's avatar Nate Iler

Anyone have any issues with browser cacheing? I’m running into the problem where even if i detect a publish page, my browser will return a cached version from a prior page.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.