@@ -347,6 +347,188 @@ ahoy composer require --dev drupal/devel
347347ahoy drush pm:install admin_toolbar pathauto
348348```
349349
350+ ### Patching Contributed Modules
351+
352+ When contributed modules need fixes or customizations, use the proper patching workflow with ` cweagans/composer-patches ` .
353+
354+ #### Prerequisites for Patching
355+
356+ - Project uses ` cweagans/composer-patches ` package
357+ - Git is available for version control
358+ - Access to Drupal.org git repositories
359+
360+ #### Patch Storage and Configuration
361+
362+ Patches are defined in ` composer.json ` under the ` extra.patches ` section:
363+
364+ ``` json
365+ {
366+ "extra" : {
367+ "patches" : {
368+ "drupal/module_name" : {
369+ "Description of fix" : " patches/module-name-description.patch" ,
370+ "Another fix" : " https://www.drupal.org/files/issues/external-patch.patch"
371+ }
372+ }
373+ }
374+ }
375+ ```
376+
377+ - ** Local patches** : Store in ` patches/ ` directory in project root
378+ - ** External patches** : Reference URLs directly in composer.json
379+ - ** Naming convention** : Use descriptive names like ` module-name-description.patch `
380+
381+ #### Creating Module Patches
382+
383+ Step 1: Identify Module Version
384+
385+ Always work with the exact version and state used in your project:
386+
387+ ``` bash
388+ # Check installed version
389+ ahoy composer show drupal/module_name
390+
391+ # Verify version in composer.lock
392+ grep -A 20 " drupal/module_name" composer.lock
393+ ```
394+
395+ Step 2: Clone Module from Git
396+
397+ ** CRITICAL** : Always use the official Drupal git repository, not tarball downloads:
398+
399+ ``` bash
400+ # Create working directory
401+ cd /tmp && mkdir module_patch_work && cd module_patch_work
402+
403+ # Clone the module
404+ git clone https://git.drupalcode.org/project/module_name.git
405+
406+ # Navigate to module directory
407+ cd module_name
408+
409+ # Checkout the exact version/tag used in project
410+ git checkout 1.2.3 # Replace with actual version
411+
412+ # Create working branch
413+ git checkout -b fix-description
414+ ```
415+
416+ Step 3: Apply Existing Patches
417+
418+ If your project already has patches for this module, apply them first to match the current state:
419+
420+ ``` bash
421+ # Apply each existing patch in order
422+ curl -s https://example.com/patch1.patch | patch -p1
423+ curl -s https://example.com/patch2.patch | patch -p1
424+
425+ # Commit the patches to establish baseline
426+ git add .
427+ git commit -m " Apply existing patches to match project state"
428+ ```
429+
430+ Step 4: Make Required Changes
431+
432+ Edit the necessary files to implement your fix:
433+
434+ ``` bash
435+ # Make your changes using your preferred editor
436+ vim path/to/file.php
437+
438+ # Or use automated tools if applicable
439+ sed -i ' s/old_code/new_code/' path/to/file.php
440+ ```
441+
442+ Step 5: Generate Clean Patch
443+
444+ Create a proper git-based patch:
445+
446+ ``` bash
447+ # Stage your changes
448+ git add .
449+
450+ # Generate patch from staged changes
451+ git diff --cached > /path/to/project/patches/module-name-fix-description.patch
452+ ```
453+
454+ Step 6: Test Patch Application
455+
456+ ** ALWAYS test that your patch applies cleanly** :
457+
458+ ``` bash
459+ # Reset to test patch application
460+ git reset --hard HEAD
461+
462+ # Test patch applies without conflicts
463+ git apply /path/to/project/patches/module-name-fix-description.patch
464+
465+ # Verify changes were applied correctly
466+ git status
467+ git diff
468+ ```
469+
470+ Step 7: Integrate into Project
471+
472+ Add the patch to your project's composer configuration and test:
473+
474+ ``` bash
475+ # Update the specific module to apply patch
476+ ahoy composer require drupal/module_name
477+
478+ # Verify no patch application errors
479+ # Check that functionality works as expected
480+ ```
481+
482+ Step 8: Clean Up
483+
484+ Remove temporary working directory:
485+
486+ ``` bash
487+ rm -rf /tmp/module_patch_work
488+ ```
489+
490+ #### Patching Best Practices
491+
492+ ** ✅ Do This:**
493+
494+ - Use descriptive patch names that explain what they fix
495+ - Keep patches focused - one fix per patch when possible
496+ - Follow Drupal coding standards in your changes
497+ - Test locally before committing patches
498+ - Document the issue being fixed in composer.json description
499+ - Include issue URLs when available from drupal.org
500+
501+ ** ❌ Avoid These Mistakes:**
502+
503+ - Working with tarball downloads instead of git repositories
504+ - Creating patches from modified project files
505+ - Skipping patch application testing
506+ - Creating patches without applying existing patches first
507+ - Assuming patches work across different module versions
508+
509+ #### Troubleshooting Patch Issues
510+
511+ ** Patch Won't Apply:**
512+
513+ 1 . Check module version matches between patch creation and application
514+ 2 . Verify existing patches are applied in correct order
515+ 3 . Check for whitespace issues in patch file
516+ 4 . Ensure patch paths are correct (usually relative to module root)
517+
518+ ** Patch Conflicts:**
519+
520+ 1 . Identify conflicting patches by applying them individually
521+ 2 . Update patch order in composer.json if needed
522+ 3 . Recreate patches against the current patched state
523+ 4 . Merge patches if they modify the same areas
524+
525+ ** Performance Issues:**
526+
527+ 1 . Minimize external patch URLs to reduce download time
528+ 2 . Store frequently used patches locally in patches directory
529+ 3 . Keep patch files small and focused
530+ 4 . Remove obsolete patches when updating modules
531+
350532### Adding JavaScript/CSS Libraries
351533
352534For npm packages that need to be Drupal libraries, define them as inline
0 commit comments