Module Builder

Generates scaffold code for custom Drupal modules including hooks, plugins, entities, services, forms, and tests.

module_builder
373 sites
132
drupal.org

Install

Drupal 11, 10, 9, 8 v4.1.3
composer require 'drupal/module_builder:^4.1'

Overview

Module Builder is a powerful development tool that simplifies the process of creating Drupal modules by generating scaffold code. Instead of manually creating boilerplate code and constantly referencing api.drupal.org for hook signatures and parameters, developers can use Module Builder's intuitive interface to select the components they need.

The module analyzes your site's codebase to discover available hooks, plugin types, services, and other Drupal components. Based on this analysis, it presents organized forms where you can select which hooks to implement, which plugin types to create, and configure various module components. The generated code includes proper PHPDoc comments, function signatures, and file structures that follow Drupal coding standards.

Module Builder stores module definitions as configuration entities, allowing you to iteratively develop and regenerate code as requirements change. It supports merging generated code with existing files and integrates with Git to show version control status of files being overwritten.

Features

  • Generates complete module scaffolding including .info.yml, .module, and other required files
  • Creates hook implementations with proper function signatures, parameters, and PHPDoc comments
  • Generates plugin classes for various plugin types (Block, Field, Action, Condition, etc.)
  • Creates content entity types and config entity types with full scaffolding
  • Generates services with dependency injection and proper service definitions
  • Creates admin settings forms with menu routing and permissions
  • Generates router items and dynamic routes
  • Creates PHPUnit test case classes
  • Generates README files and other documentation
  • Analyzes site code to discover available hooks, plugins, services, and tagged services
  • Provides filterable hook selection interface grouped by module/type
  • Links to api.drupal.org documentation for core hooks
  • Stores module definitions as configuration entities for iterative development
  • Supports merging generated code with existing module files
  • Integrates with Git to show version control status of files being modified
  • Supports adopting existing modules and their components into the builder
  • Provides autocomplete for service selection and other large option sets
  • Configurable module write location with smart defaults
  • Batch processing for code analysis to handle large codebases

Use Cases

Creating a new custom module from scratch

When starting a new module, use Module Builder to generate the initial scaffolding. Navigate to Module Builder, click 'Add module', enter your module name and description. Then use the tabs to select which hooks to implement, add plugin classes, define services, and configure other components. Finally, go to the Generate tab and write all files to create your module structure with proper boilerplate code.

Adding hooks to an existing module

If you have an existing module and need to add new hooks, first 'Adopt existing module' to create a Module Builder entity for it. Then go to the Hooks tab, search for and select the hooks you need. Generate the code and use 'Write selected files' to only update the .module file with the new hook implementations. The merge functionality will preserve your existing code.

Creating a custom Block plugin

Navigate to your module's Plugins tab. Add a new plugin item and select 'Block' as the plugin type. Configure the block ID, admin label, and any settings the block should have. Generate the code to create a properly structured Block plugin class in src/Plugin/Block/ with all the required annotations and methods.

Generating a content entity type

Use the Entity types tab to define a new content entity. Configure the entity ID, labels, base fields, handlers (storage, view builder, form, list builder), and admin UI settings. Module Builder generates the complete entity class, interface, form classes, route providers, and configuration schema.

Setting up an admin settings form

On the Routes & forms tab, enable 'Settings form'. This generates a ConfigFormBase class, adds the route to routing.yml, creates a menu link under Configuration, and sets up the associated permission. You just need to add your actual form fields and submission logic.

Iterative module development

Module Builder stores your module definition as a config entity. This allows iterative development where you can come back, add new components, regenerate code, and use the merge functionality to update files while preserving your custom implementations. Version control integration helps you track changes.

Tips

  • Always run code analysis after installing or updating modules to ensure Module Builder has current data about available hooks and components.
  • Use version control (Git) before writing files to easily review and revert changes. Module Builder shows Git status to help you identify risky overwrites.
  • The 'Write new files' button is safest for existing modules - it only creates files that don't exist yet.
  • For services with many options, use the autocomplete search. The '.' and '_' characters are interchangeable when searching.
  • Module definitions are stored as config entities, so you can export them with Configuration Management and share them between environments.
  • The 'Adopt existing module' feature is powerful for bringing legacy modules under Module Builder management.
  • Use the filter on the Hooks page to quickly find hooks - just start typing part of the hook name.
  • Check the 'Clear all caches before analysis' option when you've modified custom module code to ensure fresh discovery.

