Packaging Objects Into Extensions
You can ship an extension with custom data from your Latch installation, such as forms or menus.
The usual pattern is:
- Export the object SQL from the Latch dashboard.
- Place the SQL file in your extension's
installdirectory. - Run an installation routine from the extension's
initevent. - Rename
installtoinstall-oldafter a successful import so the routine does not run on every request.
Back up your files and database before testing installation routines.
Basic Structure
Create an initialization event at:
extensions/my_ext/lib/events/init.php
Example:
<?php
namespace Latch\Ext\MyExt\Events;
class Init
{
public static function event($args)
{
include_once __DIR__ . "/../my-ext.php";
$install_dir = __DIR__ . "/../../install";
if (!is_dir($install_dir) || LATCH_DEBUG)
{
return;
}
$log = \Latch\Util\new_log("ext.my_ext");
$log->info("Running MyExt extension installation routine.");
\Latch\Util\rrmdir($install_dir . "-old");
$_SESSION["latch"]->db->delete_parent("Example", "category = 'forms'");
$_SESSION["latch"]->db->import_sql($install_dir . "/forms_Example.sql");
rename($install_dir, $install_dir . "-old");
$log->info("Completed MyExt extension installation routine.");
}
}
You can refer to extensions.md for more information on event structure.
Exporting Objects
Head to your Latch dashboard and export the SQL for the objects you want to include in your extension. Move the file to a location such as:
extensions/my_ext/install/forms_Example.sql
Exported SQL should include a first-line type marker such as:
-- Type: forms
When that marker is present, import_sql() automatically calls fix_children_ids() for the imported object type after the SQL runs.
How the Routine Works
$install_dirpoints to the extension'sinstallfolder.- The routine exits unless
installexists andLATCH_DEBUGis disabled. install-oldis removed so a previous install directory does not block the rename.delete_parent()removes the existing parent object and its children. Parent names should be unique and are case-sensitive.import_sql()imports the exported SQL.- The
installfolder is renamed toinstall-old, preventing the routine from running again until a future extension update ships a newinstallfolder.
If your SQL file does not include a -- Type: marker, call fix_children_ids() manually after the import:
$_SESSION["latch"]->db->fix_children_ids("forms");