MODX Community

Create Article Resource via snippet

We’re successfully creating a new resource from user input. This is done via a front end FormIt form with a hook. This uses the influencerPageGenerator as seen here: https://forums.modx.com/thread/88270/create-a-new-modx-resource-with-the-api

All works well, only the pages.resources we create however need to be ‘Articles’ as the client is using the Articles plugin to create this particular kind of page. So the ‘$parent’ resource ID is actually an Article container resource.

So rather than the resource appearing in the regular resource tree, it needs to be added to the Articles resource list. We’ve looked through the various files in the Articles plugin but are not 100% sure about how to go about this.

Would anybody be able to point us in the right direction?

1 Like

Did you set the class_key to Article for the new created resources?

1 Like

What you want should happen automatically as long as the class_key is set to Article and the parent field is set to the ID of the Articles Container.

1 Like

Thanks for the replies. We’ve amended the code to this:

$doc = $modx->newObject('modDocument');
  $doc->set('class_key','Article');
  $doc->set('parent',$parent);
  $doc->set('pagetitle',$pagetitle);

etc

but it creates a 500 error. Are we setting the class-key incorrectly?

1 Like

You may need to first make sure the Articles service is loaded.

This may also help:

$doc = $modx->newObject('Article');
$doc->set('class_key', 'Article');
$doc->set('parent', $parent);
$doc->set('pagetitle', $pagetitle);

If that doesn’t resolve it, look at your PHP error log for the reason behind the 500 error. That would be massively helpful identifying what is going wrong.

Mark may well be right, but I was going to suggest this:

$doc = $modx->newObject('modResource');