Skip to content

Instantly share code, notes, and snippets.

@Sogl
Last active December 25, 2022 23:32
Show Gist options
  • Save Sogl/9914f02eadfc8f9071bdaed12356f4e9 to your computer and use it in GitHub Desktop.
Save Sogl/9914f02eadfc8f9071bdaed12356f4e9 to your computer and use it in GitHub Desktop.
<?php
private function processForm(Form $form, Event $event): bool
{
/** @var \Grav\Plugin\Form\Form $form */
$form->validate();
/** @var Flex $flex */
$flex = $this->grav['flex'];
/** @var FlexDirectory $dir */
$dir = $flex->getDirectory('therapies');
/** @var array $data */
$data = $form->getData()->toArray();
//no need captcha data
unset($data['basic-captcha']);
$addFolder = $this->grav['page']->folder();
$therapiesFolder = $this->grav['page']->parent()->folder();
$newData = [];
foreach ($data['description_files'] as $key => $file) {
//replace the "user/pages/xxx" url on the image into the flex route
$path = str_replace('user/pages/' . $therapiesFolder . '/' . $addFolder . '/', '', $file['path']);
$data['description_files'][$key]['path'] = $path;
//++
$newData[$path] = $data['description_files'][$key];
}
$data['description_files'] = $newData;
$obj = $dir->createObject($data);
$obj->setProperty('published', 0);
try {
/** @var FormFlash $flash */
$flash = $form->getFlash();
$obj->update($data, $flash->getFilesByFields(true));
$obj->save();
if ($obj instanceof FlexObjectInterface) {
$flash->clearFiles();
$flash->save();
}
} catch (\Exception $e) {
$form->setMessage($e->getMessage(), 'error');
//stop the event running
$event->stopPropagation();
//TODO: sync with dropzone (it's broken after that code)
// if ($obj instanceof FlexObjectInterface) {
// $flash->clearFiles();
// $flash->save();
// }
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment