1
**mod_cookiACT.xml**
   <?xml version="1.0" encoding="utf-8"?>
    <extension type="module" version="3.1.0" client="site" method="upgrade">
        <name>Cookie Notifications !</name>
        <author>Engjell</author>
        <version>1.0.0</version>
        <description>Cookie notifications displaying for the users</description>
        <files>
            <filename>mod_cookienotifications.xml</filename>
            <filename module="mod_cookienotifications">mod_cookienotifications.php</filename>
            <filename>helper.php</filename>
            <filename>tmpl/default.php</filename>
        </files>
        <config>
            <fields name="param">
                 <fieldset name="basic">
                    <field name="backgroundColor" type="color" default="#eee" format="hex"
                        label="Background Color of the Cookie Notifications!"
                        description="Background Color of the Cookie Notifications!" />
                    <field name="textColor" type="color" default="#eee" format="hex"
                        label="Text color of the Cookie Notifications!"
                        description="Text color of the Cookie Notifications!" />
                    <field name="btnColor" type="color" default="#eee" format="hex"
                        label="Button background color"
                        description="Button background color" />
                 </fieldset>
            </fields>
        </config>
    </extension>

helper.php

class ModCookieNotificationsHelper
{
    private $params = null;
    public function __construct()
    {
        $this->params = $this->getParams();
    }
    public function getParams()
    {
        $module = JModuleHelper::getModule('mod_cookieACT');
        $moduleParams = new JRegistry;
        if ($module->params !== '')
        {
            $moduleParams->loadString($module->params);
        }
        return $moduleParams;
    }
    public static function getCookie()
    {
        $helper = new ModCookieNotificationsHelper();
        $backgroundColor = $helper->getParams()->get('backgroundColor');
        $textColor = $helper->getParams()->get('textColor');
        $btnColor = $helper->getParams()->get('btnColor');
        $javascript = "
            <script src='//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js'></script>
            <script>
            window.addEventListener('load', function(){
            window.cookieconsent.initialise({
                'palette': {
                'popup': {
                    'background': '$backgroundColor',
                    'text': '$textcolor'
                },
                'button': {
                    'background': '$btnColor'
                }
                },
                'position': 'top'
            })});
            </script>";
            return $javascript;
    }
}

mod_cookieACT.php

// No direct access
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';
$doc = JFactory::getDocument();
$cookieNotification = ModCookieNotificationsHelper::getCookie($params);
require JModuleHelper::getLayoutPath('mod_cookieACT');

default.php

<?php 
// No direct access
defined('_JEXEC') or die; ?>
<?php  $result = $doc->addScriptDeclaration($cookieNotification); ?>

My javascript is working just fine, but i want to change the colors. Mister Lodder helped me with creating my XML files with the helper function to actually give the admin user a choice of colors. the problem is when I'm trying to save after choosing a color it doesnt work.

  • Oh wait, the custom form you made? Is there something I missed in the previous question? Or are you only using Joomla's standard parameters? – Lodder Mar 8 at 18:39
  • well ''custom'' as in when you edit this module I want the admin user to be able to set the color of his choice. I want to be able to send the colors in my function get Cookie() – engjell bislimi Mar 8 at 18:40
1

Parameters must be within params field group. In the manifest file add name="params" attribute to <fields> element.

<config>
    <fields name="params">
        ...
    </fields>
</config>

getParams() in the helper and related code is unnecessary and doesn't work anyways. Remove it along with the constructor and $params property. Add $params argument to getCookie() method and use it within the method:

defined('_JEXEC') or die;

class ModCookieNotificationsHelper
{
    public static function getCookie($params)
    {
        $backgroundColor = 
        $textColor = $params->get('textColor');
        $btnColor = $params->get('btnColor');
        $javascript = "
            <script src='//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js'></script>
            <script>
            window.addEventListener('load', function(){
            window.cookieconsent.initialise({
                'palette': {
                'popup': {
                    'background': '$backgroundColor',
                    'text': '$textColor'
                },
                'button': {
                    'background': '$btnColor'
                }
                },
                'position': 'top'
            })});
            </script>";
            return $javascript;
    }
}

You were already passing $params to getCookie() so this part is fine:

$cookieNotification = ModCookieNotificationsHelper::getCookie($params);

The issue should be solved by now. I'd like to share some unrelated observations. Given the current functionality of this module, it shouldn't be a module at all. Instead it should be a plugin, since it only adds script to document's head. The module layout is empty. If you do plan on adding something to the layout, the module can remain a module. But it may not need a helper class at all. Scripts can and should be in the layout so they can be overridden, i.e. in such case the scripts inside getCookie() would be moved to the layout, rendering helper class useless.

Finally, when building modules make sure their helper class matches the module name. Your module is called mod_cookieACT but your helper class is named after mod_cookienotifications. If such module exists, this would cause class conflicts. I would also avoid using capital letters in module name and main module file, just in case there's some code in Joomla that expects the names to be lowercase.

  • thanks for answering! I'm seeing the form on my module window but when I press save it doesn't still save the colors and I even get a message -->Undefined variable on my function getcookie – engjell bislimi Mar 8 at 21:42
  • Have you changed <fields> to <fields name="params"> in the manifest file (mod_cookieACT.xml)? If so, the values should save properly. Undefined variable is due to a typo in $textColor and $textcolor. Notice the casing. – Sharky Mar 8 at 21:52
  • Must be name="params", not name="param". – Sharky Mar 8 at 21:56
  • you're right! I was able to save it thanks! not sure if my cookie notifications is working but i'll try thanks!!! If I change it to a plugin it wouldn't be much trouble right? – engjell bislimi Mar 8 at 21:57
  • I haven't touched the JS code, apart from retrieval of params. The code, as you'll find out does not work. Because the code in addScriptDeclaration() must not be wrapped with <script> tags. And script files should be added addScript() instead or HTMLHelper's script() method. The code you currently have could be added using document's addCustomTag() method but that's not recommended. – Sharky Mar 8 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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