Jump to content
Gazley

Learning Module Development

Recommended Posts

Hi there,

Whilst creating a very simple Module, I found myself looking at other Modules and wondering why certain things were done in certain ways. I also, saw comments about methods with triple underlines and being to hook and pre-hook Page methods and after my head stopped spinning, I wondered whether there is a tutorial anywhere about this whole thing :)

If its just a case of doing and asking, that's fair enough. However, there are a fair few tutorials on most areas of the system but I couldn't find anything around Modules. Please let me know if there's something I'm missing.

Many thanks!

  • Like 1

Share this post


Link to post
Share on other sites

This would be a good starting point: http://processwire.com/api/modules/

Anything with 3 underscores is hookable, so in the init function on that page you can hook into anything with 3 underscores.

Share this post


Link to post
Share on other sites

Hi Pete,

Thanks for the link. Just so you know, I have read that quite a few times but it doesn't mention things like the underscores "rule" that you just referenced. It sort of says how you physically put the Module class together but I couldn't see anything about how you actually extend and take advantage of the PW core. Maybe, it's simpler than I think. Right now, I'm trying to get so many things done, I haven't really had the chance to sit down and just follow other Modules to see what/how they do things.

Are there any other things like the triple underscore? Which are the main classes that you would usually target or extend in a Module?

Cheers!

Share this post


Link to post
Share on other sites

No probs - just wasn't sure you'd seen that page yet and it helps to include the link to explain it for others who might have similar questions.

The best way probably is to see if there's a module that does something similar to what you want and take a look at the code.

The triple-underscore thing is literally just for hooking - I don't think there is anything else like that that springs to mind - most of the things you might do in your module are going to be a mix of regular PHP and the API.

If you explain what you're trying to do we might be able to give more specific help, but your last question is a little broad as it depends on what your module is doing - since a module can do pretty much anything you can think of (within the confines of what it's actually possible to code in PHP) then there's no easy answer to that one :)

  • Like 1

Share this post


Link to post
Share on other sites

Hi Gazley,

I think you're doing it the right way by looking at other modules. I'm still new to ProcessWire and just built my first module, will continue to improve it.

After reading the docs, I started looking at modules in wire/modules and also downloaded some modules already developed by the pro's ;-)

What helps me a lot is to look at the extended classes and implemented interfaces. The code there is pretty well commented!

So for example, check out extended classes like "WireData" (Data.php in the core directory), "Wire" and the implemented interfaces. A lot of the magic happens there :)

I still have to learn a lot more about Pw, but once you understand how it works... it is really fun coding and building a module, because the architecture of Pw is amazing!

  • Like 5

Share this post


Link to post
Share on other sites

Thanks guys! I think I have my answer(s). Seems I need to throw a bit of time studying what's already there. At least now I know I'm not missing something :)

Share this post


Link to post
Share on other sites

@Gazley, to answer your original question: I don't think that there are too many proper tutorials on module development available. At least I haven't seen any, so please, someone correct me if I'm wrong :)

There has been some talk about proper module development documentation and/or tutorial though, see this topic for an instance. In my humble opinion that's something the community definitely needs at some point, preferably rather soon, because (IMHO) asking the same questions over and over or trying to dig all the tips from dozens, hundreds or even thousands of posts is not very efficient in the long run.

Of course many of us like to learn from examples, but I'd still prefer to have common guidelines, frequent questions / problems, basic components and things like that explained in one place -- this would make it much easier for new folks to get into PW module development.

Anyway, once you've managed to grasp basics of module development, why don't you share your tips for others too? wiki.processwire.com would be a great place for such content. :)

  • Like 3

Share this post


Link to post
Share on other sites

Few things that are "nice to know" when building PW modules:

  • Autoload and non-autoload modules: autoload ones are loaded on every page load. So if you need to hook into core, then you probably need autoload. Non-autoload modules are run only by request: $modules->get("myUberModule")->doSomething()
  • When you need hooks? Well.. common things are "do something when page is saved", "alter the output that is generated", "do something before page is accessed" etc.. Actually there is quite a many hooks and best way to find right hook for you need is to ask for it here.
  • Building modules is pretty similar to working with templates. Nothing "special" there. Few notes though:
  • You cannot directly access the PW API variables like $pages, $session, $input etc.. Those are available at $this->pages or wire('pages'). If you have static method(s) in your modules then you need wire('pages') there - so if you want to play it safe always use that (it works on command line scripts also)
  • Modules can have settings - those are very easy to implement (just look examples from other modules that have settings)
  • Modules are cool.
  • init() is run before page is loaded, so if your autoload module needs to know the current page, that is available at ready() method.
  • Forums is your friend, just ask and someone (probably Soma with his quick editing skills) will surely help.
  • Like 14

Share this post


Link to post
Share on other sites

Anyway, once you've managed to grasp basics of module development, why don't you share your tips for others too? wiki.processwire.com would be a great place for such content. :)

I actually tried to put my bullet list to wiki... didn't realize that wikimedia is that awful to use :(

Share this post


Link to post
Share on other sites

@teppo - thanks for your comments and the link. I agree with you about having some guidelines; you can't beat experiencing things first hand, but a set of rules and handy information on a topic always sets you out along the right path from the get go.

