sheadawson/silverstripe-quickaddnew — pull request #14 by micschk (2014-10-07 19:43:16+00:00)
Title: Make newly created object available to callback closure function

Then within the callback closure function, extra properties can be set (optionally) on the newly created object if necessary.

Example usage:

// Closure for getting tags, including newly created one
$blogitem = $this;
$tagsource = function($newtag = null) use($blogitem) {
// link created FilterableTag to Parent of this page
if($newtag){
$newtag->ItemHolderID = $blogitem->Parent()->ID;
$newtag->write();
}
// return all Tags (including new one)
return $blogitem->Parent()->Tags()->map()->toArray();
};
// Add Categories field
$tagsList = ListboxField::create(
"Tags", _t("FilterableArchive.Tags", "Tags"),
$tagsource()
)->setMultiple(true);
$tagsList->useAddNew('Tag', $tagsource);

$fields->insertBefore($tagsList, "Content");