# Chaining multiple hooks

**URL:** <https://forum.getkirby.com/t/chaining-multiple-hooks/9075>\
**Category:** Questions\
**Tags:** v2\
**Created:** [November 22, 2017, 8:33am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075 "2017-11-22T08:33:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![georgobermayr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/georgobermayr/32/6112_2.png) [@georgobermayr](https://forum.getkirby.com/u/georgobermayr)\
**Post date:** [November 22, 2017, 8:33am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/1 "2017-11-22T08:33:17Z")

</div>

Hi,

I’m implementing a editorial approval process using hooks. In one scenario I want to reset already given approvals and restart the approval process. The code for the hook looks like this:

```
kirby()->hook(['panel.page.update'], function($page, $oldPage = null) {

  // Start the process if all approvers are set and a content creator edits the page
  if ( $page->approver()->isNotEmpty()
    && $page->author()->isNotEmpty()
    && $page->approvecontent()->bool()
    && $page->approvelanguage()->bool()
    && $this->role() == 'contentcreator' ) {

    // Reset the approvals
    $newPage = $page->update(array(
      'approvecontent' => '',
      'approvelanguage' => ''
    ));

    // Trigger an update event to restart the approval process
    $this->kirby()->trigger('panel.page.update', $newPage);

  }

});

```

The hook executes correctly and updates the page, however the new hook is not triggered. Is this by design? Is there any way to trigger a new hook after the current one is executed?

Thank you,  
Georg

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 22, 2017, 8:43am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/2 "2017-11-22T08:43:47Z")

</div>

You are using `$this` out of context, so this code won’t work.

Correct:

```auto
kirby()->trigger('panel.page.update', $page);

```

---

<div class="post-metadata">

**Author:** ![georgobermayr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/georgobermayr/32/6112_2.png) [@georgobermayr](https://forum.getkirby.com/u/georgobermayr)\
**Post date:** [November 22, 2017, 8:50am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/3 "2017-11-22T08:50:20Z")

</div>

Thank you. I already tried this one and it is also not working. The hook is not triggered.  
[Here](https://forum.getkirby.com/t/trigger-hook-does-not-fire-oldpage/8700/6?u=georgobermayr) it says that this is happening by design? But the suggested code  
`kirby()::$triggered = array();`  
is also not working inside the hook.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 22, 2017, 9:45am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/4 "2017-11-22T09:45:38Z")

</div>

Yes, the problem is that such nested hooks might easily end up in an endless loop if you are not careful.

---

<div class="post-metadata">

**Author:** ![georgobermayr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/georgobermayr/32/6112_2.png) [@georgobermayr](https://forum.getkirby.com/u/georgobermayr)\
**Post date:** [November 22, 2017, 10:02am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/5 "2017-11-22T10:02:43Z")

</div>

Yes, I try to be careful 😉  
So there is no way to trigger a hook from inside a hook? And my example from above is expected to not work?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 22, 2017, 10:32am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/6 "2017-11-22T10:32:07Z")

</div>

I think this is not possible, at least it does not work. But in your code above, the second update hook wouldn’t do anything anyway (unless your code is incomplete).

---

<div class="post-metadata">

**Author:** ![georgobermayr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/georgobermayr/32/6112_2.png) [@georgobermayr](https://forum.getkirby.com/u/georgobermayr)\
**Post date:** [November 22, 2017, 11:07am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/7 "2017-11-22T11:07:23Z")

</div>

Yes, my code is not complete. I’ve other hooks in other files that together build the process, so one of them should fire, wen I trigger a new hook with `kirby()->trigger('panel.page.update', $page);`  
Does this also mean, that if I have multiple hooks (in different files and plugins) that are working on the same action, only one of them (the first?) fires and the others are not executed?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 22, 2017, 11:11am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/8 "2017-11-22T11:11:26Z")

</div>

Yes, I think so, see also this issue: [https://github.com/getkirby/panel/issues/1006](https://github.com/getkirby/panel/issues/1006)

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [November 22, 2017, 11:14am UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/9 "2017-11-22T11:14:56Z")

</div>

> [@texnixe](#):
>
> Yes, I think so, see also this issue: [File sort hook triggered only for the first file · Issue #1006 · getkirby-v2/panel · GitHub](https://github.com/getkirby/panel/issues/1006)

You are correct @texnixe! I was about to mention that issue.

Under the hood, Kirby keeps a record of which hooks have been called during each session and skips the duplicate ones.

---

<div class="post-metadata">

**Author:** ![lukaskleinschmidt](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukaskleinschmidt/32/13388_2.png) [@lukaskleinschmidt](https://forum.getkirby.com/u/lukaskleinschmidt)\
**Post date:** [November 22, 2017, 12:36pm UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/10 "2017-11-22T12:36:11Z")

</div>

You can work around that issue.  
I had to do this as well in [one of my plugins](https://github.com/lukaskleinschmidt/kirby-sortable/blob/master/sortable/actions/paste/controller.php#L45).

```php
// Reset previously triggered hooks
kirby()::$triggered = array();

```

But **you need to make sure** your code does not end up in an endless loop!

> Does this also mean, that if I have multiple hooks (in different files and plugins) that are working on the same action, only one of them (the first?) fires and the others are not executed?

Kirby will execute **all** registerd hooks. But kirby will execute all of them **only once**!

---

<div class="post-metadata">

**Author:** ![georgobermayr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/georgobermayr/32/6112_2.png) [@georgobermayr](https://forum.getkirby.com/u/georgobermayr)\
**Post date:** [November 22, 2017, 4:20pm UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/11 "2017-11-22T16:20:06Z")

</div>

Thank you, this workaround seems fine for me, otherwise I would need to re-think the whole architecture … 😉  
However, this seems not to work in the context of the hook:  
`[22-Nov-2017 17:08:42 Europe/Berlin] PHP Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in /Users/georgobermayr/Documents/Git/my-site/site/plugins/workflow/hooks/resetApprovals.php on line 28`  
Do you have any idea if it is possible to access `$triggered` inside of a hook?

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [November 22, 2017, 4:29pm UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/12 "2017-11-22T16:29:25Z")

</div>

> [@lukaskleinschmidt](#):
>
> kirby()::$triggered

I think Lukas meant this: `kirby::$triggered` &nbsp; 😉

---

<div class="post-metadata">

**Author:** ![georgobermayr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/georgobermayr/32/6112_2.png) [@georgobermayr](https://forum.getkirby.com/u/georgobermayr)\
**Post date:** [November 22, 2017, 7:35pm UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/13 "2017-11-22T19:35:58Z")

</div>

Thank you both. The syntax with `kirby::$triggered` didn’t produce a syntax error, but from my perspective it also didn’t work when executed from inside a hook ☹  
I now endend with a solution that relies on the order in which I include the different hook functions. With that I no longer need to trigger the hook again, because it already runs **after** the reseting took place in the first hook. I would say that this is not an ideal solution and I hope this is solid when we further scale the application but for now it works 😉

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:30pm UTC](https://forum.getkirby.com/t/chaining-multiple-hooks/9075/14 "2025-02-06T17:30:30Z")

</div>


