1

Both the PHP and the JS is tested and works in a module. But there's really no need for a module this is a plugin. I'm quite new to this so I thought onAfterRender would work, but nothing seems to happen.

What is the problem and in the future, what is the best way to troubleshoot this?

Also, as this will load on every page, should I be using a .js file and then using defer to improve performance?

koy_alias_as_body_class.php

<?php
/**
 * @package     Joomla.Plugin
 *
 * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Plugin class for adding the alias to the body
 *
 * @since  1.6
 */
class plgSystemKoy_alias_as_body_class extends JPlugin
{

    public function onBeforeCompileHead()
    {
        $app = JFactory::getApplication();

        // only insert the script in the frontend
        if ($app->isClient('site'))
        {
            var_dump(1); exit;
            $prefix = "";
            if ($params->get('prefix'))
            {
                $prefix = $params->get('prefix');
            }

            $menu      = $app->getMenu();
            $active    = $menu->getActive();
            $alias     = $active->alias;

            JFactory::getDocument()->addScriptDeclaration('
                document.addEventListener("DOMContentLoaded", function() 
                {
                    document.body.classList.add("' . $prefix . $alias  . '");
                });
            ');
        }
    }
}

koy_alias_as_body_class.xml

    <?xml version="1.0" encoding="utf-8"?>
    <extension version="3.8" type="plugin" group="system" method="upgrade">
        <name>PLG_SYSTEM_KOY_ALIAS_AS_BODY_CLASS</name>
        <author>Moi</author>
        <creationDate>January 2019</creationDate>
        <copyright>Copyright (C) 2005 - 2019 Open Source Matters. All rights reserved.</copyright>
        <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
        <authorEmail>info@site.com</authorEmail>
        <authorUrl>https://www.site.com</authorUrl>
    <version>1.0.0</version>
    <description>KOY_ALIAS_AS_BODY_CLASS_XML_DESCRIPTION</description>
    <files>
        <filename plugin="koy_alias_as_body_class">koy_alias_as_body_class.php</filename>
        <folder>language</folder>
    </files>
    <!-- Update servers -->
    <updateservers>
        <server type="extension" priority="1" name="koy_alias_as_body_class">https://www.kindofuseful.com/downloads/joomla/modules/koy_alias_as_body_class/updates.xml</server>
    </updateservers>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field name="prefix" type="text" default="m" label="KOY_ALIAS_AS_BODY_CLASS_PREFIX_LABEL" description="KOY_ALIAS_AS_BODY_CLASS_PREFIX_DESCRIPTION" size="10" />
            </fieldset>
        </fields>
    </config>
</extension>
  • Can you try adding a simple var_dump(1); exit; inside if ($app->isClient('site')) { }, just to see if the plugin is actually being executed. – Lodder Jan 3 at 15:41
  • Done, what should happen? The site appears to load normally. – Eoin Jan 3 at 16:27
  • The site should go blank and display a single 1 in the top left. Are you sure the plugin is enabled and the class name is correct? – Lodder Jan 3 at 17:36
  • I copied and pasted the XML to show. I copied and pasted the file names too so they should be correct. It all looks ok to me but perhaps my eyes are tired. Is the class name done correctly? – Eoin Jan 3 at 17:57
  • Also are the languages done correctly? For some reason they show the literal string and not the langauge. I've done it this way in modules before and it has worked. – Eoin Jan 3 at 18:05
1

Ok I've debugged this now. I'll write this down in simple steps:

  1. Change the PHP file name to koy_alias_as_body_class.php
  2. Change the XML file name to koy_alias_as_body_class.xml
  3. Change the class name to class plgSystemKoy_alias_as_body_class
  4. Replace the event onAfterRender with onBeforeCompileHead
  5. In the XML file, replace this:

 <filename plugin="plg_system_koy_alias_as_body_class">plg_system_koy_alias_as_body_class.php</filename>

with this:

<filename plugin="koy_alias_as_body_class">koy_alias_as_body_class.php</filename>

Hope this helps


Also you cannot call params by using $params->get('prefix'). To see the params you need first to use $this->params. Unlike in a module you cannot use get. As the params are in an array inside $this you can use this syntax

$this->params['prefix']

  • Ah, I see. Ok, so from my reading I should never use plg_system in the file names. I was wondering why none of the others were in folders called that. But I should still call my zip file plg_system_xxx_xxx as reasonably good practice right? Thanks a million, when will we see you at Joomla! London? We're on meetup now meetup.com/Joomla-User-Group-London – Eoin Jan 3 at 20:05
  • Hmm, still no joy. That's odd. Should I update my code? – Eoin Jan 3 at 20:13
  • Yeah please do and I'll take another look tomorrow – Lodder Jan 3 at 20:18
  • Crap. Got the var_dump working but still no class on the body. And still no language strings. – Eoin Jan 3 at 20:23
  • 1
    @Eoin - Ah yes. I did fix the params when installing on my site but forgot to mention it in the steps. – Lodder Jan 4 at 7:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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