Jump to content

Debugging hooks


celfred
 Share

Recommended Posts

Hello,

 

Here's what I'm trying to achieve : I have a textarea field that is frontend editable on any page I want to. If a user with a specific role updates it, I want to tick a checkbox field on the parent page where the textarea resides. So I tried to hook as follows in my ready.php file :

	  $this->addHookBefore('Fieldtype::savePageField', function(HookEvent $event) {
      $page = $event->argument[0];
      $field = $event->argument[1];
      if($this->user->hasRole('teacher')) {
	     $page->alertBox = 1;
	      }
	// DEBUG HERE (bd($page),  l($field)... ????
  });
	

Of course (!) it doesn't work (yet !) but the thing is I have no idea how to debug this since my bd() never triggers. I've tried the 'Event interceptor' of Tracy debugger which helped me setting up my hook. I guess my $page and $field are correct... but how could I go any further ?

The road is long to become a dev (when you're not one ? )...

For further information in case someone wonders : I'd like to set up a textarea that is modified by a user (having a 'player' role, ie 'student'). When front-end modified, I wish it would automatically alert me (the teacher) by ticking a box. So when I log in with my teacher role, I get a list of all textareas I have to read over. When I read/correct/update them (front-end), I would like my hook to automatically untick the box to remove it from my list. In other words, the 'textarea' status should go back and forth according to who modified it last. For the time being, I have managed to make it work with a checkbox that the user has to manually tick/untick, but I've noticed many kids forget to tick the box so they edit their text and I don't get a notice ?

Thanks if anyone takes time to try and help me on that one !

Link to comment
Share on other sites

Still digging...

I have tried this taken from the docs :

	$wire->addHookAfter('Pages::saved', function(HookEvent $event) {
  $page = $event->arguments(0);
  $event->message("You saved page $page->path");
});
	

But I see nothing... My logs > messages do not show anything, my Tracy bar doesn't show anything...

Does a front-end edition and a hit on the 'Save' button does not trigger the hook ?

I have a feeling I was confusing things in my previous post, so all in all, let's sum up my question : how do you debug hooks ? Is a simple bd('ok') suppose to work if the hook is working ?

 

Link to comment
Share on other sites

Thanks a lot @Zeka for your reply. I do use TracyDebugger in my usual debugging 'flow', but my problem here is that it never triggers anything, and that is exactly what I don't understand.

After trying some more things, I can understand thath Tracy works in hooks by testing this :

	  $this->addHookBefore('Page::render', function(HookEvent $event) {
    bd('ok');
  });
	

which does give me a result on page load.

Eventually, my concern is there : in my initial question, I want to debug in a hook that doesn't completely reload my page. I guess that's why I can't see anything. When I double-click my textarea to front-end edit it and then hit 'Save', the page doesn't reload so my dump bar stays silent. Is there a way to dynamically see what's going on ?

Link to comment
Share on other sites

Still desperately trying things...

I have this in my ready.php :

	$this->addHookBefore('Fieldtype::savePageField', function (HookEvent $event) {
	bd('ok'); // Also tried fl(), d()...
});

I have nothing anywhere (after front-editing my textarea and clicking 'save' in my CKEditor) aven after reloading the page, whereas if I use the event interceptor, I get the whole list of fields being saved (after reloading the page) and I can see that one of them is the field I have just edited.

So I'm stuck ?

Link to comment
Share on other sites

Thanks @bernhard for your reply. This helps me feeling more confident about my tests since I often don't know if I'm going in the right direction. I must admit I am not mastering anything there (you can tell !). I was glad to learn about the 'Event interceptor' but it seems like it lead me on a false interpretation ?

So hooks don't fire when front-end editing ? I don't really understand why since the field is actually saved, isn't it ?

So I guess I won't be able to avoid my 'Manually check this box to alert your teacher' way of doing ?

Link to comment
Share on other sites

2 minutes ago, celfred said:

So hooks don't fire when front-end editing ? I don't really understand why since the field is actually saved, isn't it ?

I don't know if that is the case and I don't have time to investigate. You did lots of things right (like placing the most basic hook you can think of, then check if it works and only then doing the next step). If you encounter any problems on that road you either have to look for a different solution (like changing from frontend to backend editing) or you have to investigate and find the reason why things don't work as expected.

In the current case it could either be a bug or that saving of the field just works a little differently when fired from the frontend. That's by the way one of the reasons why I'm not using frontend editing any more. It bring's too much troubles for too little benefits imho. Doing those investigations can be frustrating sometimes, but you'll learn a lot about ProcessWire which will help you on all following challenges ? 

  • Like 1
Link to comment
Share on other sites

5 minutes ago, bernhard said:

Doing those investigations can be frustrating sometimes, but you'll learn a lot about ProcessWire which will help you on all following challenges

That's what I keep telling myself but I often end up having a greater and more difficult challenge ?

On my side, front-end editing seemed a good solution because I may have up to 50 textareas to edit and if I have to click on each in back-end, save and go to another, having a rather slow connection, can be troublesome... Here, I can load all the 'alerted' textareas on 1 page and edit them 'right away'. It was then just (!) a matter of automatically detecting who was the last user to modify the text. But nevermind for now, as you said, I still have learned a lot trying to do that !

Thanks a lot for your encouraging message and your help !

Link to comment
Share on other sites

Well... I'm also not convinced 100% myself to ban frontend editing completely ? The interface is great and it's so much more user friendly to get exactly the same visual representation as you type. And it sounds reasonable to only load/save parts of a page and not everything. It always depends on the usecase, just wanted to note that one has to be careful with those fancy tools and also think of the drawbacks ? 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...