My First Craft CMS Module

Generating a Module Scaffolding with PluginFactory.io

We generate a module scaffolding using PluginFactory.io and then use it for our simple module approach using the init() method.

We hand-cod­ed the mod­ule our­selves for our first attempt because we didn’t need a lot of func­tion­al­i­ty. For this sim­ple mod­ule what we have is just enough. How­ev­er, if you plan to build out your mod­ule to have more func­tion­al­i­ty, you should start with a more robust struc­ture in the beginning. 

The eas­i­est way to do that is with the Plug​in​fac​to​ry​.io tool from Andrew Welch. This gen­er­ates the scaf­fold­ing for a Craft plu­g­in or mod­ule and lets you include the com­po­nents you need. You can even have it include help­ful com­ments to guide you along.

In this video we gen­er­ate scaf­fold­ing but then we sim­ply use it to house our sim­ple approach to a Craft mod­ule using the init() method.

  1. Choose Craft CMS Mod­ule ver­sion 3.x
  2. Mod­ule name: Down­link Con­trol Panel 
  3. Descrip­tion: A mod­ule that allows us to cus­tomize the con­trol pan­el of our project.”
  4. Ver­sion: 0.0.1
  5. Author: Your name or company
  6. Author URL: your URL 
  7. Dis­able Tweet it Out
  8. Leave Code Com­ments enabled 
  9. Leave all com­po­nents disabled

Once you gen­er­ate the mod­ule and it is down­loaded, unzip it and then move the mod­ule direc­to­ry (e.g. downlinkmodule) to your modules direc­to­ry in your project.

You can see that the Plug­in­Fac­to­ry uses a nam­ing con­ven­tion that includes the word mod­ule” in the name. That’s not a big deal, and we’ll take that into account as we’re cod­ing our module. 

The mod­ule direc­to­ry struc­ture is also dif­fer­ent. The main mod­ule file. DownlinkModule.php is in the src directory:

├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── config
│   └── app.php
└── src
    ├── DownlinkModule.php
    ├── assetbundles
    │   └── downlinkmodule
    │       ├── DownlinkModuleAsset.php
    │       └── dist
    │           ├── css
    │           │   └── DownlinkModule.css
    │           ├── img
    │           │   └── DownlinkModule-icon.svg
    │           └── js
    │               └── DownlinkModule.js
    ├── translations
    │   └── en
    │       └── downlink-module.php

That’ll be just fine and we’ll take into account when we code our sim­ple mod­ule inside of the this scaffolding.

My First Craft CMS Module is made up of the following videos: