Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

The Crema module allows you to replace existing PHP classes within your Drupal modules. By defining replacements in your module's info file, you can provide your own implementations of Drupal classes without altering the core or contributed modules directly. This enables custom logic and behavior through a clever mechanism that intercepts class loading, though it comes with limitations such as debugger inaccessibility and file size constraints.

…And the idea also is pretty insane and silly anyway…

A PoC module which aim to "replace" PHP classes you define in your module's info.yml file.

Usage

  1. Add a new key class_replacements to the .info.yml of the module which contains the replacement modules:
    name: Migration Manager Replacement
    type: module
    class_replacements:
      # Key: the qualified name of the class which should be replaced.
      # Value: location of the replacement file, relative to the actual module's root.
      Drupal\migrate\Plugin\MigrationPluginManager: src/migrate/MigrationPluginManager.php
      Drupal\migrate_drupal\MigrationPluginManager: src/migrate_d/MigrationPluginManager.php
    
  2. Make sure that the module depends on crema; or have a good reason not doing so 🙃
  3. Create the replacement file at the declared location:
    • Its namespace must be the namespace of the original file with the leading Drupal replaced by Crema.
    • The class name must be the same as the replaced class name.
    • If you want to extend the original class (I'm pretty sure you want to), then you can do it by adding the proper (aliased) use declaration of the original class.
    # File content of <module_root>/src/migrate/MigrationPluginManager.php
    <?php
    
    namespace Crema\migrate\Plugin;
    
    use Drupal\migrate\Plugin\MigrationPluginManager as Original;
    
    /**
     * Replaces the default migration plugin manager.
     */
    class MigrationPluginManager extends Original {
    
      protected $defaults = [
        'class' => '\Drupal\my_module\Plugin\BetterMigration',
      ];
    
      public function foo(): string {
        return 'foo';
      }
    
    }
    

Limitations

  1. You cannot replace the same PHP class (or trait or interface) more than once.
  2. Due to the way how Crema works, you cannot use debugger breakpoint neither in the replacement nor in the replaced php file.
  3. The module only can replace files with Drupal\* namespace, and not all of them, but most of them. For example you cannot replace Drupal kernels, database driver classes etc.
  4. Crema uses PHP's temporary stream to store the replaced and the replacement files before it loads them. Because of this, (for now) the files must me smaller than 2MB: https://www.php.net/manual/en/wrappers.php.php#wrappers.php.memory

Activity

Total releases
1
First release
Dec 2025
Latest release
7 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Slowing

Releases

Version Type Release date
1.0.0-alpha4 Pre-release Dec 4, 2025