You've built sites with Drupal and know that with a few dozen modules (and a ton of configuring), you can do nearly everything in modern Drupal.
But what do you do when there's not a module for that? When the modules that exist don't meet your needs, and cannot be made to by contributing changes?
You make your own.
This blog post accompanies the New England Drupal Camp session and helps you take that step. All you need to do is write two text files. The first file tells Drupal about the module; it is not code so much as information about your code. The second file can have as little as three lines of code in it. Making a module is something that anyone can do. Every person developing a module is still learning.
Slides source code: https://gitlab.com/agaric/presentations/when-not-a-module-for-that
(Site with slides coming.)
This is about the simplest module you can make, and—unlike the popular implications of its name—is not growing by adding bits and pieces of other modules as we build a monstrosity (that is what we will do later, as exhuming and incorporating adaptations of other modules' code is our main technique for making new modules).
Frankenstein (example) | Drupal.org
Sharing your module out into the world
Put your module on Drupal.org: drupal.org/node/add/project-module
(I honestly do not know where they hide that link, but remember on any Drupal site you can go to /node/add
and see what content you have access to create, and that includes creating projects on Drupal.org!)
It will start its life as a "sandbox" module by default.
Tip: Do not try to choose a part of core under "Ecosystem", and always check that the node ID it references is really one you want— there are a lot of projects with similar or even the same name.
Do not get overwhelmed; you can come back and edit this page later.
Press Save.
Then press the Version control tab to go to a page with Git instructions tailored to your module.
If you have already started your module and made commits in a repository dedicated to only your module, make certain your commits are on a branch named 1.0.x
(the instruction included there git checkout -b 1.0.x
will be enough to do that) and then follow only the git remote add
and git push
steps.
Double-checked to make certain the "Frankenstein" example module would not be better an example of when already "There's a module for that" and discovered the sad backstory— it exists in Drupal 7, people wanted it ported to modern Drupal, someone did port it to modern Drupal, but in a private GitHub repository and module maintainers did not take action and now that repo is gone.
Contributing to an existing module
(To do: Make into its own post.)
I apologize, on behalf of all of Drupal, that this is harder than it ought to be.
First, create an issue (if there is not one already) stating the needed feature or bug fix.
Before you start modifying the module's code, go into your project's composer.json
and edit the version of the module you are using and want to contribute to and make it the dev version.
If you have gotten the module as ^3.0@RC
for example, change that to ^3.0.x-dev@dev
.
Run composer update
.
Now, so long as you do not have a restriction in your composer.json
, composer will get the git repository of that module for you and put it in the usual place for contributed modules, such as web/modules/contrib
.
From the command line (terminal), change directory to the module you are modifying and have filed an issue against (for instance, cd web/modules/contrib/example
).
Back on your issue, press the light green "Create issue fork" button and follow the instructions it adds to the issue to issue under the "Show commands" link, to the right of the issue fork.
The lines you want to copy paste are under the first "Add & fetch this issue fork’s repository" and the "Check out this branch" copy code sections in that expanded "Show commands" section.
Make your code changes and git add
them. You can copy the entire line to git commit from the bottom of the issue. And finally, git push
.
Now you need to make a merge request.
In the issue, press the link to your issue fork. If you do not see a blue button "Create merge request" at the top of this page, you need to log in. Pressing "Sign in" is likely to log you in automatically because you are already logged in to Drupal.org, but you have to press it.
Press the blue "Create merge request" button at the bottom of the form pressing the first blue "Create merge request" button took you to.
To keep using the improvements you contributed to the module while waiting for them to be reviewed and, with lots of luck, eventually merged in, make a couple more modifications to your project composer.json
file. The information you need is the same as you put into the module's git configuration with the issue fork repository and branch checkout commands you copy and pasted above. We can get that information again most easily by opening up the .git/config
file in the module (or theme) you have been contributing to, and use the URL under "origin" in your "repositories" section of your composer.json
and the branch name at the bottom of the .git/config
in the "require" section, like this:
"repositories": {
"drupal/example": {
"type": "git",
"url": "https://git.drupalcode.org/issue/example-123456.git"
}
},
"require": {
"drupal/example": "dev-123456-issue-fork-branch-name",
},
Comments
Add new comment