1

I have created a system plugin for onAfterRender event however I'm unable to make it execute. I'm on Joomla 3.9

Here's the sample code of the plugin


    <?xml version="1.0" encoding="utf-8"?>
    <!--
    /**
     * @copyright   Copyright (c) 2018 myvendorcustomscripts. All rights reserved.
     * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
     */
    -->
    <extension type="plugin" version="1.0" group="content" method="upgrade">
        <name>plg_system_myvendorcustomscripts</name>
        <author>myvendor</author>
        <creationDate>October 2018</creationDate>
        <copyright>Copyright (c) 2018 myvendorcustomscripts. All rights reserved.</copyright>
        <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
        <authorEmail></authorEmail>
        <authorUrl></authorUrl>
        <version>1.0.0</version>
        <description>
        <![CDATA[

        ]]>
        </description>

        <files>
            <filename plugin="myvendorcustomscripts">myvendorcustomscripts.php</filename>
            <filename>index.html</filename>
        </files>

        <languages>
            <language tag="en-GB">en-GB.plg_system_myvendorcustomscripts.ini</language>
            <language tag="en-GB">en-GB.plg_system_myvendorcustomscripts.sys.ini</language>
        </languages>

        <config>
            <fields name="params">
                <fieldset name="basic">

                </fieldset>
            </fields>
        </config>
    </extension>

And


        <?php
        /**
         * @copyright    Copyright (c) 2018 myvendorcustomscripts. All rights reserved.
         * @license        http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
         */

    // no direct access
        defined('_JEXEC') or die;

        jimport('joomla.plugin.plugin');

        /**
         * content - MyVendorCustomScripts Plugin
         *
         * @package        Joomla.Plugin
         * @subpakage    myvendorcustomscripts.MyVendorCustomScripts
         * @since
         */
        class plgSystemMyVendorCustomScripts extends JPlugin
        {
            /**
             * Constructor.
             *
             * @param    $subject
             * @param    array $config
             * @since
             */
            public function __construct(&$subject, $config = array())
            {
                // call parent constructor
                parent::__construct($subject, $config);
            }


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

            }


            public function onBeforeCompileHead()
            {
                $app = JFactory::getApplication();
                $document = $app->getDocument();

                foreach ($document->_styleSheets as $index => $value) {
                    if (strpos($index, 'fonts.googleapis.com')) {
                        unset($document->_styleSheets[$index]);
                    }

                    if (strpos($index, 'simple-line-icons')) {
                        unset($document->_styleSheets[$index]);
                    }

    //                $styleSheet = $styleSheet;
                }
            }
        }

However the functions never get executed when I'm browsing the frontend but they do get executed when browsing the backend. It's really strange and I can't understand the reason. It seems on the documentation that this even should trigger always

2
  • Are you using any sort of caching? System - Page Cache plugin or similar can be causing this.
    – Sharky
    Dec 4, 2019 at 6:23
  • Well, $document->_styleSheets doesn't execute in the frontend. Do you need the data from the backend? I've been using System - Page Cache yes, and indeed it's the reason that it's not breaking. As soon as I completely disabled it, my breakpoint was hit. So what can I do to fix this? Caching is really important to completely disable.
    – gabtzi
    Dec 4, 2019 at 6:40

1 Answer 1

2

The code you currently have works. You just need to clear page cache and view the page again. You'll see fonts.googleapis.com links are removed. This may or may not be fine depending on the rest of your code.

When System - Page Cache plugin is enabled, on first load page (determined by URL) on is rendered normally with all expected plugin events triggered and stored to cache during onAfterRespond event. On following page loads the plugin fetches the page from cache, pushes it to application and closes the application. This happens during onAfterInitialise event. No further events are triggered. Except onAfterRespond if debug is enabled.

The code you currently have in onBeforeCompileHead actually works fine because your changes are applied before the page is stored to cache.

However, in case you want to add code that is not cache-safe, e.g. code that changes from visitor to visitor or some randomly generated code, you could selectively disable cache using onPageCacheSetCaching event.

1
  • OK I see, this was very informative. Thank you very much. So even if I enable the system plugin Page Cache this will execute when I clear the cache completely, only once for each page. Then the saved page will be served ignoring all events. In my case I don't wish to disable caching for specific pages as this will go site-wide but I want some specific DOM Manipulation to occur to clear some Pagespeed Insights issues before serving the page.
    – gabtzi
    Dec 4, 2019 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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