Example

Let's take a look at a plugin for logging of FILEman events. This plugin will get information from the event and let you add the information to a custom database table.

Events to respond to:

  • When: After - we do this after the action has run so that if the action were to fail, our event listener won't run.
  • Component: Fileman & Files - This is to only affect the FILEman and Files extensions.
  • Name: Files - We're only interested in files.
  • Action: Add & Delete - Add/delete events.

The event method names we need for this example are:

<?php
class PlgFilemanFile extends PlgKoowaSubscriber
{
    onAfterFilesFileControllerAdd(KEventInterface $event){}
    onAfterFilesFileControllerDelete(KEventInterface $event){}
    onAfterFilemanFileControllerDelete(KEventInterface $event){}
}

Add event

We want to get the following information about this event: (1) What file was added? (2) Who added the file?

<?php
public function onAfterFilesFileControllerAdd(KEventInterface $event)
{
    // The result of the controller action is stored in the "result" property
    $entity = $event->result;
    // The entity contains the result's properties
    $filename = $entity->name;
    // Get the user who performed the action
    $username = $this->getObject('user');
    // Now do some custom query to store the values. Perhaps store in a table using $entity->id as an index.
}

Note that we used Files instead of Fileman. This is because FILEman makes use of the built-in Joomlatools com_files component especially for managing files and folders in the backend.

Delete event

Our delete event will use both Files and Fileman. The onAfterFilemanFileControllerAdd event will be triggered when a user deletes a file from the frontend while onAfterFilesFileControllerAdd will be triggered from the admin.

Normally, your methods would have different functions. But for this example, we will have one method call the other since they have the same function.

<?php
// Frontend event
public function onAfterFilemanFileControllerAdd(KEventInterface $event)
{
    $this->onAfterFilesFileControllerAdd($event);
}
<?php
// Backend event
public function onAfterFilesFileControllerAdd(KEventInterface $event)
{
    // The result of the controller action is stored in the "result" property
    $entity = $event->result;
    // The entity contains the result's properties
    $filename = $entity->name;
    // Get the user who performed the action
    $username = $this->getObject('user');
    // Do some query to store the values.
}