Add a New Entry Field

Note: Revised for compatibility with MT version 2.21.


I read the Movable Type forum regularly and have noticed that every now and then there is a question on how to add a new field to the Entry records. Being inquisitive by nature, I thought I would try to find out. The following is a description of the method I used; there are probably others but this seems to work.


Warning: Be sure to make back up copies of your complete database directory and all the files modified... just in case. I was able to add the new field to existing entries without any difficulty but be sure to keep a copy of the database files, not just an export set.


We will be working with the following files:
Entry.pm
Context.pm
CMS.pm
edit_entry.tmpl


Step 1: Add the field to the Entry object.

We will be adding a field named "DisplayOrder" in this example. You can of course name the field what ever you want; just subsitute the name you want where I have used "DisplayOrder". Open the Entry.pm file and search for convert_breaks. You will see the following lines:

'title','excerpt','text','text_more','convert_breaks',
## Have to keep this around for use in mt-upgrade.cgi.
'category_id',


Add the new field name. You will need to enter it enclosed in single quotes followed by a comma. Therefore, after adding the new "DisplayOrder" field, the above lines look like:

'title','excerpt','text','text_more','convert_breaks',
'displayorder',
## Have to keep this around for use in mt-grade.cgi.
'category_id',


If you want to update the POD documentation at the bottom of the file, search for $entry->text('Some text'); and then enter
$entry->displayorder('display order'); after it.


Save the Entry.pm file and close it.


Step 2: Register the field

Each field in a record must be "registered" to place it in the proper parsing context. Essentially, this enables Movable Type to find the field when it is needed. Open the Context.pm file and search for handler(EntryExcerpt. You will see:

$ctx->register_handler(EntryExcerpt => \&_hdlr_entry_excerpt);


Enter the following line to register a handler subroutine for the new field (substitue the field name you are using for "DisplayOrder"):

$ctx->register_handler(EntryDisplayOrder => \&_hdlr_entry_displayorder);


Now search for sub _hdlr_entry_author and then insert the following lines above the author handler subroutine:

# Create a handler for the entry record displayorder field 
sub _hdlr_entry_displayorder {
  my $e = $_[0]->stash('entry')
    or return $_[0]->_no_entry_error('MTEntryDisplayOrder');
  $e->displayorder;
  # These lines added for compatibility with version 2.21
  # Thanks to Adam Rice for testing this.
  my $a = $e->displayorder;
  $a = defined $a ? $e->displayorder  : '';

}
sub _hdlr_entry_author {


Note: The lines above wrap due to the display area on this page. Just make the subroutine look like the others near it.


Save the Context.pm file and close it.


Step 3: Enable the field in the Preview

You only need to do this if you want your new field to display in the entry preview. Open the CMS.pm file and search for $entry->convert_more. You will see:

$entry->text_more($q->param('text_more'));


Enter the following line to make the new field available to the preview page:

$entry->displayorder($q->param('displayorder'));


Now scroll down a few lines until you see:

my $preview_code = <<'HTML';
<p><b><$MTEntryTitle$></b></p>
<$MTEntryBody$>
<$MTEntryMore$>
HTML


and insert an MT tag where you want the new field to display on the preview page. I put mine at the top and made it red, so my preview output description looks like:

<p><b><$MTEntryTitle$></b></p>
<p><font color="Red"><$MTEntryDisplayOrder$></font></p>
<$MTEntryBody$>
<$MTEntryMore$>
HTML


Save the CMS.pm file and close it.


Step 4: Enable the field in the Entry form

Almost there! All we need to do now is to place a form field in the Entry form so you can enter something in your new field. In my case, I only want to be able to enter a small amount of text so I chose to use an input type=text field similiar to that used for the Title field. Open the edit_entry.tmpl file and search for Main Entry Text. This will get you in about the right spot. You will see something like:

<tr>
  <td width="402">
    <font class="pagetitle">Main Entry Text</font>
  </td>


You should probably scan the html code to see where you want to place your new field in the form. In my case, I put it just above the Main Entry Text field. This area of the page is a two column layout so I chose to place the field name in the left column and the form field next to it in the right column. If you are using a larger field and want to use a textbox form field, you will probably want to do it like the Main Entry Text field. Play with it a little until you have the presentation you want. What I did was to insert the following lines just above the row containing the Main Entry Text field.

<tr>
  <td valign="top">
    <font class="pagetitle">Display Order</font>
 
</td>
  <td>
    <input class="text-short" name="displayorder"
      value="<TMPL_VAR NAME=DISPLAYORDER>">
  </td>
</tr>
<tr>
  <td width="402">
    <font class="pagetitle">Main Entry Text</font>
  </td>


Save the edit_entry.tmpl and close it.


Now all you need to do is to include the new field you just created in a template. You can use an MT tag like <$MTEntryDisplayOrder$> just as you would use any of the other Entry field tags.


Good Blogging!

Comments
Post a comment

Name:


Email Address:


URL:


Comments:


Remember info?



   

© 2002 bnsDesigns