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:

  1. Export the object SQL from the Latch dashboard.
  2. Place the SQL file in your extension's install directory.
  3. Run an installation routine from the extension's init event.
  4. Rename install to install-old after 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

  1. $install_dir points to the extension's install folder.
  2. The routine exits unless install exists and LATCH_DEBUG is disabled.
  3. install-old is removed so a previous install directory does not block the rename.
  4. delete_parent() removes the existing parent object and its children. Parent names should be unique and are case-sensitive.
  5. import_sql() imports the exported SQL.
  6. The install folder is renamed to install-old, preventing the routine from running again until a future extension update ships a new install folder.

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");