Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

Say for example, I wanted to add a "Expand/Collapse All" button to Matrix fields.

Is there any way to do this, or is this something more appropriate for a feature request for P&T?

share|improve this question

2 Answers 2

up vote 14 down vote accepted

One way to accomplish this is to write some code in the init() function of your root plugin file. So in your example that might look something like:

class MyFunkyPlugin extends BasePlugin
{

  function init() {

    // check we have a cp request as we don't want to this js to run anywhere but the cp
    // and while we're at it check for a logged in user as well
    if ( craft()->request->isCpRequest() && craft()->userSession->isLoggedIn() )
    {

      // the includeJs method lets us add js to the bottom of the page
      craft()->templates->includeJs('var Myjs = "foo";');

      // the includeCss method will add css in the head
      craft()->templates->includeCss('.myFunkyPluginSelector { color: red; }');

      // the includeJsResource method will add a js file to the bottom of the page
      craft()->templates->includeJsResource('myPlugin/js/app.js');

      // the includeCssResource method will add a link in the head
      craft()->templates->includeCssResource('myFunkyPlugin/css/style.css');

    }

  }

  // rest of the root plugin file
  ...

}

The nice thing about the init() function is it gets run before anything else in your plugin does and it gets run on every request as every installed plugin is initialised on every request.

More detail on the available functions can be found in the TemplatesService docs.

share|improve this answer
    
If you implement the includeJsResource or includeCssResource method, where would you put the actual resources? –  negatron Apr 15 at 9:17
1  
Well, if you are doing it in your own plugin then in the resources folder in the root of your plugin. So for example includeJsResource ('myPlugin/js/script.js') would load pluginRoot/resources/js/script.js. See the docs for a more thorough explanation. –  Josh Angell Apr 15 at 9:30
    
Brilliant, thanks for the super-speedy response –  negatron Apr 15 at 10:27
    
Glad I could help :) –  Josh Angell Apr 15 at 10:39

Josh's answer is great if you are interested in building your own plugin. But if that's a little much for you, you're still in luck...

You can use these plugins to quickly & easily add CSS and JavaScript to your control panel:

Disclaimer: I wrote both of those plugins, let me know if you run into any issues!

share|improve this answer
2  
Good shout @Lindsey D! –  Josh Angell Jun 18 '14 at 17:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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