Quote from: Bruno17 at Jul 15, 2014, 07:46 PMcan you try it at a clean resource with nothing, than the form and the formit-call?
Yes I did. Here is the code from the content area of a ressource:
[[!FormIt? &hooks=`spam,formit2resource,email` &preHooks=`resource2formit`
&resource2formitfields=`pagetitle,content`&emailTpl=`myEmailChunk` &emailTo=`[email protected]`]]
<h2>Resource Form</h2>
<p>[[+fi.error.error_message]]</p>
<form class="form" action="[[~[[*id]]]]" method="post">
<input name="nospam:blank" type="hidden" />
<input name="resource_id" type="hidden" value="[[+fi.id]]" />
<label for="parent">
Parent:
<span class="error">[[+fi.error.parent]]</span>
</label>
<input id="parent" name="parent:required" type="hidden" value="2" />
<!--input id="parent" name="parent:required" type="text" value="[[+fi.parent]]" /-->
<br/>
<label for="pagetitle">
Pagetitle:
<span class="error">[[+fi.error.pagetitle]]</span>
</label>
<input id="pagetitle" name="pagetitle:required" type="text" value="[[+fi.pagetitle]]" />
<br/>
<label for="content">
Content:
<span class="error">[[+fi.error.content]]</span>
</label>
<textarea id="content" cols="55" rows="7" name="content:required:stripTags">[[+fi.content]]</textarea>
<br class="clear" />
<div class="form-buttons">
<input type="submit" value="Create Resource" />
</div>
</form>
The Template is really basic:
<html>
<head>
<title>[[++site_name]] - [[*pagetitle]]</title>
<base href="[[++site_url]]" />
</head>
<body>
[[*content]]
</body>
</html>
resource2formit snippet:
<?php
if (isset($_GET['resId'])){
if ($doc=$modx->getObject('modResource',array('id'=>$_GET['resId']))){
$docarray=$doc->toArray();
$fields = explode(',',$scriptProperties['resource2formitfields']);
$fields[] = 'id';
foreach ($fields as $field){
if ($doc->getFieldName($field) === null) {
/* if field isnt defined, look for TV value */
$tvValue = $doc->getTVValue($field);
if ($tvValue !== null) {
$hook->setValue($field,$tvValue);
}
} else {
/* otherwise get field value */
$hook->setValue($field,$docarray[$field]);
}
}
}
//$errorMsg = '<pre>'.print_r($docarray,true).'</pre>';
//$hook->addError('error_message',$errorMsg);
}
return true;
formit2resource snippet:
<?php
$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);
}
}
$doc->set('template', '3');
$doc->set('alias', $doc->get('alias' . '-' . $doc->get('id')));
$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();
}
}
return true;
It works, but it creates two resources with different IDs and same content.
Thanks in advance.
[ed. note: comnerds last edited this post 9 years, 9 months ago.]