2

I am trying to write a simple plugin that runs after the user logs in to the system. I have written content and authentication plugins before with no issues. In this case I have stripped the plugin down to the following and still see no evidence it ever runs.

<?php 
  defined('_JEXEC') or die; 
  class Plgtest extends JPlugin {   
    public function onUserLogin($user, $options = array()) {
      JFactory::getApplication()->enqueueMessage('I Ran!.', 'message');   
    } 
  } 
 ?>

I have tried both the onUserLogin and onUserAfterLogin events and never see the plugin run. My xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="3.0" method="upgrade" group="user">
    <name>User - test</name>
    <creationDate>2014-07-19</creationDate>
    <version>1.0</version>
    <releaseDate>2014-07-19</releaseDate>
    <releaseType>First release!</releaseType>
    <author>me</author>
    <authorEmail>me@me.com</authorEmail>
    <authorUrl>www.me.com</authorUrl>
    <copyright>(c) 2014 me</copyright>
    <description>test</description>
    <files>
    <filename plugin="test">test.php</filename>
    </files>
    <config>            
        <fields name="params">
            <fieldset name="general_settings" label="Connection Settings">
                <field name="host" type="text" default="127.0.0.1" label="Host" description="Database host." />
                <field name="database" type="text" default="" label="Database" description="Database name." />
                <field name="prefix" type="text" default="" label="Prefix" description="Database prefix, may be blank." />
                <field name="user" type="text" default="" label="User" description="User name." />
                <field name="password" type="password" default="" label="Password" description="User password." />
            </fieldset>
        </fields>
    </config>
</extension>

Any help or example code from a working plugin would be appreciated. Never had such problems before, hopefully whatever I am missing is painfully obvious.

Also, the connection settings are irrelevant in this example. The real plugin connects to an external database.

  • Maybe its the name of the plugin? I once had one as plg_my_test and it wouldn't run until i renamed it plg_mytest – poproar Jul 18 '14 at 21:22
6

You don't mention a Joomla version so I'm assuming 3.x, although this should be pretty much the same for 2.5.x.

As mentioned your plugin's class name is incorrect, the name of the class is derived from the plugin name and type, and the file/folder naming conventions & structure for the plugin help Joomla find and load the right php files.

If your plugin is in /plugins/user/ and it's folder is test, main file is test.php (with a test.xml file) then your class should be called PlgUserTest.

JPluginHelper basically loads a category of plugins (authentication, captcha, content,... user etc) in ::importPlugin() based on the type e.g. user. First it gets the published plugins from the database (so your plugin needs to be registered with Joomla, this happens in the install process or you can use the "Discover" option in the Extension Manager).

The importPlugin() method then loops through the published plugins matching for the $type it's been asked to load, for each match it import()'s the plugins main php file by building the $path to it like this:

$path = JPATH_PLUGINS . '/' . $plugin->type . '/' . $plugin->name . '/' . $plugin->name . '.php';

From that you can see the path to your file should be:

/plugins/users/test/test.php

Once it's got the file, it then loads the class by creating the $className from the attributes it already has using this line:

$className = 'Plg' . $plugin->type . $plugin->name;

From this you can see that your class name should be PlgUserTest

0

I think that if you make your plugin part of the user group, you need to change the class name to:

PlgUserTest

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.