0

I'm trying to show a warning based on robots being set to noindex or nofollow. I've copied the PHP Version quickicons plugin.

Here's my php file.

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

defined('_JEXEC') or die;

/**
 * Plugin to check if the robots configuration is correct filled in.
 *
 * @since  3.7.0
 */
class PlgQuickiconKoyRobots extends JPlugin
{

    /**
     * Application object.
     *
     * @var    JApplicationCms
     * @since  3.7.0
     */
    protected $app;

    /**
     * Load plugin language files automatically
     *
     * @var    boolean
     * @since  3.7.0
     */
    protected $autoloadLanguage = true;

    /**
     * Check what Robots.txt is set to and display an error if it's hiding the site.
     *
     * @param   string  $context  The calling context
     *
     * @return  void
     *
     * @since   3.7.0
     */
    public function onGetIcons($context)
    {
        if (!$this->shouldDisplayMessage())
        {
            return;
        }

        // $app = JFactory::getApplication();
        // $robots = $app->get('robots');
        $robots = $this->app->get('robots');

        /*
            Blank = index, follow
            noindex, follow
            index, nofollow
            noindex, nofollow
        */

        // $app->enqueueMessage('This is an error', 'error');
        $this->app->enqueueMessage('This is an error', 'error');
        if ($robots == "noindex, follow"){
            // Add a message to the message queue
            $this->app->enqueueMessage(JText::_('PLG_QUICKICONS_KOY_ROBOTS_NO_INDEX_FOLLOW'), 'error');
        }
        if ($robots == "index, nofollow"){
            // Add a message to the message queue
            $this->app->enqueueMessage(JText::_('PLG_QUICKICONS_KOY_ROBOTS_NO_INDEX_NO_FOLLOW'), 'error');
        }
        if ($robots == "noindex, nofollow"){
            // Add a message to the message queue
            $this->app->enqueueMessage(JText::_('PLG_QUICKICONS_KOY_ROBOTS_INDEX_NO_FOLLOW'), 'error');
        }
    }



    /**
     * Determines if the message should be displayed
     *
     * @return  boolean
     *
     * @since   3.7.0
     */
    private function shouldDisplayMessage()
    {
        // Only on admin app
        if (!$this->app->isClient('administrator'))
        {
            return false;
        }

        // Only if authenticated
        if (JFactory::getUser()->guest)
        {
            return false;
        }

        // Only on HTML documents
        if ($this->app->getDocument()->getType() !== 'html')
        {
            return false;
        }

        // Only on full page requests
        if ($this->app->input->getCmd('tmpl', 'index') === 'component')
        {
            return false;
        }

        // Only to com_cpanel
        if ($this->app->input->get('option') !== 'com_cpanel')
        {
            return false;
        }

        return true;
    }
}

It installs correctly and all of the language files show, so I'm pretty sure the error is within this file.

  • No message displays.
  • The plugin is activated.
  • The robots text is set to a value that should show (noindex, nofollow)
  • I am on the dashboard.
2

This

JFactory::getapp();

should be

JFactory::getApplication();

or because the app property has already been made protected above you can just use it like

$this->app->get('robots');

but the fact that it didn't throw a fatal error suggests to me that either you didn't enable the plugin after installation, or you weren't looking in the control panel where the quick icons appear.

  • Actually I didn't find and replace so I probably overwrite that code so good catch. But it was definitely enabled and I definitely went to the cPanel. I'll try your suggestion though. Thanks for answering. – Eoin Oct 12 '18 at 22:29
  • I updated my answer, but it still doesn't work – Eoin Oct 13 '18 at 11:36
  • 1
    I have tested the above code and it works as expected. Have you disabled the quickicons admin module? That's what calls the get icons method. Also, if you echo something at the start of the onGetIcons method does that show up? – jamesgarrett Oct 13 '18 at 22:26
  • Weird. No I haven't disabled anything. I'll test again if it works for you it works. – Eoin Oct 13 '18 at 23:11
  • No, I've tested again and still no joy. I'm confused. Nothing echos. No errors come. I've checked all the settings again and I'm using the above code. I've tried with $this->app and I've tried with getApplication too – Eoin Oct 14 '18 at 14: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.