Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

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

A module that adds a JS tag to all pages


Reply


2 Posts   482 Views

Avatar
JussiL

Community Member, 1 Post

1 December 2011 at 2:17am

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?

Avatar
Chris_Bryer

Community Member, 35 Posts

3 December 2011 at 9:35am

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 [url=http://doc.silverstripe.org/sapphire/en/reference/dataobjectdecorator]DataObjectDecorator Documentation[/url] 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