Problem with my plugin with the OnDocFormSave event — Endless saving loop

macOs : 15.3.1 (24D70)
MAMP Pro 7
Modx : 3.0.3 pl
PHP : 8.4.2
Collections : 4.1.1-pl

With the Collections extra, I created a product catalog, for the moment the catalog contains about thirty products.

I created a snippet to generate a json file with some product data. This snippet is run from a resource and it works fine.

Except that following the modification of the data of a product we are obliged to render the resource containing the snippet to update the json file.

So I made a plugin from my snippet that fires on the OnDocFormSave event to automatically update my json file.

But my plugin doesn’t work. When I click the save button Modx goes into an endless loop and the json file is not created or modified.

Here is my code:

// Génére un fichier json avec certaines données des fiches
if ($modx->resource->get('parent') != 8) {
	return;
}
$json = $modx->runSnippet('getResources', array('parents' => 8,'tpl' => 'tplJason', 'tplLast' => 'tplLastJason','processTVs' => 1, 'sortby' => 'menutitle', 'published' => 1, 'includeTVs' => 1, 'limit' => 0));
$json = "[" . "$json";
$fichierChemin = $_SERVER['DOCUMENT_ROOT'] . "/assets/data/genetics-fr.json";
$myfile = fopen($fichierChemin, "w") or die("Unable to open file!");
fwrite($myfile, $json);
fclose($myfile);

Could someone please point out my mistakes?

Thanks for your help

Use if ($resource->get('parent') != 8) { instead.

Thanks, your recommendation solved my problem, have a good day.