How to Create a Custom Module in Dolibarr (Simple Example)
   05/18/2025 00:00:00     Dolibarr , Wiki Dolibarr    0 Comments
How to Create a Custom Module in Dolibarr (Simple Example)

Dolibarr ERP & CRM is a versatile open-source platform that allows businesses to manage various aspects of their operations. One of its strengths lies in its modular architecture, enabling users to extend its functionality through custom modules. This guide will walk you through the process of creating a simple custom module in Dolibarr.

Understanding Dolibarr's Modular Architecture

Before diving into module creation, it's essential to understand how Dolibarr's modular system works. Each module in Dolibarr is a self-contained package that can add new features, modify existing ones, or integrate external services. Modules can include:

  • New menus and pages

  • Database tables

  • Business logic

  • Permissions

  • Triggers and hooks

By creating a custom module, you can tailor Dolibarr to meet specific business requirements without altering the core code, ensuring easier maintenance and upgrades.

Prerequisites

To create a custom module in Dolibarr, you'll need:

  • A working installation of Dolibarr (preferably the latest stable version)

  • Basic knowledge of PHP, HTML, and SQL

  • Access to the Dolibarr file system (via FTP, SSH, or local access)

  • A code editor (e.g., VS Code, Sublime Text)

Step-by-Step Guide to Creating a Custom Module

Step 1: Set Up the Module Directory

Navigate to the htdocs/custom directory in your Dolibarr installation. This is where custom modules are stored. Create a new directory for your module, for example:

bash
mkdir htdocs/custom/mymodule

Replace mymodule with your desired module name.

Step 2: Create the Module Descriptor File

The module descriptor defines the module's properties and is essential for Dolibarr to recognize and manage the module. Create a file named modMyModule.class.php inside the htdocs/custom/mymodule/core/modules/ directory:

bash
mkdir -p htdocs/custom/mymodule/core/modules/ touch htdocs/custom/mymodule/core/modules/modMyModule.class.php

Populate modMyModule.class.php with the following content:

php
<?php include_once DOL_DOCUMENT_ROOT . '/core/modules/DolibarrModules.class.php'; class modMyModule extends DolibarrModules { public function __construct($db) { global $langs, $conf; $this->db = $db; $this->numero = 104001; // Unique ID for the module $this->rights_class = 'mymodule'; $this->family = "other"; $this->name = preg_replace('/^mod/i', '', get_class($this)); $this->description = "My custom module description"; $this->version = '1.0'; $this->const_name = 'MAIN_MODULE_' . strtoupper($this->name); $this->picto = 'generic'; $this->module_parts = array(); $this->dirs = array(); $this->config_page_url = array("admin/mymodule_setup.php@mymodule"); $this->langfiles = array("mymodule@mymodule"); $this->dependencies = array(); $this->conflictwith = array(); $this->phpmin = array(5, 6, 0); $this->need_dolibarr_version = array(9, 0, -1); $this->tabs = array(); $this->dictionaries = array(); $this->boxes = array(); $this->rights = array(); $this->menu = array(); } }

Adjust the properties as needed, ensuring the numero is unique to avoid conflicts with other modules.

Step 3: Define the Module's Structure

Organize your module's directory with the following structure:

pgsql
mymodule/ ├── admin/ │ └── mymodule_setup.php ├── class/ │ └── myclass.class.php ├── core/ │ └── modules/ │ └── modMyModule.class.php ├── lang/ │ └── en_US/ │ └── mymodule.lang ├── page/ │ └── mymodule_page.php ├── sql/ │ └── mymodule.sql
  • admin/: Contains administrative pages for module configuration.

  • class/: Contains PHP classes related to your module's business logic.

  • core/modules/: Contains the module descriptor.

  • lang/: Contains language files for translations.

  • page/: Contains public-facing pages of your module.

  • sql/: Contains SQL scripts for creating necessary database tables.

Step 4: Create the Setup Page

In admin/mymodule_setup.php, create a basic setup page:

php
<?php require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php'; $langs->load("admin"); $langs->load("mymodule@mymodule"); $title = $langs->trans("MyModuleSetup"); llxHeader('', $title); print load_fiche_titre($title); print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">'; print '<table class="noborder" width="100%">'; print '<tr class="liste_titre">'; print '<td>'.$langs->trans("Parameter").'</td>'; print '<td>'.$langs->trans("Value").'</td>'; print '</tr>'; print '</table>'; print '<br><div class="center">'; print '<input type="submit" class="button" value="'.$langs->trans("Save").'">'; print '</div>'; print '</form>'; llxFooter();

This page will appear under the module's configuration section in Dolibarr.

Step 5: Create a Language File

In lang/en_US/mymodule.lang, add translations:

ini
MyModuleSetup=My Module Setup Parameter=Parameter Value=Value Save=Save

This allows for easy localization of your module's interface.

Step 6: Add a Public Page

In page/mymodule_page.php, create a simple page:

php
<?php require '../../main.inc.php'; $langs->load("mymodule@mymodule"); llxHeader('', $langs->trans("MyModule")); print load_fiche_titre($langs->trans("MyModule")); print '<p>This is my custom module page.</p>'; llxFooter();

This page can be accessed via the menu you define later.

Step 7: Define a Class (Optional)

If your module requires specific business logic or database interactions, define a class in class/myclass.class.php:

php
<?php class MyClass { public $db; public function __construct($db) { $this->db = $db; } public function myFunction() { // Your custom logic here } }

Step 8: Create SQL Script (Optional)

If your module needs to create database tables, define them in sql/mymodule.sql:

sql
CREATE TABLE IF NOT EXISTS llx_mymodule ( rowid INTEGER AUTO_INCREMENT PRIMARY KEY, entity INTEGER NOT NULL DEFAULT 1, tms TIMESTAMP, import_key VARCHAR(14) ) ENGINE=innodb;

Dolibarr will execute this script upon module activation to set up necessary tables.

Step 9: Activate the Module

  1. Log in to Dolibarr as an administrator.

  2. Navigate to Home > Setup > Modules/Applications.

  3. Find your module in the list (it should appear under the "Other" category).

  4. Click Activate to enable the module.

Step 10: Test Your Module

  • Access the setup page via Home > Setup > Modules/Applications > My Module.

  • Navigate to the public page you created to ensure it's displaying correctly.

  • Verify that any database tables have been created as expected.

Conclusion

Creating a custom module in Dolibarr allows you to tailor the ERP/CRM system to your specific business needs. By following the steps outlined above, you can develop modules that integrate seamlessly with Dolibarr's architecture, ensuring maintainability and compatibility with future updates.

Remember to adhere to Dolibarr's development guidelines and best practices to maintain code quality and system stability. With this foundation, you can expand your module's functionality, integrate with external systems, and provide a more customized experience for your users.

Comments

Log in or register to post comments