Create additional pages after the page is created

I have a product page with pricing and link to payment service.
It’s needed now to separate the pricing page to a standalone one, that should also be unique for the particular product.
I think it should be done with some sort of page creational hook in modx, but I’m not really sure what exactly to use. OnDocFormSave will be fired every time the resource is updated, right? So that’s not what I really need. Or maybe I can track somehow if the resource exists at that point?

Can you help, please? I need to just create additional resource when the product is created and write that additional resource id in the TV in product’s resource.

Thanks a lot in advance

1 Like

This is a good question. If you figured it out, please post about what you did so other MODX users can do the same.

If not, please give some more details about what you want to do and we’ll be glad to help.

I don’t really write on PHP, so not sure how good it is, but it seems to be working:

<?php
if ($modx->event->name == 'OnDocFormSave') {
    
    $template = $resource->get('template');

    if ($template === 32) { // template id to apply plugin to
        $tv = 'pricingResourceId'; // tv on parent resource, that contains the child id
        $tvv = $resource->getTVValue($tv);
        if ($mode == 'new' || empty($tvv)) { // if creating new resource or there's no child on existing one
            $id = $resource->get('id');

            // first, define what we want to create:
            $parent       = 415; // simply the folder for the children
            $pagetitle    = 'Example Page';
            $longtitle    = 'Example - A New Document Created Through The Modx API';
            $alias        = 'example-page';
            $description  = 'Example description...';
            $template     = 36; // template id that the child should use
            $published    = 1;
            $content      = '<p>This is some sample content.</p>';

            // secondly, create the new resource and save it:
            $doc = $modx->newObject('modDocument');
            $doc->set('parent', $parent);
            $doc->set('pagetitle', $pagetitle);
            $doc->set('longtitle', $longtitle);
            $doc->set('alias', $alias);
            $doc->set('description', $description);
            $doc->set('template', $template);
            $doc->set('published', $published);
            $doc->setContent($content);

            $doc->save();

            // thirdly, retrieve the newly created resource so we update TV value on parent:
            // i'm not really sure it's the best way to get child, but not sure if I can get created child id at this point (should try it out later)
            $newdoc = $modx->getObject('modDocument', array('pagetitle' => $pagetitle));
            $newid = $newdoc->get('id');

            $resource->setTVValue($tv, $newid);
        }
    }
}
1 Like

It looks pretty good. Thanks for posting it. :slight_smile:

I hope you don’t mind a few minor suggestions.

Although it’s often used, you don’t need the test at the top (don’t forget to remove the closing }):

if ($modx->event->name == 'OnDocFormSave') {

as long at that’s the only event the plugin is connected to the test will always pass.

This code:

if ($mode == 'new' || empty($tvv)) {

Should be:

if ($mode === modSystemEvent::MODE_NEW || empty($tvv)) {

In all cases, modDocument should probably be modResource. It’s recommended to work with the parent class. If you need to skip weblinks or symlinks, you can test the class_key of the resource at the top.

You might want to make sure the save() works before executing the last three lines:

if ($doc->save()) {
  /* Your last three lines */
} else {
    $modx->log(modX::LOG_LEVEL_ERROR, ' [YourPluginName] failed to save child resource';
}

This last one is optional, it will save a little memory and make your code shorter:

// first, define what we want to create:
$fields = array(
    'parent'       = 415, // simply the folder for the children
    'pagetitle'    = 'Example Page',
    'longtitle'    = 'Example - A New Document Created Through The Modx API',
    'alias'        =  'example-page',
    'description'  = 'Example description...',
    'template'     = 36, // template id that the child should use
    'published'    = 1,
    'content'      = '<p>This is some sample content.</p>',
);

$doc = $modx->newObject('modResource');
$doc->fromArray($fields, "", false, true);
if ($doc->save()) {
  /* Your last three lines */
}
1 Like

Thank you for the corrections :slight_smile:

1 Like