In the previous article, we looked at how to use a chunk with placeholders to set the content of Articles resources created in code. In this one, we'll see how to create the resource using a MODX processor.
Drawbacks of Previous Method
In an earlier article, we mentioned that the quick method we used had certain drawbacks. Here's a list of them:
- Unless you set the
createdbyfield explicitly, the created Article will have no author (the
createdbyfield is set to
- If you set the
1, the article will be published, but will have no publication date unless you set one in the
automatic_aliassystem Settings will be ignored.
- Unless you explicitly set the
templatefield, it will be set to
0(the empty template).
- The usual events (
OnDocFormSavewill not fire and plugins attached to those events will not execute.
- Worst of all, unless you explicitly set the
aliasfield, the Article resource will have a blank alias -- even if you have the
automatic_aliasSystem Setting turned on!
You can set all those fields, as we did in the previous articles, but it's easy to forget one or more. You also won't get the advantage of the
automatic_alias System Setting, which makes sure that your aliases are consistent from page to page.
When you use the method from the previous articles, MODX will also ignore the defaults for other fields (e.g.,
cacheable etc.), unless you set them explicitly.
Luckily, there's a way to avoid all of that — using the
resource/create processor, which will set all those fields automatically and will honor the various System Settings.
Using the processor, you set the values you want in an array variable (let's call it
$articleFields), and pass that to the
resource/create processor. You still need to set the
parent field to make sure the Article is seen as an Article. You also need to set the ID of the blog article template in the
template field — otherwise the article resource will get the site's default template. The code looks like this:
/* Article Creation Code */ $articleFields = array( /* set this to the ID of an Articles Container */ 'parent' => 12, /* Required */ /* set this to the ID of the Article Template (not the container template) */ 'template' => 122, /* Required */ 'pagetitle' => 'Blog5', /* Required */ 'class_key' => 'Article', /* Required */ 'content' => '<br> <h3>Blog 5</h3>', /* Optional */ ); $modx->runProcessor('resource/create', $articleFields);
The other fields will be set automatically, though you're free to set them yourself if you don't want the defaults or have other fields to set (e.g.,
All the default System Settings (including
automatic-alias) will be honored, and if you set
publishedby fields will be set.
You may also want to set the
publishedby fields to the ID of a user other than the admin.
The events will be fired and any relevant plugins will execute.
Note that the process may throw a few harmless
info messages, but normally, you won't see them.
In the next article, we'll look at how to use a plugin to create related resources when a new resource is created.
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.)