Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-09-19 15:13:04

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Push callback event results in an array

Hi,
How could I push each result of a created callback event in an array, or at least, how could I add a delimiter between each result (string) of this event?
Thanks!

Last edited by NicolasGraph (2016-09-19 15:45:59)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#2 2016-09-19 15:53:01

planeth
Plugin Author
From: Nantes, France
Registered: 2009-03-19
Posts: 215
Website

Re: Push callback event results in an array

By using callback_event_ref ?

Offline

#3 2016-09-19 16:22:14

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Push callback event results in an array

Thanks @planeth; I never used this function but I’ll investigate tomorrow; feel free to tell me more about how you would do it. I thought I could use the $pre argument of callback_event() but I’m not sure to understand how it works for now…


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#4 2016-09-19 16:33:56

planeth
Plugin Author
From: Nantes, France
Registered: 2009-03-19
Posts: 215
Website

Re: Push callback event results in an array

This is the @callback_event_ref@ definition

Basic you’ve got 2 more parameters which are passed by reference to do anything you want with.
Don’t know exactly what you’re trying to achieve, but should do the trick IMHO.

Offline

#5 2016-09-19 16:51:45

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

Re: Push callback event results in an array

NicolasGraph wrote #301620:

I thought I could use the $pre argument of callback_event() but I’m not sure to understand how it works for now…

In theory, it works like this in its array form:

register_callback('my_callback_one', 'my_event', 'my_step', 0);
register_callback('my_callback_two', 'my_event', 'my_step', 0);

function my_callback_one($event, $step, $rs) {
    return (array) $rs + array('one' => 'one apple');
}
function my_callback_two($event, $step, $rs) {
    return (array) $rs + array('two' => 'two apples');
}
...
$apples = callback_event('my_event', 'my_step', array(0, 0), 'no apples');

After this, $apples should contain array(0 => 'no_apples', 'one' => 'one apple', 'two' => 'two apples').

Offline

#6 2016-09-20 08:21:10

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Push callback event results in an array

etc wrote #301622:

In theory, it works like this in its array form […]
After this, $apples should contain array(0 => 'no_apples', 'one' => 'one apple', 'two' => 'two apples').

This sounds to be exactly what I was looking for; I just don’t need the first element but I’ll see how to play with that. Thanks (again…) Oleg!


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#7 2016-09-20 09:02:12

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

Re: Push callback event results in an array

You are welcome! Actually, if callback functions always return an array, there is an easier way:

function my_callback_one($event, $step, $rs) {
    return array('one apple');
}
function my_callback_two($event, $step, $rs) {
    return array('one more apple');
}
$apples = callback_event('my_event', 'my_step', 0, 'no apples, but nobody cares');

This will return Array ( [0] => one apple [1] => one more apple ). If it works for you, I would prefer this over $pre array, which is only a hack to patch pluggable_ui in a bw-compatible way.

Edit: note that in this case $rs = 'no apples, but nobody cares' in all calls.

Last edited by etc (2016-09-20 09:04:34)

Offline

#8 2016-09-20 09:15:10

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Push callback event results in an array

etc wrote #301627:

If it works for you, I would prefer this over $pre array…

It works like a charm, thanks!

Last edited by NicolasGraph (2018-05-02 19:32:33)


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

Board footer

Powered by FluxBB