Finding and Creating Patches
Search the issue queue for an existing patch before creating one; when none exists, create it from a clean clone.
Debugging Errors: Find Patches BEFORE Creating
CRITICAL WORKFLOW: When encountering Drupal errors, ALWAYS search for existing patches before creating your own.
Step 1: Extract the Exact Error Signature
From the error message, extract the exact error string:
# Example error:
TypeError: Unsupported operand types: array + null in Drupal\field_ui\Form\EntityViewDisplayEditForm
# Extract this part:
"Unsupported operand types: array + null"Step 2: Search Drupal.org Issue Queue FIRST
# Method 1: Direct URL search (BEST)
https://www.drupal.org/project/drupal/issues?text=Unsupported+operand+types+array+null
# Method 2: Search with file + line number
https://www.drupal.org/project/drupal/issues?text=EntityViewDisplayEditForm+line+166What to look for in search results:
- Issues with status: "Needs review" or "Reviewed & tested by the community" (RTBC)
- Recent activity (check dates)
- Patch files in comments (look for
.patchattachments) - Merge requests (look for
!13611references)
Step 3: Use WebFetch to Get Patch Details
# Once you find the issue, fetch details:
WebFetch(https://www.drupal.org/project/drupal/issues/3552531)Look for:
- Patch file URLs: Usually
https://www.drupal.org/files/issues/YYYY-MM-DD/filename.patch - Merge request numbers: E.g.,
!13611→https://git.drupalcode.org/project/drupal/-/merge_requests/13611 - Issue status: RTBC means ready to use
Step 4: Download and Apply Official Patch
# Download to patches directory
curl -O https://www.drupal.org/files/issues/2025-10-16/field-ui--unsupported-operand-types--3552531-2.patch
mv field-ui--unsupported-operand-types--3552531-2.patch patches/
# Add to composer.json with descriptive name referencing issue
{
"extra": {
"patches": {
"drupal/core": {
"Fix TypeError: Unsupported operand types array + null in EntityViewDisplayEditForm - Issue #3552531": "patches/field-ui--unsupported-operand-types--3552531-2.patch"
}
}
}
}
# Record it in patches.lock.json, then re-install core so the patch applies
composer patches-relock
composer reinstall drupal/coreCommon Search Patterns
| Error Type | Search Term |
|---|---|
| TypeError | Exact error message in quotes |
| Deprecated function | Function name (e.g., user_roles) |
| Missing method | Class name + method name |
| Fatal error | Exact error text |
Why This Matters
- Saves time: Don't recreate existing solutions
- Better quality: Community-reviewed patches are more robust
- Upstream integration: Using official patches means easier upgrades
- Documentation: Issue threads contain context and discussion
Anti-Pattern Example
❌ What NOT to do:
- See error
- Read code
- Create patch
- Apply patch
- (Someone points out existing issue)
✅ What TO do:
- See error
- Extract exact error message
- Search drupal.org issue queue
- Find existing patch
- Apply official patch
Creating Local Patches
IMPORTANT: Always create patches from a separate clone of the contrib module repo, not from the installed version in your project.
# Step 1: Clone the module repo to a separate directory OUTSIDE the project (one-time setup)
cd <workspace-dir> # e.g. the directory that contains <project-root>
git clone git@git.drupal.org:project/module_name.git module_name-contrib
# Step 2: Checkout the exact version you have installed
cd <workspace-dir>/module_name-contrib
git checkout 1.0.3 # Match your installed version
# Step 3: Make your changes in the contrib repo
# Edit files as needed...
# Step 4: Generate the patch using git diff
git diff > <project-root>/patches/module_name-custom-fix.patch
# Step 5: Add to composer.json
{
"extra": {
"patches": {
"drupal/module_name": {
"Custom fix description": "patches/module_name-custom-fix.patch"
}
}
}
}
# Step 6: Add the patch to patches.lock.json (composer-patches v2)
cd <project-root>
composer patches-relock
# v2 applies patches from patches.lock.json, NOT from composer.json, on
# install/reinstall — skip this and the patch is silently dropped on every
# clean install (CI, teammates, deploys).
# Step 7: Apply via composer
composer reinstall drupal/module_name
# Step 8: Commit composer.json, patches.lock.json and the .patch file
git add composer.json patches.lock.json patches/module_name-custom-fix.patchWhy use a separate repo?
- Creates clean patches without local modifications bleeding in
- Matches the exact file structure composer expects
- Allows proper version tracking with git tags
- Enables contributing patches upstream to drupal.org
Patch format: Patches should use git diff format (includes a/ and b/ prefixes):
diff --git a/src/File.php b/src/File.php
index abc123..def456 100644
--- a/src/File.php
+++ b/src/File.php