Thanks Bruno,
Here's my contribution. I only just noticed that someone did this with a random alias, but here is my code to take the title and clean it before putting in the alias.
I couldn't get the modx system regexp to work so cobbled my own together (my brain still hurts - but regular expressions do that) - I am certain someone will find fault with this and am happy for improvements
It also has a redirect to a Thank You page at the end.
$doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
if (empty($doc)){
$doc = $modx->newObject('modResource');
$doc->set('createdby', $modx->user->get('id'));
}
else{
$doc->set('editedby', $modx->user->get('id'));
}
$allFormFields = $hook->getValues();
foreach ($allFormFields as $field=>$value)
{
if ($field !== 'spam' && $field !== 'resource_id'){
$doc->set($field, $value);
}
//we need to parse the title into the alias
if($field == 'pagetitle'){
//replace spaces with -
$alias = preg_replace('/[\' \']/' , '-' , $value);
//remove non alpha and a common injection string
$alias = preg_replace('/[^a-z0-9\.\x0B\-]/i' , '' , $alias);
//this is the standard revo regexp
// \0\x0B\t\n\r\f\a&=+%#<>"~:`@\?\[\]\{\}\|\^'\
}
}
// now set the alias
$doc->set('alias', $alias);
$doc->set('template', '1');
$doc->save();
foreach ($allFormFields as $field=>$value)
{
if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
{
/* handles checkboxes & multiple selects elements */
if (is_array($value)) {
$featureInsert = array();
while (list($featureValue, $featureItem) = each($value)) {
$featureInsert[count($featureInsert)] = $featureItem;
}
$value = implode('||',$featureInsert);
}
$tv->setValue($doc->get('id'), $value);
$tv->save();
}
}
$url = $modx->makeUrl(84);
$modx->cacheManager->refresh(); // suggested by gallenkamp
$modx->sendRedirect($url);
[ed. note: xxxmicrobexxx last edited this post 11 years, 4 months ago.]