Skip to content

For other plugins

Flux Importer writes entries through EmDash's own content handlers, so EmDash's content hooks run for imported entries as they do for an editor's saves. Another plugin reacts to them with content:afterSave and content:afterDelete, and tells them apart by their flux_source_id field.

Flux Importer adds no hooks of its own on EmDash. EmDash’s plugin hooks are a fixed set, declared when a plugin is defined, with no events from one plugin to another. A plugin that wants to index, sync or announce imported entries uses EmDash’s content hooks, the same ones it uses for everything else.

What the import does Hook Event
Creates an entry content:afterSave isNew: true
Updates an entry content:afterSave isNew: false
Moves a missing entry to the trash (missing entries) content:afterDelete permanent: false
A rollback puts back an entry the import updated content:afterSave isNew: false
A rollback deletes an entry the import created content:afterDelete permanent: false as it moves to the trash, then permanent: true as it’s deleted for good

A rollback works through the import’s writes newest first, so its events come in that order. An entry it only moves to the trash, because it can’t confirm the import created it, gets the first content:afterDelete and not the second. An entry an import leaves unchanged runs no hook.

Publishing, unpublishing, scheduling and restoring from the trash run EmDash’s other content hooks: content:afterPublish, content:afterUnpublish, content:afterSchedule and content:afterRestore. So a missing entry that is unpublished, rather than trashed, runs content:afterUnpublish, and a rollback that brings a trashed entry back runs content:afterRestore.

All of these hooks need the content:read capability.

content:afterSave receives:

  • content: the whole saved entry. Its ID is content.id, its fields are under content.data, and it also carries slug, status and locale.
  • collection: the collection’s slug.
  • isNew: true for a create, false for an update.
  • locale: the entry’s locale.
  • actor and editedBy: the Administrator who ran the import. A scheduled import runs without a user, so its events have neither.

content:afterDelete receives id, collection and permanent. The entry is already out of ordinary reads, so a plugin that needs its fields keeps its own copy from content:afterSave.

EmDash’s hook reference describes every field.

Every entry an import writes holds its import key in the flux_source_id field: the key of the record it came from, such as a SKU. The first real import into a collection adds the field, indexed.

import { definePlugin } from "emdash";
export function createPlugin() {
return definePlugin({
id: "import-notifier",
version: "0.1.0",
capabilities: ["content:read"],
hooks: {
"content:afterSave": async (event, ctx) => {
const key = event.content.data?.flux_source_id;
if (typeof key !== "string" || key === "") return; // not from an import
ctx.log.info(`${event.isNew ? "Imported" : "Re-imported"} ${event.collection}/${key}`);
},
},
});
}

An entry an editor creates has no flux_source_id. An imported entry that an editor saves later keeps its key, so the field says where an entry came from, not who saved it last.

  • Draft first, then published. The import saves an entry, then publishes it. content:afterSave sees a new entry as a draft; content:afterPublish follows when the import publishes. React to content:afterPublish when only live content matters.
  • At least once. EmDash runs each hook once per write, but a record can be written more than once: when the server stops in the middle of a batch, the resumed job repairs the record it was writing and can save it again. Make handlers idempotent, for example by keying your own records by content.id.
  • After the write. EmDash runs the after-hooks once the write has succeeded, and may run them after the response. A hook that throws is logged by EmDash; it doesn’t undo the write or fail the record.
  • Dry runs run nothing. A dry run, of an import or of a rollback, writes no entries, so no content hook runs.
  • No “job finished” event. EmDash has no hook for it, and plugins can’t add one. A plugin that needs to know when a run ends reads the job through the jobs API.