I must be doing something wrong here because I can’t seem to get it to work.
I just want a page where users can edit a resource.
In this particular case I want User X to be able to edit page 12.
so on page 12 I call a certain chunk (via Personalize, but that part works).
That chunk contains
[[FormIt? &hooks=`Formit2Resource` &preHooks=`Resource2Formit` &Resource2FormitFields=`parent,pagetitle,content` &resId=`[[~[[*id]]]]`]]
<h2>[[~[[*id]]]]</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="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>
and
Formit2Resource
is
<?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', '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();
}
}
return true;
and Resource2Formit is
<?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;
I do get the form but it’s empty and clicking the Create Resource just empties the form and refreshes the page but nothing gets created.