Jump to content

How do I install a module then immediately run a method on that module while maintaining hooks in the ready method?

Recommended Posts

I have a module that I created which I am installing and then running a method on via the command line (although this issue still occurs when running in the script in my browser as well).

My script looks like this (I've heavily simplified it here to demonstrate the issue precisely):

<?php namespace ProcessWire;
include(dirname(__FILE__).'/../index.php'); // bootstrap processwire


MyModule's ready() method contains various hooks, many of which do things on page save.
MyModule's setupDemo() method does a bunch of stuff like creating pages and fields.

The problem is, when doing those two things back-to-back as shown above, the ready() method doesn't get executed for some reason and therefore the hooks do not get applied!  As a result, the various things that rely on hooks in setupDemo() do not execute.

If I were to split the two method calls into two different scripts and ran them one after another, this issue does not occur.

What am I doing wrong?

I've tried wire('modules')->refresh(); between the two calls and several other things but no luck.  Note: if I call the ready() method explicitly, it causes problems as well.

  • Like 1
Link to comment
Share on other sites

I asked ChatGPT and it gave me what I needed to know:


When a module is installed, it gets added to the list of active modules and its ___install() method is called (if it exists). However, the ready() method isn't immediately triggered upon installation. The usual lifecycle of the module involves it being loaded and initialized during the bootstrapping process, and then the ready() method is called when ProcessWire has fully initialized.

By trying to execute setupDemo() right after the installation, you're not giving the module a chance to reach the "ready" state, and therefore the hooks aren't being attached.

Therefore, I've moved the relevant hooks into a method outside of ready() and am attaching the hooks explicitly now in my script.

  • Like 1
Link to comment
Share on other sites

Just now, Jan Romero said:

This helped me give in to my nagging coworkers and sign up for ChatGPT, but I can’t seem to get it to answer this question properly? May I ask what your prompt was, @Jonathan Lahijani? Also, what’s causing problems with calling ready() explicitly?

ChatGPT has blown my mind countless times from a programming point of view.  It's ridiculously helpful and has given me superpowers (ie, less reliance on poor Google results, which in turn helps with learning faster).  It's also very helpful in providing a starting point on topics I have low experience in (for example, doing some advanced things with ImageMagick).

My prompt was literally copy/paste of my original post.  Pretend ChatGPT is a human that knows basically everything about everything.

I have a bunch of other code in ready() which was causing errors given the context.  But I should investigate that further (I was losing patience debugging for 2 hours).  Will try what Ryan mentioned, but for now I refactored my hooks into their own method.

  • 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.
  • Create New...