Tập tành Create a Patch in Drupal 9
8th Jun 2022In this blog, we are going to uncover two main questions:
- What is a patch?
- How to create a patch in Drupal?
So, let’s get started.
What is a patch?
Patch is a file that consists of a list of differences and is usually created with the help of the git diff command. In the Drupal community, developers make use of this patch file as a contribution to fix an issue or enhance a feature in a module, theme, or even for Drupal core.
How to create a patch in Drupal?
Here we will show you the ways of creating a Patch file:
There are 2 different ways of creating a patch file
- Path Independent
- Path Dependent
Path Independent Patch
Follow these steps to create a Path Independent Patch:
- Create and set up a repository on GitHub specifically for creating patch files. It is best to maintain a single git repository for the patch files that you will be applying to your drupal project.
- Move the module/theme or core files that you need to generate patches to the newly created git repository.
- Make the necessary changes to a file on your local
![create a patch](/sites/default/files/inline-images/create%20a%20patch.png)
- Verify that the “git status” command shows the file that you have modified.
![create a patch](/sites/default/files/inline-images/create%20a%20patch-2.png)
- To generate the patch, use the following command: git diff --no-prefix [file-name] > ./file-name.patch
- The above command will generate a path-independent patch with the mentioned patch filename.
Example: git diff --no-prefix path_independent_module.info.yml > ./updated-module-description.patch
will generate the below file:
![create a patch](/sites/default/files/inline-images/create%20a%20patch-3.png)
- You can then use the independent patch file and apply it to the appropriate module(s).
Path Dependent Patch
Follow these steps to create a Path Independent Patch:
- Create and set up a repository on GitHub specifically for creating patch files. It is best to maintain a single git repository for the patch files that you will be applying to your drupal project.
- Move the module/theme or core files that you need to generate patches to the newly created git repository.
- Make the necessary changes to a file on your local.
![create a patch](/sites/default/files/inline-images/create%20a%20patch-4.png)
- Verify that the “git status” command shows the file that you have modified.
![create a patch](/sites/default/files/inline-images/create%20a%20patch-5.png)
- To generate the patch use the following command: git diff [file-name] > ./file-name.patch
- The above command will generate a path-independent patch with the mentioned patch filename.
Example:
git diff path_dependent_module.info.yml > ./updated-dependent-module-description.patch
will generate the below file:
![create a patch](/sites/default/files/inline-images/create%20a%20patch-6.png)
- You can then use the dependent patch file and apply it to the appropriate module(s) and make sure that the file path mentioned in the git differences is the same as in your project.
Add new comment