Upgrading to Movable Type 4 with MTCommentFields

By now, there’s a reasonable chance that if you’ve wanted to upgrade to the latest release of Movable Type, you’ve done so. But you may still be waiting. Perhaps you’re hoping that your favorite plugin will be updated (there are still a few that haven’t been released for MT4). Or maybe you’ve run into a problem that you can’t seem to overcome.

One of the issues that I run into fairly regularly is for people who use the MTCommentFields tag. This tag, originally released in version 3.0D – way back in 2004, for those of you keeping track – was designed so that you wouldn’t have to keep up with your comment form. Unfortunately, people like to keep up with their comment form, to move it around, change some wording, add features or just tinker. So it rapidly fell out of favor, and in MT4, it’s gone completely.

If you’re still using it and you want to upgrade, you’ll see a message that reads The MTCommentFields tag is no longer available; please include the Comment Form template module instead. You will be able to rebuild your templates, but you won’t be able to accept any new ones.

And naturally, if you’re upgrading, you won’t have this module to make that happen. To make matters worse, if you’re a user of certain versions, even if you try and get rid of the tag, you might still have problems. What do you do? Luckily, it’s an easy fix.

To fix this problem, you need to first make two changes to your Individual Entry Archive template. Users who first created this template in a version of Movable Type from v3.0D to 3.17 will have to make three changes. Ready? Here we go.

First, click on Design, then Archive Templates. You are looking for the Individual Entry Archive template. Near the top, locate the JavaScript in the template. It will look something like this:

<script type="text/javascript" language="javascript">
<!--

var HOST = \'<$MTBlogHost$>\';

// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
document.cookie = curCookie;
}

