In the last article, we saw how to create a Weblink in code, but we neglected to set the responseCode property. You might, for example, want to use a different response code, such as 'HTTP/1.1 302 Moved Temporarily'. In this article, we'll see how to set the responsecode property when the Weblink is created.
The Problem
The properties field of the modResource object, like the properties field of an element, stores a JSON string. The JSON string itself is a little complicated because it involves a nested array where the outer level contains namespaces and the inner level contains an array of keys and values. In our case the responsecode value goes under the core namespace. The pure JSON string in this case looks like this:
$props = '{"core":{"responseCode":"HTTP\/1.1 301 Moved Permanently"}}';
It's a little easier to understand as a PHP array:
$props = array(
'core' => array(
'responseCode' => 'HTTP/1.1 301 Moved Permanently',
),
);
The Solution
The set() method of the xPDOObject, when used on a field with a PHP type of json like this one (properties), will accept either a JSON string or a PHP array. So we could use either of the examples above and set the properties field like this:
$webLink->set('properties', $props);
We could also insert either of these code examples into the $fields array we used with fromArray() in the previous example. In this case, that would work fine, but it's kind of a bad habit. The reason is that in some cases (like updating an existing object), there may already be properties set for the object. Using this method would wipe them out. It's also easy to make a mistake when setting up the JSON string or the PHP array.
We could get the existing properties, if they exist, and merge our property in with them, but it's fairly tricky. Luckily, there's a much easier way. The modResource object (and all the element objects) have a method called setProperty() that handles all the dirty work for us. This single line of code will do the job:
$webLink->setProperty('responsecode', 'HTTP/1.1 301 Moved Permanently');
The code above will set the responsecode property without disturbing any of the other properties.
The setProperty() method has a third argument that sets the namespace for the property, but its default value is core, which is exactly what we want.
The Full Code
Here is our code from the previous article with the added line to set the responsecode field:
/* Weblink creation code */
/* Set this to the ID of the resource you want to link to */
$content = 12;
// $content = 'https://mysite.com/somePage.html';
// $content = '[[~12]]';
$fields = array(
'content' => $content,
'class_key' => 'modWebLink',
'pagetitle' => 'My Page',
'menutitle' => 'My Page',
'alias' => 'my-page',
);
$webLink = $modx->newObject('modWebLink');
$webLink->fromArray($fields);
/* Set the response code */
$webLink->setProperty('responsecode', 'HTTP/1.1 301 Moved Permanently');
$webLink->save();
Coming Up
Now that we've seen how to create a Weblink, you might be curious about how to update an existing Weblink. We'll look at that in the next article.
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 Hosting.com (formerly A2 Hosting). (More information in the box below.)

Comments (0)
Please login to comment.