Jump to content
feniks502

Hook runs twice

Recommended Posts

Hello!

The only hook into 'trash' in a module runs twice, by itself, as it seems.

The following code outputs:

  • Session: Hooked 1 times on abcd
  • Session: Hooked 2 times on abcd

on the admin side when one page is deleted from it's delete tab.

And it's no matter 'addHookBefore', 'addHookaAfter' or 'addHook' method is used.

Why?!

private $i = 0;

public function init() {

	$this->pages->addHookBefore('trash', $this, 'test');

}

public function test($event) {

	$page = $event->arguments('page');

	$this->i++;
	$this->message("Hooked {$this->i} times on {$page->title}");

}

Share this post


Link to post
Share on other sites

Hi,

I could reproduce this in current dev 3.0.16. .   Pages::trash seems to be executed twice.
Is this still up to date for you? Maybe anyone else has a hint on what is wrong with it?

Greetings

Share this post


Link to post
Share on other sites

Hi,

I could reproduce this in current dev 3.0.16. .   Pages::trash seems to be executed twice.

Is this still up to date for you? Maybe anyone else has a hint on what is wrong with it?

Greetings

I had found nothing about this.

Just updated my installation to dev 3.0.17 and still have two messages instead of one.

It is critical for me, because  I have write current amount of 'Pages' within the structure like 'Parent(1) -> Parent(1.1) + Parent(1.2) +Parent(1.3) , ... -> Pages' to the 'Parent(1)' each time it is changed, so I don't know any other way, than hook into 'save', 'trash', 'delete', 'move' methods.

Particularly, concerning the 'trash' or 'delete' methods, I have to hook into them before execution to find '$page->parent->parent', to which data will be written (as you see after that it is impossible), but with 'trash' method my 'beforeHook' saves '$page->parent->parent' to a module class property correctly, and then it is get overwritten immediately, so it looks like 'addHookBefore' adds hook before and after.

Share this post


Link to post
Share on other sites

Hi thanks for that hint. I can only try it out later.

For feniks, this might not help as I understood, as he needs the page before it is trashed, but maybe it could work if you change your hook method (test above) with a check if the page is trashed? If there are really two different calls like Soma suggested, it should be one before actually trashed and in the second call of the modified page before save(?).
Maybe you get to try it out before me:

$page = $event->arguments('page');
if($page->isTrash()) return; //should only happen on the second call(?)

However I did not fully understand your use-case fully. Depending on what you want, it might be much easier to just count the children where you need them in Parent(1) or if you know the parent(s) you want the count for, you could also do it easier I guess:

// e.g. just:
$page->numChildren;
// in the template, or outside e.g:
$pages->count("has_parent=/locationOf/Parent");

Hope this helps.

Share this post


Link to post
Share on other sites

I ran into this same issue with triggers 'trash' and 'restore'. I was trying to do stuff before page is trashed/restored. I saw weird behavior but it took a good while until I understood triggers were being triggered multiple times per request.

My use case: I need to communicate with REST API when page changes. Running triggers multiple times is an issue as it leads to unexpected results. I had to go around the issue with class properties, but I don't think that should be the case. I assumed addHookBefore('Pages::restore'...) would be run once.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Hardoman
      Hello community,
      we have a website running version 3.0.118. The owner would like to have a watermark merged to the images, that are being uploaded in the CKEditor as a requirement.
      Image upload besides the CKEditor within galleries and single images works as a charm already. We also use croppable image 3 there. (PIM2)
      To realize this requirement, I thought of using a hook in the admin area. So, I read a lot in our forums and tested this by adding a hook into the ready.php file.
      $this->addHookAfter('InputfieldFile::fileAdded',function(HookEvent$event){ wire('log')->save('test','Image upload works'); ... The log entry is being created correctly. But when I try to use the pim/watermark-function like in a template, he cannot find the watermark-image anymore. Furthermore, when I try to get the page-id, it does not seem to be accessible, because the application does not seem to know how to reference it, or I dont know the right way to do so…
      So my questions are:
       
      Is this the right attempt at all or will there be another, better workaround? It seems, I cannot access the page object (of the content page) within this scope or file but I would need it to save the processed image inside the right files/id folder Would it be better to place the hook into the admin-template? (or admin.php)
        Thanks for any hints in advance. 🙂
    • By Macaco
      It's a bilingual site. There are two pages: "Artists" and "Events" each with a "Page Reference" field connecting each other.
      - Artists has a field where one can choose events available.
      - Events has a field where you can either choose artists available or create new ones.
      The problems: 
      - When I create an "Artist" page and select events from the list, it doesn't update the collection of participating artists on the "Event" page.
      - When I create an artist from the "Event" page. The field 'artist page > settings > language' is not "Active" for the second language.  When the artist page is created manually,"Active" is on by default.
      I know this all have to do with hooks, but I'm don't fully understand the logics.
    • By VeiJari
      Hello forum!
      I've yet again stumbled on a head-scratching situation. We have enabled the option on our articles template and events template that it skips the title adding part and goes straight to the form. This is what our customer wants. So when you add a new article or event it automatically names it temporary to "article-0000000" and same with event. Now the problem is that obviously after saving the form we want to change to page url or "name" to the title, like it's normally. 
      Now here's the code for the hook:
      wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
        $page = $hook->arguments(0);
        $newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
        wire()->log->message($page->name);
        $page->setAndSave('name', $newUrl);
      });
      I get the correct page and the name and path changes when I log them, but when I try to save it. It just loads and then I get: 
      Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) This happens in sanitizer.php
      and then another error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0
       
      What is happening? Am I not suppose to use sanitizer in this way? When we made a temporary page object in out other hook, the sanitizer worked perfectly.
      Thanks for the help!
    • By VeiJari
      Hello forum! I'm trying to learn how to time up my functions by using lazycron but I can't get it fired up. 
      Here's my code in site/init.php:
      function setEventToRepeat(HookEvent $e) { $wire->log->message("kutsutaan cron"); $events = $wire->pages->find('template=tapahtuma'); die(var_dump($events)); $wire->log->message('Kaikki tapahtumat haettu:' .$events); }   $wire->addHook('LazyCron::every30Seconds', null, 'setEventToRepeat'); I get nothing in the logs and can't echo or dump anything
      Thanks for the help!
    • By VeiJari
      Hello forum! 
      I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates)  for a certain template. 
       
      Here's the code: 

      Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. 
      But the code isn't doing anything. Not even dumping anything
      What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? 😄
      Thanks for the support in advance!
×
×
  • Create New...