Hacks

Show Your Custom Field Formatting List Who’s Boss

February 2, 2009
by Tim Kelty

A quick and painless hack makes it easy to select alternative formatting styles like Textile or Markdown for custom fields, and saves many repetitive steps.

EE Custom Fields

Edit (June 3, 2009): Since this article was written, Stephen Lewis of Experience Internet has written an extension called SL Field Formatting that solves the problem posed in this article. SL Field Formatting allows you to specify the default list of formatting options that will appear for all new weblog fields – including formats such as Textile, Markdown, and MarkyPants.

When EE sites grow, it can mean managing a lot of custom fields. I find this to be one of the most tedious parts of EE development, so when things come along like Brandon Kelly’s Gypsy, I’m all about it. Anything to make that process a bit more manageable.

I like to use Textile, a LOT. This means I’m very accustomed to publishing a new field, editing that field, editing the publish list, adding textile, then updating the field again. See what I mean? Tedious!

Today I got sick enough of it to poke around in system/cp/cp.publish_ad.php to see what’s doin’. I wanted Textile included by default. Turns out, the solution was quite simple. Now clearly this is a hack, so you know what that means. Tread softly, soldier.

First look for this block (approximately line 8043 of cp.publish_ad.php in ExpressionEngine 1.6.7):

{
$menulink  = '';
$typemenu .= $DSP->input_select_option('none', $LANG->line('none'), '')
      .$DSP->input_select_option('br', $LANG->line('auto_br'), '')
      .$DSP->input_select_option('xhtml', $LANG->line('xhtml'), 1);
}

Add a new line for the type of formatting you want to be included by default. First is the value, then the display text (note the period at the beginning of the line). For example:

.$DSP->input_select_option('textile', 'Textile', '')

Textile will now be included in default drop-down set when publishing a field. It should look like this:

{
$menulink  = '';
$typemenu .= $DSP->input_select_option('none', $LANG->line('none'), '')
      .$DSP->input_select_option('br', $LANG->line('auto_br'), '')
      .$DSP->input_select_option('textile', 'Textile', '')
      .$DSP->input_select_option('xhtml', $LANG->line('xhtml'), 1);
}

If you make it the last item in your list, remember the semi-colon, which follows the last item in the list.

Next look for (approximately line 8476 of cp.publish_ad.php in ExpressionEngine 1.6.7):

foreach (array('none', 'br', 'xhtml') as $val)
{
  $DB->query("INSERT INTO exp_field_formatting (field_id, field_fmt) VALUES ('$insert_id', '$val')");    
}

…And add textile to the array. This will add a row in the exp_field_formatting table for textile as well as the others. That will look like:

foreach (array('none', 'br', 'Textile', 'xhtml') as $val)
{
  $DB->query("INSERT INTO exp_field_formatting (field_id, field_fmt) VALUES ('$insert_id', '$val')");    
}

That’s it, you’re done. The same should work for any other field formatting you want to include by default. And don’t forget that you need to have the correct plugin installed for any field formatting type you’re including.

Go get em!

9 Comments:

Eric 02.02.09

Eric

I tried to figure this out a while ago, but couldn’t — thank you! I also posted in the forums about creating an extension that would include Textile by default — would that be possible?

Here is the forum post:

http://expressionengine.com/forums/viewthread/78512/

Tim Kelty 02.03.09

Tim Kelty

Making this into an actual extension would be a great idea, this is certainly more of a down and dirty approach. An addon that let you customize your defaults for other things like weblog prefs would be killer, too.

Ryan Masuga 02.03.09

Ryan Masuga

@Eric: I checked the thread you mentioned and it reminded me that I was experimenting with the publish_admin_edit_field_format hook when working on MD Markitup. I may have a half finished extension on my drive that could add field formatting.

I’m just reticent to work on any new addons because devot:ee itself needs all my attention, and of course, there is this legendary, fabled thing out there called “EE 2.0” that makes me not want to spend too much time on 1.6.x additions.

Eric 02.03.09

Eric

@Ryan: I understand your point and I hope EE 2.0 will offer a few more of these conveniences.

In the meantime, Tim’s hack should be a huge timesaver as I use textile on pretty much every textarea in every EE install I do. One question, though: What happens if you overwrite system/cp/cp.publish_ad.php (say if you upgrade EE to a new build)? Will the formatting on your existing Textile fields still be there, or do you have to hack the new file as well? (Actually, that’s three questions, I guess.)

Tim Kelty 02.03.09

Tim Kelty

@Eric: When you upgrade, your existing Textile fields will still be intact with Textile formatting, exactly as if you had done it manually (without the hack). You would have to reapply the hack to have Textile automatically added for new fields, however.

Eric 02.03.09

Eric

@Tim: That’s awesome; I typically only do this at the start of a project anyway. An unobtrusive hack if ever I saw one. Thanks again for the article.

John Faulds 02.03.09

John Faulds

I don’t use Textile, but it would be useful to somehow completely turn off the default formatting buttons for each new weblog and the formatting menu for each new custom field created without having to manuall alter those settings each time.

CreateSean 05.04.09

CreateSean

Just found this now and will be implementing for future builds. Do wish that the extension eric linked to had been built.

Eric 05.04.09

Eric

Not sure if I missed a step, but when I tried this, I couldn’t get it to work (on the latest version/build of EE). Instead, the formatting choices got duplicated in some cases (multiple XHTMLs listed in the dropdown, for example). In order to fix it, I had to undo the hacks and reset the formatting for the fields.

In the end, not a big deal and probably my own mistake, but I thought I’d report back anyway.

You must be registered member to comment. If you're already a member, log in now, and if not go register (it's free and easy!).