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>[email protected]</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>
var_dump(1); exit;insideif ($app->isClient('site')) { }, just to see if the plugin is actually being executed.1in the top left. Are you sure the plugin is enabled and the class name is correct?