Creating Additional Pages after a New Page is Created

Using a plugin to create related pages after a new resource is created

In the previous article, we looked at how to create the an Article in code using the MODX resource/create processor. This one is something of a spin-off from that page. We'll see how to use that same processor to create other pages when a new page is created.

MODX logo

The Problem

Lets say that you already have code that creates a particular page, but you'd also like to create one or more child pages under it at the same time. For example, you might have an eCommerce setup that helps you create a new resource for each product you sell. You'd like to add reviews of the product, but not on the same page. You can create a child page under it using a fairly simple plugin that calls the MODX resource/create processor we saw in the previous article.

Creating the Plugin

The code here assumes that your product pages are all in the same folder. You'd create the plugin (let's call it CreateReviewPage) by going to the Elements tree, right-clicking on the "Plugins" heading and selecting "Create New Plugin". Fill in the name of the plugin and a descriptions, then on the System Events tag, check the box next to OnDocFormSave to attach the plugin to that event.

The first step in the plugin code is to make sure it only fires when the resource is new and is directly under the "Products" folder:

/* CreateReviewPage plugin */

$productFolder = 12; // change this to the ID of the products folder

/* Only execute for children of products folder */
if ($resource-get('parent') !== $productFolder) {
/* Only execute for new pages */
if (!($mode == modSystemEvent::MODE_NEW)) {

Now we'll set the parent of the new resource to the product page ID. We'll also make the pagetitle the same as the parent but with -reviews at the end.

    $parent = $resource->get('id');
    $pagetitle' = $resource->get('pagetitle') . '-reviews';
    $alias = $resource->cleanAlias($pagetitle);
    $template = $resource->get('template'); // or another template ID
    $published = true; // or false

$fields = array(
    'parent' => $parent,
    'pagetitle' => $pagetitle,
    'alias' => $alias,
    'template = $template,
    'published' => $published,

Important: Note that the variables at the top are set with = and each line ends with a semicolon. The array members below use => and end with a comma.

You may also want to set the description, introtext (summary), longtitle, and menutitle fields using information pulled from the product page. Just add them to the list of variables and then to the list of array members. In both cases, the order of the items doesn't matter as long as they're all there.

If you have the automatic_alias System Setting turned on, you can leave out the alias lines.

If you want to set the content of the new page, you can add the content field to the variable list and the array. It's convenient to use the Heredoc format we saw in the article before the previous one, but it's not necessary.

You might, for example, want to use Quip to handle the comments. Your content might look something like this (using the heredoc format:

$content = <<<EOT
<div class="reviews">

    <p>Some text about review guidelines here.</p>



Then, it's just a matter of adding this line to the array:

    'content' => $content,

Other Use Cases

The technique above can be used to create any number of pages when a new resource is saved. The pages don't have to be under the resource that was just saved or use its template. You'll want to set the parent and template, but the pages can go anywhere and can have any content you like.

In an article long ago, I discussed creating a user page when a new user registers. You could modify that code to create child pages under the user page, but you could also leave it alone and just create a plugin to do the job. Be careful, though, to make sure that, like the plugin above, it only executes for the pages that should get children and only when the page just saved is new. You don't want create a new child page every time the page is updated.

Coming Up

In the next article, we'll look at using FormIt hooks.

For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.