-
- 30 Posts
So I've extended the modResource to add a few more fields.
Now, I'm trying to determine how to add a form to the front end of the site to allow users to add a new resource including the additional fields. I've toyed with formit2resource and it does just that. It will add a new resource, however how do I get it to create the pagetitle, alias, content, etc. and also my new extended fields which are in a different table?
I've considered a two part form. Using formit2resource to add the initial new resource and then formit retriever to get the ID of the new resource and continue on with something like formit2db on the second part of the form. The problem is retrieving the ID from the new resource doesn't really work as expected.
Anyone have experience with this? Any thoughts on where to look for more info?
-
- 1,145 Posts
Is NewsPublisher an option?
TinymceWrapper: Complete back/frontend content solution.
Harden your MODX site by
passwording your three main folders:
core, manager, connectors and renaming your
assets (thank me later!)
5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
-
- 5,888 Posts
you just need to extend formit2resource a bit, if you want to save everything at once
do you have a link to the formit2resource - code, which you are using currently?
maybe the hook-snippet could also be modified to use runProcessor('resource/create') instaed of xpdo directly
-
- 5,888 Posts
something like that should work:
<?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');
//set other doc-fields here
if ($doc->save()) {
$id = $doc->get('id');
// set fields of extented table here
$classname = 'yourExtClass';
$ext_object = $modx->newObject($classname);
$ext_object->set('resource_id', $id);// set the relation-field here
$ext_object->set('yourField', $hook->getValue('yourField'));
$ext_object->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;
-
- 30 Posts
Thanks Bruno!
I'll give this a try. So it seems from my limited programming knowledge that the snippet saves the resource, retrieves the id and then does a second save?
-
- 24,544 Posts
Have you tried NewsPublisher? I think it can work for this.
-
- 30 Posts
Bob,
No I've never worked with newspublisher. My concern is going to be in attaching files in a future update of my current project. I'm not adverse to using it as long as it has capabilities for future expansion. I'll look at it.
-
- 24,544 Posts
NP won't let you attach files without some custom addition to the form, but whatever you would use with FormIt should work there as well. NP fires the OnDocFormSave event, which might make it easier.
It might be easier yet to use one or more File TVs if that would work for you. NP should handle those fine.