Skip to content

Commit

Permalink
Update RapidObjectCreateAdmin.php
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderwar authored Feb 6, 2018
1 parent 40a0ff8 commit d6bac9f
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/RapidObjectCreateAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,65 @@ public function getCurrentObject()

return $object;
}
}

/**
* @param array $data
* @param Form $form
*
* @return mixed
*/
public function doSave($data, $form)
{
$objectClass = static::config()->get('tree_class');

/** @var DataObject|Versioned $object */
$object = $objectClass::create();

$currentStage = Versioned::get_stage();
Versioned::set_stage('Stage');

$controller = Controller::curr();
if (!$object->canEdit()) {
return $controller->httpError(403);
}

try {
$form->saveInto($object);
$object->write();
} catch (ValidationException $e) {
$result = $e->getResult();
$form->loadMessagesFrom($result);

$responseNegotiator = new PjaxResponseNegotiator([
'CurrentForm' => function () use (&$form) {
return $form->forTemplate();
},
'default' => function () use (&$controller) {
return $controller->redirectBack();
}
]);
if ($controller->getRequest()->isAjax()) {
$controller->getRequest()->addHeader('X-Pjax', 'CurrentForm');
}
return $responseNegotiator->respond($controller->getRequest());
}

Versioned::set_stage($currentStage);
if ($objectClass::has_extension(Versioned::class)) {
if ($object->isPublished()) {
$this->publish($data, $form);
}
}

$link = '"' . $object->singular_name() . '"';
$message = _t('GridFieldDetailForm.Saved', 'Saved {name} {link}', [
'name' => $object->singular_name(),
'link' => $link
]);

$form->sessionMessage($message, 'good');
$action = $this->edit(Controller::curr()->getRequest());

return $action;
}
}

0 comments on commit d6bac9f

Please sign in to comment.