@apeisa - thanks for the list of tips. These are the precisely the kind of gems that probably need to be collated in a single document or article. Much appreciated! :)

Share this post


Link to post
Share on other sites

Modules are cool - apeisa's right :)

In fact, modules don't just have to do one specific task or set of tasks - they can be a whole application (see his shopping cart application http://modules.processwire.com/modules/shopping-cart/ or the discussion forum application: http://modules.processwire.com/modules/discussions/ for examples).

I'm personally thinking of developing a few modules to sell as a service to prospective clients (management of their own clients/services is about as much detail as I can go into just now) as well as to see what I can do with PW since I've not done too much myself with the PW admin interface. These are pretty much going to be applications by the end of it, but all I mean by "application" is that they will simply just be a collection of installable modules that are grouped together and reliant on one another.

Hopefully I'll be a bit careful about how I build a couple of the modules as there is some crossover, so I could create some base modules to extend for each specific app :)

Share this post


Link to post
Share on other sites

@apeisa: yeah, well, MediaWiki takes some getting used to.. once you "get it" you'll realize that it's super powerful (but still rather confusing at times.)

Wikipedia is actually using a newer, cleaner and much easier text editor these days. It has most of the basic formatting you'll need (such as different headings, lists, refs, tables and blockquotes etc.) implemented in an easy-to-understand way. Might be worth taking a look at, perhaps we could have that one too.

Edit: the text editor that Wikipedia currently uses can be found from here: http://www.mediawiki.org/wiki/Extension:WikiEditor.

Edited by teppo
  • Like 2

Share this post


Link to post
Share on other sites

Wow, if I'd have known that was around back in 2010 teppo I'd have installed it on the other MediaWiki site I maintain - the mind boggles as to why they don't include things like that in the core distribution as for a wiki that would surely increase participation by a huge amount, or at the very least aid in terms of legibility ;)

I'll look at installing that one soon.

EDIT: hang on, it is in the core by default, must just not be switched on by default which is slightly odd.

  • Like 1

Share this post


Link to post
Share on other sites

Okay, it's installed now and makes editing the wiki MUCH easier.

  • Like 2

Share this post


Link to post
Share on other sites

Looks fantastic. Thanks Pete! :)

Share this post


Link to post
Share on other sites

Thanks for your efforts Ryan! I'm so amazed with this community, your good self and ProcessWire :)

  • Like 1

Share this post


Link to post
Share on other sites

I've put together a Process module skeleton that may be good as a starting point for any Process module. It is well commented and fully functional as a demonstration. It also includes instructions on what to do with it when using it as a starting point for another module:

http://modules.proce.../process-hello/

I'm planning to make more skeleton modules to cover all the bases.

  • Like 4

Share this post


Link to post
Share on other sites

Here's a start on creating modules documentation. I still need to write a whole section on hooks, but it's fairly complete in other areas. Please feel free to edit/add to this. http://wiki.processw...Module_Creation

What a timing, I just started trying to build a module. This community is remarkable !

  • Like 2

Share this post


Link to post
Share on other sites
This community is remarkable !

Yeah, it's alright I suppose ^_^

@ryan - thanks for this as it's perfect timing for a few things I'm intending to do too :)

  • Like 3

Share this post


Link to post
Share on other sites

Wow, I've been missing some great discussions during my vacations :o

Share this post


Link to post
Share on other sites

I hope this is not off topic, and is also not the incorrect place to ask this. I am still learning how to interact with the forums, especially for support/questions.

Anyway, can someone help me with the following.

I am looking to include a plugin via a module. One sticking point in my understanding of the module construction is the 'version' number.

I can see and understand the way this is offered up in module 'info'.

However, many of the plugins I come across have 2 digit revisions.

I.e. 1.3.12

How do we handle this with the 3 digit total for version numbers.

Should above be 1.3.1 giving 131 or 1.3.2 giving 132

This can lead to problems. Or am I completely wrong in my understanding.

  • Like 2

Share this post


Link to post
Share on other sites

@MikeB: no worries, this is just about as good as any other place to ask questions :)

Great timing here, too. Just today I wrote in another thread that integers (such as 32 for version 0.3.2 or 132 for version 1.3.2) feel wrong to me because they can only handle a subset of version numbers.

Integers are the default you'll see used a lot, but you can (and, when it's useful, should) use strings like '1.3.12' instead. It should work everywhere just as well as integers (and if it doesn't, that's most likely a bug).

  • Like 4

Share this post


Link to post
Share on other sites

Thanks for the reply, will look deeper into this. I was actually looking into updating an existing module, but it has turned out to go way beyond my capabilities at the moment. 

Glad I brought some attention to that point though.

I am hoping to get more experience with PW, and as I found with Drupal, every little setback/misunderstanding is very informative. 

  • Like 1

Share this post


Link to post
Share on other sites
On 14 augusti 2012 at 4:56 PM, ryan said:

Here's a start on creating modules documentation. I still need to write a whole section on hooks, but it's fairly complete in other areas. Please feel free to edit/add to this. http://wiki.processwire.com/index.php/Module_Creation

This URL seems not to be valid anymore. I get an error trying to access it. 

Thought that you should Know cause the info IT provides is helpfull. Maybe the plan is to move that info to the main pw website ?

  • Like 1

Share this post


Link to post
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...