Development
Phase2
February 19, 2020
One of Drupal’s defining features is its open source nature, and the fact that it invites contributions from its vast community. Having been in the Drupal world for a few years now, I found myself wanting to make my own contributions, but unclear on how best to do so.
The Drupal docs are sparse in some places, overly verbose in others, and they don’t take into account some of the specific tools we at Phase2 rely on, like Docker and Xdebug.
This guide is intended to help remove all of the barriers for getting patches up to Drupal.org.
If you’re following this guide with an issue already in mind, feel free to skip this section.
Otherwise, here are some steps to help you find an issue to solve:
Once you find an issue, consider following it by clicking the green star icon. That way you’ll receive notifications on comments.
Regardless of the approach you use to find an issue, keep in mind that it being either Core or Contrib will have a significant impact on your work. Core patches have a lot more visibility and the potential to help a lot more people, but since they’re core they’re going to have a lot more barriers to entry and will almost certainly require automated testing.
Contrib patches will likely have a smaller impact, but are usually easier to get in because they only need approval from the module’s developers. Once you’ve settled on an issue, you’re ready for the next step.
Before moving any further I would highly recommend you install Dreditor in your browser. This tool adds a number of tools to Drupal.org, the most important being the “review” button that it will add to patch links.
This button displays patch files in an easy-to-read format that makes full use of javascript and color-coding.

You should only have to do the set up steps once. If you already have your environment going feel free to skip this.
Like other projects, we should have a virtual environment to work in. At Phase2 we usually rely on Docker/Docksal for that task. Fortunately this repo makes it easy to set up a D8 environment using Docksal.
git clone https://github.com/docksal/drupal8-contrib.git
cd drupal8-contrib
fin init
fin init
Xdebug is a crucial tool for debugging PHP code, but it can be a challenge to set up. Use the following steps to get it running on your Drupal8 local site.

2. Preferences > Languages and Frameworks > PHP > Servers and make sure your settings match the following screenshot.

3. Go to your docksal.env file and change the value of XDEBUG_ENABLED to 1.

4. Restart your containers with fin restart to make sure your change to the environment takes effect.
5. If Xdebug still isn’t working, try the following steps:
Be aware that Xdebug, while helpful, can also slow down your system a lot, so you don’t always want to have it enabled. Here are two ways to address that issue:
fin config set XDEBUG_ENABLED=1 && fin restartphp/xdebug on and fin php/xdebug off commands to toggle xdebug. This shortcut runs faster and doesn’t require restarting containers, but the site will still have some slowdown compared to the previous method.Follow these steps to get Git set up.
cd docrootgit checkout -b 12345-short-description\cd modulesgit clone git@git.drupal.org:project/machine_namecd module_name`git checkout -b 12345-short-descriptioncurl -O https://www.drupal.org/files/[patch-name].patchgit apply [patch-name].patchgit diffNow you’re finally able to write the code that will address your issue, and there are a few things to keep in mind while doing so.
First, make sure whatever you add is reinforced by automated tests, either by updating existing tests or making your own. You can run tests using the following commands:
Test an entire file: fin phpunit path/to/file
Run an individual tfin init est: fin phpunit path/to/file --filter=testFunctionName
You also probably want to turn off Xdebug before running tests as it will make them take much longer.
Functional tests are particularly guilty of this problem and as such can be hard to create. However, if you pass the --printer="\Drupal\Tests\Listeners\HtmlOutputPrinter" tag into your test, your terminal will print out something like the following.

Each url represents a different step in running the functional test, such as logging in or creating a user. Assuming this test was using the aforementioned docksal setup, you could see the generated html by going to: fin http://drupal8-contrib.docksal/sites/simpletest/browser_output/Drupal_Tests_user_Functional_UserCancelTest-313-38450349.html
Writing good tests will likely save you time in the long run, not only by catching regressions, but also because tested code is far more likely to be merged in.
Feel free to follow whatever commit strategy makes sense to you, since the patch file you’ll be creating won’t have an associated git history. That being said, it’s recommended that if you’re working on top of multiple patches that you make a commit between each patch to make jumping between them easier.
Once you’re confident about your work, make sure to add comments where appropriate and run php codesniffer to ensure that your code is up to Drupal 8 standards:fin composer phpcs path/to/file
E.g. fin composer phpcs core/modules/field/tests/src/Kernel/FieldStorageCrudTest.php
Once your code is complete, it's time to generate a patch file. Depending on how long you’ve been working on the patch, you’ll probably want to pull down the most recent version of the Drupal master branch and rebase your new branch off of it to make sure that your patch file only contains your changes. The following command will generate your patch file.git diff master_branchname > [patch_name.patch]
E.g git diff 8.8.x> my_patch.patch
Finally, if you’re adding to an existing patch its best practice to generate an interdiff file to post with your patch. An interdiff is a text file in patch format that makes it easier for reviewers to understand the changes between patches. git diff HEAD~1 > ISSUE_ID-COMMENT_ID-interdiff.txt
It should be clear by now that this process requires a lot of detailed and error-prone steps, fortunately there are a number of shortcuts and tools that can make this process easier.
You can put a number of incredibly useful shortcuts into your local gitconfig file. For example, the following lets you download and apply a patch very easily:
a = apply --index
ac = "!f() { curl $@ | git a; }; f"
E.G. git ac https://www.drupal.org/files/issues/2019-04-23/core-field-storage-error-message-3050264-1.patch
This code lets you easily download contrib modules from Drupal.org:
[url "git@git.drupal.org:project/"]
insteadOf = dm:
E.G. (from the modules directory) git clone dm:token
With a little Bash knowledge you can put all kinds of useful shortcuts in the gitconfig file.
I also highly recommend installing the Drupal CLI tool which you can find here. There are a host of useful things in this CLI, and here are some of the highlights.
drupalorg drupalci:list [issue_number] drupalorg issue:apply [issue_number]drupalorg issue:patch [issue_number]drupalorg issue:branch [issue_number]Dorgflow also has some helpful command line tools, for instance after all your work is finished you can type dorgflow and it will create a patch and an interdiff file for you.
Make sure you fully understand the process that is being run before using these shortcuts. While they’re helpful, they can break, and you do not want to be fully dependent on them.
Once you’re done with your patch and interdiff, post it to the issue with a comment about what you changed and set its status to ‘needs review,’ this will run all tests and tell other developers that your work is ready for feedback. If your patch isn’t getting a lot of attention, feel free to reach out to people on the official Drupal Slack Channel and ask for reviews. Also, keep in mind that while writing your own patches is great, contribution also involves commenting on and reviewing other people’s work. In general, the more you review, the more likely you are to get your work reviewed.