Jump to:

10931 Posts in 2614 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » A module that adds a JS tag to all pages

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 432 Views
  • JussiL
    Avatar
    Community Member
    1 Post

    A module that adds a JS tag to all pages Link to this post

    Hi,

    I have a task to create a SilverStripe (2.4) module, that has a really simple goal: It should just add a "<script src='http://...'></script>" JS-tag to the end of all pages on a SilverStripe site. That's like (for example) Google Analytics -module does.

    However, the JS-tag (mentioned above) includes a custom code (in src-attribute), that the administrator should be able to change by using CMS. I'm quite new to SilverStripe (especially when creating modules), but I have planned to create the module's admin menu by creating a custom admin area by extending class LeftAndMain.

    Currently I have just made the following simple files:

    MyAdminPanel.php:

    <?php
    class MyAdminPanel extends LeftAndMain {

       static $url_segment = 'myadminpanel';
       static $menu_title = 'myAdminPanel';

       public function init() {
          parent::init();
       }
    }

    MyAdminPanel_left.ss:

    <div id="MyAdminPanel_left" style="margin: 5px;">

       <br />
       <p>Insert your code here:</p>
       <input type="text" class="text" />
       <br />
       <button>Save</button>
             
    </div>

    MyAdminPanel_right.ss:

    <div id="MyAdminPanel_right">

       <style> h2 { margin: 0; padding: 0; } </style>
       
       <h2>Welcome to my module!</h2>
       <p>Write your code to the text field on the left and click save. Then the code is added to the script tag on every page.</p>
             
    </div>

    My problem is, how to continue and expand those files? How should I make a script that adds "<script src='http://... code here'></script>" -tag automatically to every page on the site? The code (to the tag) should be taken from the input-field on MyAdminPanel_left.ss. Basically this module should work like Google Analytics module (which also adds a script tag to the end of every page), BUT the administrator should be able to modify the code (in the <script>-tag) from CMS.

    Is there anywhere good tutorials how to build modules and custom admin areas?

  • Chris_Bryer
    Avatar
    Community Member
    35 Posts

    Re: A module that adds a JS tag to all pages Link to this post

    create a class that extends DataObjectDecorator,
    in that class, you can add

    public function contentcontrollerInit(){
    $script = "alert('hi there')";
    Requirements::customScript($script);
    }


    add the extension to SiteTree:

    (mysite/_config.php)
    Object::add_extension('SiteTree', 'MyCustomDataObjectDecorator');

    now every page will pull in the custom script on init.
    take a look at the DataObjectDecorator Documentation to see how to add extra statics (add more stuff to the $db array in your decorator). from here, you can add a textfield so users can type in their own information script into each page, then you can pull that content and stick it in the required custom script. or if you want one script used on every page like GA, you can centralize it in a customized siteconfig. search ssbits.com for customizing siteconfig to do this.

    hope it helps,
    -Chris

    432 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.