Creating MODX Articles in Code IV

Using the resource/create processor to create new Articles.


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.


MODX logo

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 createdby field explicitly, the created Article will have no author (the createdby field is set to 0).
  • If you set the published field to 1, the article will be published, but will have no publication date unless you set one in the publishedon field.
  • The publish_default and automatic_alias system Settings will be ignored.
  • Unless you explicitly set the template field, it will be set to 0 (the empty template).
  • The usual events (OnBeforeDocFormSave and OnDocFormSave will not fire and plugins attached to those events will not execute.
  • Worst of all, unless you explicitly set the alias field, the Article resource will have a blank alias -- even if you have the automatic_alias System 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., richtext, hidemenu, searchable, 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., introtext or description).

All the default System Settings (including automatic-alias) will be honored, and if you set published to 1, the publishedon and publishedby fields will be set.

You may also want to set the createdby and 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 warning and info messages, but normally, you won't see them.


Coming Up

In the next article, we'll look at how to use a plugin to create related resources when a new resource is created.



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.

  (Login)