Adding a Header Item in a Package Controller

Permalink 2 users found helpful
I am having some trouble figuring out how to do this. I have a package I am trying to finish it works but I think it could be more efficient. Anyhow, what is the best way to add javascript as a header item to all but the system pages in a non block package? I have tried several things but none work. Here is what I have so far?

public function install() {
      $pkg = parent::install();
      Loader::model('single_page');
      // install pages
      $cp = SinglePage::add('/dashboard/ebibleicious/', $pkg);
      $cp->update(array('cName'=>t('eBibleicious'), 'cDescription'=>t('Instant Bible Reference')));      
   }
   function on_start() {         
       $html = Loader::helper('html');
       $pkg = Package::getByHandle('ebibleicious');
       $active = $pkg->config('EBIBLEICIOUS_ACTIVE');
       if($active == 'on'){
          $mode = $pkg->config('EBIBLEICIOUS_MODE');
          $translation = $pkg->config('EBIBLEICIOUS_TRANSLATION');
          $topics = $pkg->config('EBIBLEICIOUS_TOPICS');


Any suggestions? Thanks in advanced.
Dwayne

dwayneparton
View Replies: View Best Answer
dwayneparton replied on at Permalink Reply
dwayneparton
Figured it out. Or at least I think this is the best way.

If you run into a problem like this you need to create a class for your add-on and then hook into an event.

Like this:
public function on_start() {
       define('ENABLE_APPLICATION_EVENTS', TRUE);
       Events::extend('on_page_view', 'Ebibleicious', 'addHeaderItems', 'packages/'.$this->pkgHandle.'/models/ebibleicious.php');
   }


Then you create you model. In mycase my model looked like this:
class Ebibleicious extends Object {
   public function addHeaderItems(){
       $html = Loader::helper('html');
       $pkgHandle = 'ebibleicious';
       $pkg = Package::getByHandle($pkgHandle);
       $page = Page::getCurrentPage();
       $systemPage = $page->isAdminArea();
       $active = $pkg->config('EBIBLEICIOUS_ACTIVE');
       if($active == 'on' && $systemPage != true){
          $mode = $pkg->config('EBIBLEICIOUS_MODE');
          $translation = $pkg->config('EBIBLEICIOUS_TRANSLATION');
          $topics = $pkg->config('EBIBLEICIOUS_TOPICS');
          // add advanced tooltips to every page that is not an admin
          $v = View::getInstance();
          $v->addHeaderItem('<script type="text/javascript" src="http://ebible.com/api/ebibleicious?v=2.0&mode='.$mode.'&translation='.$translation.'&related_topics='.$topics.'"></script>');


Now i can run the script on every page. Maybe this will help someone eventually.

Thanks to:
http://www.concrete5.org/community/forums/customizing_c5/package-in...
http://www.concrete5.org/documentation/developers/system/events/...
olsgreen replied on at Permalink Best Answer Reply
olsgreen
For anyone else reading this 3 years down the line, you can add header items from package controllers like so....

public function on_start() {
        $v = View::getInstance();
        $v->addHeaderItem('<script src="/path/to/my/script.js"></script>');
    }


This will add the item to EVERY page on your site.