6

How can I add a custom script to the "New Entry" control panel page to interact with the input form. The script does not need to be added to the "Edit Entry" page and certainly should not be added to any other control panel pages within ExpressionEngine.

How is this achieved? Can I write an extension which utilises a particular hook?

2 Answers 2

3

A quick solution is to pop the code into the field's instructions. It will show on adds/edits if you do that. You might be able to create a conditional based on the url to not run it on edits.

EDIT: Added screenshot

enter image description here

6
  • I would prefer to not modify the matrix source code to avoid future maintenance issues if possible. Or is that not what you meant?
    – Lea Hayes
    Dec 13, 2012 at 5:50
  • 1
    You won't be touching Matrix code. You'll add your new JS to the custom field's instruction field. Just added a screenshot of the field I'm talking about in my answer.
    – Anna_MediaGirl
    Dec 13, 2012 at 6:32
  • 1
    Well that was much easier!! thanks once again for your help :-)
    – Lea Hayes
    Dec 13, 2012 at 6:38
  • 1
    A conditional to check whether it's a new entry or not: if (document.location.href.indexOf("entry_id") == -1) { // new entry } else { // existing entry } Dec 17, 2012 at 5:06
  • My javascript is getting converted to HTML entities using this method: <script type="text/javascript">alert(&#8220;hey it works&#8221;);</script> Anyone else having this issue? I don't see any options to stop this conversion.
    – Josh KG
    Oct 23, 2013 at 19:20
5

I'm not quite as trusting with the way the fields labels handles JavaScript over say the way it handles HTML which, by the way, handles nicely.

Anyways, I will add another option here for future reference as I think it could be helpful either way:

As I mentioned in a comment on another question you could write an extension which uses the cp_js_end hook

You said:

The script does not need to be added to the "Edit Entry" page

But I assume you mean it does need to be added to the Publish Entry page.

If that is the case then what you could do is check if you are in fact on the publish entry page by using a conditional in your extension similar to this:

if ($this->EE->input->get_post('D') == "cp" && $this->EE->input->get_post('C') == "content_publish" && $this->EE->input->get_post('M') == "entry_form" )
{
    your code
}

(Not sure you need all three checks but you get the idea.)

What the code above is doing is checking for any query strings in the URL and if they match do something. Obviously it's just a start to get you going.

Also, check out this add-on from Pixel & Tonic if you want some ideas on how to use this particular hook (and or cp_css_end) in an extension. In fact, you could add my conditional strait to that add-on unless I'm reading the license wrong. The code I'm thinking of is around line 141 which you'd wrap my conditional example around that line.

The add-on: https://github.com/pixelandtonic/cp_css_js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.