function getCookie (name) {
var prefix = name + \'=\';
var c = document.cookie;
var nullstring = \'\';
var cookieStartIndex = c.indexOf(prefix);
if (cookieStartIndex == -1)
return nullstring;
var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
if (cookieEndIndex == -1)
cookieEndIndex = c.length;
return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
if (getCookie(name))
document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
if (f.author != undefined)
setCookie(\'mtcmtauth\', f.author.value, now, \'/\', HOST, \'\');
if (f.email != undefined)
setCookie(\'mtcmtmail\', f.email.value, now, \'/\', HOST, \'\');
if (f.url != undefined)
setCookie(\'mtcmthome\', f.url.value, now, \'/\', HOST, \'\');
}

function forgetMe (f) {
deleteCookie(\'mtcmtmail\', \'\', HOST);
deleteCookie(\'mtcmthome\', \'\', HOST);
deleteCookie(\'mtcmtauth\', \'\', HOST);
f.email.value = \'\';
f.author.value = \'\';
f.url.value = \'\';
}

//-->
</script>

You want to remove this entire block and change it to this:

<script type="text/javascript" src="<$MTLink template="javascript"$>"></script>

Various versions of the software would create a Site JavaScript file for you. If you view your template listing (index templates) and see a file with a name of Site JavaScript, you’ll enter site javascript here. If you see a file with a name of JavaScript, enter javascript here. Case is not important.

This allows Movable Type to pull the name of another template – in this case the JavaScript (or Site JavaScript) – and fill in the blank. This is a much better solution than hardcoding the name, because people have a tendency to tinker with names, and what though a lot of people don’t change the name, some might, so this covers most bases. If you change the name, just make sure you change this value.

If for any reason you don’t have the JavaScript (or Site JavaScript) template, you can find a new one by looking in the default_templates directory if your Movable Type distribution. In fact, this isn’t a bad idea, as new content is periodically added to this file, so you may gain some new functionality by updating this file from time to time as new versions are released.

Next, you need to scroll down and locate the MTCommentFields tag. By default, it is located slightly down the screen, between two tags for MTIfCommentsAllowed:

<MTIfCommentsAllowed>
<MTCommentsFields>
</MTIfCommentsAllowed>

You can simply delete it – but if you do, you need to replace it with something (unless you don’t plan on accepting comments, in which case you can save it and you’re done).

If you do want to accept comments, then the simplest change is to alter the tag by changing all three lines to an MTInclude statement, which includes the aforementioned Comment Form. Don’t worry about the form itself that you need to include right now – we’ll create that in a moment. Here is what it should look like:

<$MTInclude module="Comment Form"$>

Remember, replace all three lines with this one. It won’t really hurt to leave the outer MTIfCommentsAllowed statements there, but you don’t need them, and you’re just making more work for the template, because the Comment Form template handles that for you.

Before you save the template, however, you may have one more change to make. So scroll down to the bottom and look for these lines:

<script type="text/javascript" language="javascript">
<!--
if (document.comments_form.email != undefined)
document.comments_form.email.value = getCookie("mtcmtmail");
if (document.comments_form.author != undefined)
document.comments_form.author.value = getCookie("mtcmtauth");
if (document.comments_form.url != undefined)
document.comments_form.url.value = getCookie("mtcmthome");
if (getCookie("mtcmtauth") || getCookie("mtcmthome")) {
document.comments_form.bakecookie[0].checked = true;
} else {
document.comments_form.bakecookie[1].checked = true;
}
//-->
</script>

Present in the default template from version 3.0D through 3.17, these JavaScript lines will cause your comment template to have problems if you leave them there. Specifically, the cookies that are read from the computers of your visitors will have conflicts and the fields in the form will not be filled in correctly, even though the form data will be set on the fields. Sound confusing? It is. Don’t worry about it, and just get rid of these lines. They are down near the bottom of the template, just before the closing tags.

Now you can save the Individual Entry Archive template.

When you do, the template is parsed and it will realize that you do not have a module for the Comment Form. Over in the sidebar, under the Includes and Widgets section, it will list the modules used, and you will be presented with a link to create this form if it does not already exist. If it does exist, then you’re already done. This probably won’t be the case, or you wouldn’t be reading this tutorial. So click the create link.

Movable Type uses a bit of intelligence here and it will actually create the module for you, with the name you specify and everything. All you have to do is paste it into the text area. Here is the form you need:

<MTEntryIfCommentsOpen>
<div class="comments-open" id="comments-open">
<h2 class="comments-open-header"><__trans phrase="Leave a comment"></h2>
<div class="comments-open-content">
<MTIfRegistrationAllowed>
<div id="comment-form-external-auth">
<script type="text/javascript">
<!--
<MTIf name="comment_preview_template">is_preview = true;</MTIf>
writeCommenterGreeting(commenter_name, <$MTEntryID$>, <$MTEntryBlogID$>, commenter_id, commenter_url);
//-->
</script>
</div>
</MTIfRegistrationAllowed>
<form method="post" action="<$MTCGIPath$><$MTCommentScript$>" name="comments_form" id="comments-form" onsubmit="if (this.bakecookie.checked) rememberMe(this)">
<input type="hidden" name="static" value="1" />
<input type="hidden" name="entry_id" value="<$MTEntryID$>" />
<input type="hidden" name="__lang" value="<$MTBlogLanguage$>" />
<div id="comments-open-data">
<div id="comment-form-name">
<label for="comment-author"><__trans phrase="Name"></label>
<input id="comment-author" name="author" size="30" value="<MTIf name="comment_preview_template"><$MTCommentAuthor encode_html="1"$></MTIf>" />
</div>
<div id="comment-form-email">
<label for="comment-email"><__trans phrase="Email Address"></label>
<input id="comment-email" name="email" size="30" value="<MTIf name="comment_preview_template"><$MTCommentEmail encode_html="1"$></MTIf>" />
</div>
<div id="comment-form-url">
<label for="comment-url"><__trans phrase="URL"></label>
<input id="comment-url" name="url" size="30" value="<MTIf name="comment_preview_template"><$MTCommentURL encode_html="1"$></MTIf>" />
</div>
<div id="comment-form-remember-me">
<label for="comment-bake-cookie"><input type="checkbox" id="comment-bake-cookie" name="bakecookie" onclick="if (!this.checked) forgetMe(document.comments_form)" value="1" />
<__trans phrase="Remember personal info?"></label>
</div>
</div>
<div id="comments-open-text">
<label for="comment-text"><__trans phrase="Comments"> <MTIfAllowCommentHTML><__trans phrase="(You may use HTML tags for style)"></MTIfAllowCommentHTML></label>
<textarea id="comment-text" name="text" rows="15" cols="50"><MTIf name="comment_preview_template"><$MTCommentBody autolink="0" sanitize="0" convert_breaks="0" encode_html="1"$></MTIf></textarea>
</div>
<MTIfNonEmpty tag="MTCaptchaFields">
<MTIfCommentsAccepted><MTIfRegistrationAllowed><MTElse><$MTCaptchaFields$></MTIfRegistrationAllowed></MTIfCommentsAccepted>
<div id="comments-open-captcha">
</div>
</MTIfNonEmpty>
<div id="comments-open-footer">
<input type="submit" accesskey="v" name="preview" id="comment-preview" value="<__trans phrase="Preview">" />
<input type="submit" accesskey="s" name="post" id="comment-submit" value="<__trans phrase="Submit">" />
<MTIf name="comment_preview_template">
<input type="button" name="cancel" id="comment-cancel" value="<__trans phrase="Cancel">" onclick="window.location='<$MTEntryPermalink$>'" />
</MTIf>
</div>
</form>
</div>
</div>
</MTEntryIfCommentsOpen>

You can also find this form in the comment_form.mtml file in the default_templates directory of your Movable Type distribution.

Once you have pasted the contents of this form into the module, click save. Then you can rebuild your Individual Archives, and you should be able to accept comments as normal again!

Tutorial cross posted on Learning Movable Type.


Posted

in