Community Documentation

Creating an input format programmatically

Last updated June 11, 2010. Created by NancyDru on June 11, 2010.
Log in to edit this page.

We needed to create a way to handle our RSS feeds differently than a standard teaser display. After going through several iterations of hook_nodeapi code, which were all less than we wanted, we decided to look at using the standard Drupal filter ("input format") system. Since check_markup takes a filter format id as its second parameter, this just might be the easier way out.

Coding up a custom filter is not a big deal; there's a good example in the API. So we did that, and it turned out to be much smaller than the nodeapi code because it didn't need to check for all kinds of different situations.

We then set up the full input format manually and tested. Everything was great. ...Except... [Isn't there always an "except?"] We have three tiers of systems to go through to get this stuff live. None of us were keen on creating the input format again so many times; it's a manual process, so prone to errors.

So the obvious answer is to do it programmatically. So we searched on DO and couldn't find any documentation on how to do this. Oh, well, time to re-invent the wheel... So here's the code we came up with; we hope it gives someone else a jump start.

<?php
 
// First we want a complete list of roles, because we want everyone to have access to this.
 
$roles = array();
 
$result = db_query("SELECT rid FROM {role}");
  while (
$r = db_fetch_array($result)) {
   
$roles[] = $r['rid'];
  }
 
// Build an array for writing.
 
$format = array('name' => 'test_format', 'roles' => ',' . implode(',', $roles), 'cache' => 0);
 
// Use drupal_write_record because we don't want duplicates.
 
drupal_write_record('filter_formats', $format);
 
// Save this format id.
 
$filter = $format['format'];
 
 
// Make sure it worked. If not, quit now.
 
if (!$filter) {
   
drupal_set_message(t('Oops, looks like this might be a duplicate filter?'), 'error');
    return;
  }

 
// Okay, so far, so good. Now we make a list of the filters we want to use.
  // This is array(module, delta); The weight is determined by the order listed.
 
$list = array(
    array(
'nancys_filter', 0),  // Section remover
   
array('filter', 2),  // URL filter
   
array('filter', 0),  // HTML filter
   
array('nancys_filter', 1),  // Word fixer
   
array('filter', 1),  // Line break converter
   
array('filter', 3),  // HTML corrector
   
array('pathologic', 0), // Pathologic
   
);
 
// Okie, dokie. Now all we have to do is run through that array and write it to the db.
 
foreach ($list as $weight => $r) {
   
$row = array('format' => $filter, 'module' => $r[0], 'delta' => $r[1], 'weight' => $weight);
   
drupal_write_record('filters', $row);
  }
 
 
// And finally...
  // Create a default list of HTML tags for the HTML filter to use.
 
variable_set("allowed_html_$filter", '<a> <abbr> <acronym> <address> <b> <bdo> <big> <blockquote> <br> <caption> <cite> <code> <col> <colgroup> <dd> <del> <dfn> <div> <dl> <dt> <em> <h1> <h2> <h3> <h4> <h5> <h6> <hr> <i> <ins> <kbd> <li> <ol> <p> <pre> <q> <samp> <small> <span> <strong> <sub> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <tt> <ul> <var>'); 
?>

Comments

Is this a custom module?

If so, can you provide a download link? This would benefit me greatly. Thanks for your hard work

Regards
Sean

Yes and no

It is part of a custom module that my customer needed, but it is not a contributed module. You can either put this into your own module or create a PHP-page to put it in.

Thanks, I'm just about to do

Thanks, I'm just about to do this myself. If you're interested, take a look at how the core php module does the same thing:
http://api.drupal.org/api/function/php_install/6

It looks like he php module doesn't implement hook_uninstall.

The difference is

The code that defines the filter is very similar, but I go into build the whole input format, that's where the difference is.

Hook_uninstall is not mandatory; however, it should be there in this case. Why don't you open a core issue requesting one?

php_install creates the input

php_install creates the input format as well. It does the same thing as your example.

I was trying to point out that all of this code could go in a hook_install implemenation, and, in that case, should also implement hook_uninstall.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Programmers, Site administrators

Reference

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here