Is it possible to write one function for multiple hooks? For instance, something like:
kirby()->hook(array('panel.page.create', 'panel.page.update'), function($page){
// ...
});
Is it possible to write one function for multiple hooks? For instance, something like:
kirby()->hook(array('panel.page.create', 'panel.page.update'), function($page){
// ...
});
I guess you could do this:
function foo($page) {
// Function stuff
}
kirby()->hook('panel.page.create', foo($page));
kirby()->hook('panel.page.update', foo($page));
I havenāt tested this though.
Thatās what Iām now doing, and it does work
I guess it needs to be the following:
function foo($page) {
// Function stuff
}
kirby()->hook('panel.page.create', 'foo');
kirby()->hook('panel.page.update', 'foo');
You could also use an anonymous function to avoid naming conflicts:
$foo = function($page) {
// Function stuff
};
kirby()->hook('panel.page.create', $foo);
kirby()->hook('panel.page.update', $foo);
I think @lukasbestle has got it. What was happening in @PaulMorelās solution was immediate invocation of the function, rather than passing of a callable
to the hook registry.
This is often a point of frustration in JavaScript, whereby a single set of parens causes the result of a function to be passed (foo()
), not the function itself (foo
).
Exactly. Immediately invoking the handler doesnāt make sense in this case and will probably spit out a warning about a missing parameter $page
and later about a non-callable function when the hook handler is run.
I donāt get it to work. I tried this:
$save_revision = function( $page ) {
RevisionCreate::saveRevision( $page );
}
kirby()->hook('panel.page.create', $save_revision );
Parse error: syntax error, unexpected ākirbyā (T_STRING) in C:\wamp\www\revisions\site\plugins\revisions.php on line 74
This work:
kirby()->hook('panel.page.create', function($page) {
RevisionCreate::saveRevision($page);
});
I want to add more than one hook so it would be awesome to get the first one working.
Itās just a typo! It should be a semicolon after the anonymous function, just like in javascript. Maybe update your answer @lukasbestle. With that is works just fine!
I canāt test it right now, but the original proposed code should work now as well.
I did not get it to work.
Work:
$save_revision = function( $page ) {
RevisionSave::run( $page );
};
kirby()->hook('panel.page.create', $save_revision );
kirby()->hook('panel.page.update', $save_revision );
Donāt work:
kirby()->hook([
'panel.page.create',
'panel.page.update'
], function($page){
RevisionSave::run( $page );
}
);
Donāt work:
kirby()->hook(array('panel.page.create', 'panel.page.update'), function($page){
RevisionSave::run( $page );
});
Just in case, I tried two different ways to write arrays. None of them worked for me.
So, āI will definitely add this to the next releaseā somehow got overlooked @bastianallgeier? Maybe in 3.0 together with permissions then?
It should be implemented on the develop branch. Have you tested with the latest Kirby 2.3 beta?
Considering that the issue was closed in November last year, I would have expected it in the 2.2.3 version; thereās nothing in the changelog of 2.2.3 or 2.3.0 regarding this feature, though (although it might just have been overlooked when writing the changelog)
Also the commit is missing. Seems like Bastian implemented it but did not push it. @bastianallgeier
I can just confirm that Iām using 2.3 beta.