Technical Details

Admin Pages 14
Module Builder /admin/config/development/module_builder

Main listing page showing all module entities created with Module Builder. From here you can create new modules, edit existing ones, generate code, or delete module definitions.

Add module /admin/config/development/module_builder/add

Form to create a new module definition. Enter basic module information including machine name, readable name, and optional write location.

Edit module basic properties /admin/config/development/module_builder/manage/{module_builder_module}

Edit the basic properties of a module including its name, description, package, dependencies, and lifecycle status. This is the first tab when editing a module.

Edit module hooks /admin/config/development/module_builder/manage/{module_builder_module}/hooks

Select which hooks the module should implement. Hooks are organized by group (core, node, field, etc.) with filterable search. Each hook shows its description and links to api.drupal.org documentation.

Edit module plugins /admin/config/development/module_builder/manage/{module_builder_module}/plugins

Configure plugin classes to generate for your module. Add plugins of various types (Block, Field, Action, Condition, etc.) with configurable properties for each plugin instance.

Edit module entity types /admin/config/development/module_builder/manage/{module_builder_module}/entities

Configure content entity types and config entity types for your module. Define entity properties, base fields, handlers, and forms.

Edit module routes and forms /admin/config/development/module_builder/manage/{module_builder_module}/routes_forms

Configure routes, dynamic routes, settings forms, and custom forms for your module.

Edit module tests /admin/config/development/module_builder/manage/{module_builder_module}/tests

Configure PHPUnit test classes for your module including unit tests, kernel tests, and functional tests.

Edit module miscellaneous components /admin/config/development/module_builder/manage/{module_builder_module}/misc

Configure additional module components not covered in other tabs, such as services, permissions, libraries, and help text.

Generate code /admin/config/development/module_builder/manage/{module_builder_module}/generate

View all generated code files and write them to the filesystem. Shows file merge status and Git version control status for existing files.

Adopt components /admin/config/development/module_builder/manage/{module_builder_module}/adopt

Adopt existing components from a module's files into Module Builder. This allows you to enhance existing components by first importing them into the builder.

Adopt existing module /admin/config/development/module_builder/adopt-module

Create a new Module Builder entity from an existing module on your site. This analyzes the module and creates a definition that can be used to regenerate and extend the module.

Module Builder settings /admin/config/development/module_builder/settings

Configure Module Builder settings including the data storage directory and code generation options.

Analyse site code /admin/config/development/module_builder/analyse

Run code analysis to discover hooks, plugins, services, and other Drupal components available in your site's codebase. This data is required before generating any module code.

Permissions 1
Create modules

Generate module code with Module Builder. This is a restricted access permission as it allows writing files to the site's codebase.

Hooks 1
hook_module_builder_info

Provide information about hook definition files to Module Builder. Allows modules to specify custom hook documentation sources and destination files.

Troubleshooting 6
Error: 'No component data is present'

You need to run code analysis before using Module Builder. Go to Administration > Configuration > Development > Module Builder > Analyse code and click 'Perform code analysis' or 'Update code analysis'.

Error: 'The component data directory could not be created or is not writable'

Check that the public:// files directory is writable by the web server. Verify the data_directory setting in Module Builder settings points to a valid location.

Hooks or plugins are missing from the selection lists

The code analysis may be out of date. Go to the Analyse code page and run a fresh analysis. If you've added custom code, check 'Clear all caches before analysis' before running.

Generated code doesn't match current Drupal version patterns

Ensure you're using the latest version of both module_builder and drupal-code-builder. Run 'composer update drupal/module_builder' to get updates.

Form validation errors about unrecognized values

This often occurs when analysis data is outdated. The error message will suggest re-running code analysis. Follow the link to update your analysis data.

Warning about module already existing when generating

This is informational. Module Builder detects the existing module and will attempt to merge generated code with existing files. Use version control to review changes before committing.

Security Notes 4
  • Module Builder has the capability to write files to your site's codebase. It should NEVER be installed on production sites.
  • The 'create modules' permission is marked as 'restrict access' because it grants powerful file writing capabilities.
  • Generated code should be reviewed before deployment, especially when merging with existing files.
  • Do not use Module Builder on publicly accessible development sites without proper access controls.