Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-12-12 20:09:15

monkeyninja
Plugin Author
From: Sheffield, UK
Registered: 2008-10-14
Posts: 239
Website

Callback/Hook after section/category saved

Hi,

Not sure if I am just being stupid, but I’m struggling to find a way of triggering a function after a section or category is saved.

I’m currently using pluggable_ui() to modify the section and category forms to add some additional fields to the form, but need to be able to play with these after the form has been submitted and saved.

Is this possible? I couldn’t spot anything obvious in the core Textpattern code. I’ve attempted to use register_callback('my_function', 'section_save') as described in the docs, but that doesn’t appear to trigger.

Any help would be greatly appreciated.

Offline

#2 2013-12-12 21:14:38

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Callback/Hook after section/category saved

Try:

register_callback('my_function', 'section', 'section_save');

Since it’s an admin-side event+step that you’re hooking into, you need both to trigger the callback. And if you want it to run your code prior to Textpattern’s own section_save routine, set the 4th parameter to true. Though that does have implications such as there’s the possibility that the core’s save routine might fail and you’ll have to potentially undo anything you did to your own data in the fallout.

Hope that helps.

Last edited by Bloke (2013-12-12 21:17:12)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2013-12-12 21:26:50

monkeyninja
Plugin Author
From: Sheffield, UK
Registered: 2008-10-14
Posts: 239
Website

Re: Callback/Hook after section/category saved

Hi Stef,

My mistake, that was what I intended to say I had tried in my original post. That doesn’t work for me.

Looking at the core code it seems that the only available callback is when the section is deleted; although this surely can’t be right. It doesn’t make sense being able to add to the section form if you can’t do anything with it.

Offline

#4 2013-12-12 21:46:10

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Callback/Hook after section/category saved

monkeyninja wrote:

Looking at the core code it seems that the only available callback is when the section is deleted

Correct, that’s the only official callback, but Txp does allow you to hook into any of its own action steps. At least in theory. Not sure why it’s not working for you.

Here’s a test plugin that works for me:

register_callback('smd_section_test', 'section', 'section_save');

function smd_section_test($evt, $stp) {
   echo "Hello! Here's me data:";
   dmp($_POST);
}

Make sure you set your plugin type to Admin, then after you’ve saved a section, scroll to the bottom under the list and you should see the output.

fwiw, this arena is something I’d like to sink my teeth into in the next release. Gocom has put some great new callbacks into 4.6-dev to allow plugins greater freedom, but there are still things I’d like to try, and dedicated save callbacks (both pre- and post-save) is something I’m toying with. That way, you could do something before the core touched the database, and then detect if the core’s call succeeded and rollback in the post-save hook if necessary. Same with delete. At the moment, you only get notified after something’s gone, but I can think of plenty of applications where it’d be nice if you were notified prior to deletion so you could apply custom logic and maybe even signal to the core to abort its own delete if it contravenes your constraints. Not sure how successful that would be given the core’s current callback chaining limitations so the entire system might need tweaking first, or perhaps better) allow you to inject your own constraints which the core obeys prior to save.

Last edited by Bloke (2013-12-12 21:48:31)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2013-12-12 22:54:48

monkeyninja
Plugin Author
From: Sheffield, UK
Registered: 2008-10-14
Posts: 239
Website

Re: Callback/Hook after section/category saved

Thanks Stef, got it working now, although no idea why it was failing before.

Dedicated save callbacks would be great. It’s something I was actually thinking about the other day for a plugin I have in the works. It would be useful for checking if a particular value is going to change with the save.

Textpattern would certainly benefit from the ability to hook into more of the processes.

Offline

#6 2013-12-17 13:56:29

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Callback/Hook after section/category saved

This recalls me a year and half ago discussion we had about safe_query_with_callback functionality. That would be much more plugin-friendly indeed. Meanwhile, you can misuse admin_criteria, section_list callback, retrieving _POST data and taking your decision accordingly.

Offline

Board footer

Powered by FluxBB