Okay, I understand. You want to use the Dolibarr Module Builder located at https://github.com/Dolibarr/dolibarr/tree/develop/htdocs/modulebuilder as a template for creating your own Dolibarr module. That's a great idea because the Module Builder is designed to help streamline the creation of basic module structures.
Here's how you can leverage the Module Builder as a template:
Understanding the Module Builder Structure
The modulebuilder directory in the Dolibarr repository provides a simplified interface for generating a basic module structure. It primarily consists of:
index.php: This is the main entry point of the Module Builder. It presents a form to specify module parameters.
generate.php: This script takes the module parameters from the form and generates the necessary files and directory structure for a module.
tpl.php: This is the template file used for module class creation.
core/modules/moduletpl.class.php: A template file for the module's main class.
lang/en_US/modulebuilder.lang: Language files for translations.
- Some other files css and javascript to help with the interface
Using the Module Builder as a Template
You have two main options to use the Module Builder as a template:
Option 1: Using the Module Builder Within Dolibarr (Recommended for Beginners)
-
Install the Module Builder Module:
- The Module Builder is a standard Dolibarr module, so you'll need to install and activate it.
- Go to Home > Setup > Modules and search for "Module Builder". Install and enable it if necessary.
-
Access the Module Builder:
- Once installed, you can access the Module Builder from the menu (usually under "Home").
- Alternatively you can browse
your_dolibarr_url/modulebuilder/index.php.
-
Fill the Form:
- The Module Builder form will ask for information about your module, such as:
- Module name
- Module description
- Module prefix (used in filenames)
- Module icon
- Module author
- Database table to manage (optional)
- And others
-
Generate Module:
- After filling the form, click on the "Generate" button.
- The Module Builder will generate:
- The necessary module directory structure in
htdocs/modules/.
config.php for your module.
core/modules/mod<MyModuleName>.class.php.
- Optional files for database tables.
- An initial language file.
- It can also generate a basic page for your module if you choose so.
-
Customize the Generated Module:
- Now you have a basic module structure that you can customize.
- Navigate to
htdocs/modules/mod<MyModuleName> and:
- Edit
config.php to add menus and change settings.
- Edit
core/modules/mod<MyModuleName>.class.php to add functionality and lifecycle events
- Add more PHP files for your logic.
- Add more language files.
- Add CSS and Javascript if needed.
Option 2: Manually Copying and Modifying Module Builder Files
This is less recommended but is also feasible if you want more control from the very beginning.
-
Copy Module Builder Files:
- From the Dolibarr GitHub repository (
https://github.com/Dolibarr/dolibarr/tree/develop/htdocs/modulebuilder), copy all the files in that directory.
- Create a temporary folder in your development environment (not inside your Dolibarr installation yet), and paste those files.
-
Modify index.php and generate.php (Carefully):
index.php: You can customize the form in the index.php file if you wish to add more parameters. Be careful, because you also need to modify generate.php to handle the new parameters.
generate.php: Carefully adjust the file paths, the generation logic and how parameters are used. It is also in this file where the code for the generation of config.php, module class, page for the module are made.
-
Create the Module:
- After the edits, you should copy the files for module generation to your server in a directory inside
htdocs/modules.
-
Customize the Generated Module
- Follow step 5 as described above
Benefits of using the Module Builder:
- Speed: It dramatically speeds up the initial module setup.
- Consistency: It ensures a consistent structure, making modules easier to understand and maintain.
- Basic Functionality: It sets up basic module functionality, including installation, activation, deactivation, and removal.
- Good Starting Point: It allows you to focus on the core logic of your module rather than the boilerplate code.
Example: Generating a Simple Module with Module Builder
Let's assume you want to create a module called "My Inventory" using the Module Builder.
-
Access the Module Builder within your Dolibarr instance.
-
Fill the form:
- Module name: My Inventory
- Module description: A simple module to manage inventory.
- Module prefix: myinventory
- Module author: Your Name
- Other Fields: Fill as needed.
- Database table:
myinventory (You can have the Module Builder create an SQL file to generate a basic database table.)
- Check if you want to generate a basic menu for the module
-
Click "Generate".
-
The Module Builder will generate a directory htdocs/modules/modmyinventory with files.
-
You can customize this module like adding new pages, functionality, permissions and a new menu to the config.php and adding to the class.php a new function.
Key Considerations:
- Complexity: The Module Builder is designed for creating basic modules. For more complex functionality, you'll need to extend the generated code significantly.
- Modularity: Keep your module modular, breaking down complex tasks into smaller, manageable functions and files.
- Dolibarr API: Continue using the Dolibarr API as you develop your custom modules.
- Documentation: Ensure you document your module in a way that is clear to other developers.
- Version Control: Track your changes to your custom modules with Git.
- Test Thoroughly: Remember to always test every aspect of your module before deploying it into production.
In Summary
The Dolibarr Module Builder is an excellent tool for bootstrapping Dolibarr modules. Whether you use the integrated Module Builder or customize it manually, it will help you create a working module base that you can build upon. Use the generated module as a starting point, understand the code, and customize it based on your specific needs. Remember to consult the official Dolibarr documentation for more information.
Let me know if you have more specific questions about using the Module Builder or if you run into any challenges along the way!