Additional Scripts in Head for Signup Forms

Discussion in 'Customization & add-ons' started by mithrandir, Mar 14, 2015.

  1. mithrandir

    mithrandir New Member

    Joined:
    Nov 10, 2014
    Messages:
    3
    Hi,
    i'm trying to create a plugin that adds additional scripts.
    I made it using also bricks.

    in application/default/plugins/misc/HeadScriptsSignup/brick.php i put:
    PHP:
    <?php
      
    class Am_Form_Brick_HeadScriptsSignup extends Am_Form_Brick
    {
        protected 
    $hideIfLoggedInPossible self::HIDE_DONT;
        public function 
    __construct($id null$config null)
        {
            
    $this->name ___('Scripts For Head (Hidden)');
            
    parent::__construct($id$config);
        }
        public function 
    initConfigForm(Am_Form $form)
        {
            
    $form->addText('signup_scripts_for_head')->setLabel(___('Scripts for Head'."\nUse this to include in tag <head> scripts like custom audience"));
        }

        public function 
    insertBrick(HTML_QuickForm2_Container $form)
        {
            
    $form->addHidden('signup_scripts_for_head')->setValue($this->getConfig('signup_scripts_for_head'));
        }

    }
    ?>
    and in application/default/plugins/misc/HeadScriptsSignup/HeadScriptsSignup.php i put:

    PHP:
    <?php

    class Am_Plugin_HeadScriptsSignup extends Am_Plugin
    {
        const 
    PLUGIN_STATUS self::STATUS_PRODUCTION;
        const 
    PLUGIN_REVISION '1.0';
        
    //const TRACKED_DATA_KEY = 'google-analytics-done';

        
    protected $scriptsForHead;
        protected 
    $done false;
        public function 
    __construct(Am_Di $di, array $config)
        {
            
    $this->scriptsForHead $di->config->get('signup_scripts_for_head');
            
    parent::__construct($di$config);
        }
       
        public function 
    scriptsHasValue()
        {
            return !empty(
    $this->scriptsForHead);
        }
       
        
    // this will load the custom brick
        
    function onLoadBricks(Am_Event $event)
        {
            require_once 
    dirname(__FILE__) . '/brick.php';
        }
       
        function 
    onAfterRender(Am_Event_AfterRender $event)
        {
            if (
    $this->done) return;
            if (
    preg_match('/signup\/signup.*\.phtml$/'$event->getTemplateName()))
            {
                
    $this->done += $event->replace("|</head>|i"$this->getScriptCode(). "</head>"1);
            }
        }
       
        function 
    getScriptCode()
        {
            return 
    $this->scriptsForHead;
        }
    }

    ?>
    but the variable $this->scriptsForHead is always empty.

    What am i doing wrong?

    thanks
    regards
    Guido
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    It is different things global config and brick config. So when you call $di->config->get('signup_scripts_for_head') you get value for signup_scripts_for_head from global config and it is empty obviously. You can access brick config only within brick itself.

    I do not believe it is important to insert your script to head explicitly. You can insert it to form body and it continue to work same way. You can use code in insertBrick
    PHP:
    $form->addScript()->setScript($this->getConfig('signup_scripts_for_head'));
    You can remove all methods from your plugin except onLoadBricks

Share This Page