Best practice to add Event Listener to Table

Be informed that this forum is not an official support forum for Joomla! 4.0. Any issues regarding Joomla! 4.0 must be reported at https://issues.joomla.org/.

Joomla 4.0 is still in Beta stage. This forum should be used for sharing information about Joomla! 4.0.

Moderators: ooffick, ooffick

Forum rules
Post Reply
User avatar
Todor Iliev
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Thu Jan 13, 2011 7:09 pm
Location: Bulgaria
Contact:

Best practice to add Event Listener to Table

Post by Todor Iliev » Mon Dec 21, 2020 9:46 pm

Hi friends,
What is the best way to add Event Listener to Table?

I would like to add listener to handle "onTableAfterStore" event.

I can do it in the table constructor (look the code below), as Behaviour Plugin or Observer.

Code: Select all

use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseDriver;

class ItemTable extends Table
{
    /**
     * @param DatabaseDriver $db
     */
    public function __construct(DatabaseDriver $db)
    {
        parent::__construct('#__docs_items', 'id', $db);

       // Registering Closure Listeners
        $this
            ->getDispatcher()
            ->addListener('onTableAfterStore',
                function ($event) {
                    //...
                }
        );

        // Registering Object Listeners
        $this
            ->getDispatcher()
            ->addListener(new ContentListener);
    }
}
Thank you in advance for your answers!
I was lucky – I found what I love to do early in life. « Steven Jobs »

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 27554
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Best practice to add Event Listener to Table

Post by Per Yngve Berg » Tue Dec 22, 2020 4:47 am

I have made trigger functions inside the db

https://dev.mysql.com/doc/refman/5.7/en ... yntax.html

User avatar
ceford
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 165
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Best practice to add Event Listener to Table

Post by ceford » Tue Dec 22, 2020 4:05 pm

Would an onContentAfterSave plugin be suitable? There are some examples in use.

User avatar
Todor Iliev
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Thu Jan 13, 2011 7:09 pm
Location: Bulgaria
Contact:

Re: Best practice to add Event Listener to Table

Post by Todor Iliev » Tue Dec 22, 2020 7:48 pm

Content plugins are suitable for model events such the admin model.

Behavior plugins are suitable for table events.

I would like to avoid creation of a plugin because I do not need to manage it by the extension manager.

It seems I can also use postSaveHook from the form controller for my purpose.

I found a solution that works for me in the new version of Joomla Framework Event Package.

I did event subscriber and added it to the event dispatcher in the table constructor.

event_subscriber.png
add_event_subscriber_to_table.png
You do not have the required permissions to view the files attached to this post.
I was lucky – I found what I love to do early in life. « Steven Jobs »


Post Reply

Return to “Joomla! 4 Related”