Jump to content

Debugging hooks


Recommended Posts



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) {

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By FireWire
      Hey all,
      I've been building a new feature on our PW site and the URL hooks are exactly what the doctor ordered. I am developing a RESTful web API to allow external platforms to integrate with our site. The API endpoints are namespaced at /api/v1. I am having trouble making use of the named route parameter using regex.
      I have a hook for /api/v1/offline-events that gets all Offline Event pages. The next route to be defined is /api/v1/offline-events/{event ID}. The event ID is using a custom string (not the Page ID) that is a 15 character alphanumeric string like this: E4LxQRXZadgjstw. However when I use a PCRE compliant regular expression (tested on regexr.com) I get an error.
      Here is my hook code for reference and the error produced:
      <?php namespace ProcessWire; $wire->addHook('/api/v1/offline-events/{eventId:#(?=.{15}$)([A-Za-z0-9])\w+#}/?', function($e) { // ommitted for brevity }); // Error thrown: Warning: preg_match(): Unknown modifier '(' in /wire/core/WireHooks.php on line 1170 I've tried using all delimiters in the documentation (!@#%) but no change in the outcome.
      The second question is how do I create a wildcard capture for all 404s that occur after the /api endpoint? Currently it is loading the website 404 page.
      Many thanks!
    • By nuel
      I'm working with PW for some time now, but – please don't laugh – never used hooks.
      I have 200 – 300 subpages with the template 'artist', the title of each page represents the full name, sometimes John Doe, sometimes John Emmet Brown Doe. I want to auto-populate the field 'artist_last_name' with the word that is most likely the last name, the last word of the page title. If there are more names, it can be changed manually. I need this for easier and quicker alphabetical sorting.
      What I put together is this, in ready.php. Doesn't work. Again, I'm new to hooks..
      <?php     $wire->addHookBefore('Pages::saved', function(HookEvent $event) {     $p = $event->arguments('page');     if($p->template != 'artist') return;     if($p->artist_last_name) return;     $ln = end(explode(' ', $p->title));     $p->set('artist_last_name',$ln);     }); ?> In the best case this would be done once globally without having to open and save every page. But new pages should populate the field on page save.
      Thanks for your help, I think it's easy, I just need a push..
    • By rjgamer
      is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized?
    • By Kiwi Chris
      I need to restrict editing access based on both the user role, and the value of a sub-field in a page reference field.
      eg $page->competition->closingDate 
      There's some discussion about restricting editing access based on role, and that works, but when I try to access the page via the method @Robin S suggests, the page returned is an admin page rather than the page being edited, so I don't have access to the edited page's fields.
    • By picarica
      so i am trying to put CustomHooksForVariations.module, a custom module, i am placing it into site/modules direcotry yet my modules page in admin panel gives me errors
      so this is the screen show when i refresh modules, i dont know why the shole hook is written on top of the page :||

      and this next image is when i try to install it, i saw that it is not defiuned modules.php but it shouldnt need to be ?, any ways i dont want to edit site's core just to make one moulde work there has to be a way

  • Create New...