Introduce a new hook_filter_info to allow modules to declare input filters they provide. The input filters are declared using an associative array (hook_menu-alike registry structure).
Preparation, processing and configuration are invoked using the callbacks defined instead of calling hook_filter($op, ...).

Example:

function filter_filter_info() {
  $filters[0] = array(
    'name' => t('Limit allowed HTML tags'),
    'description' => t('Allows you to restrict the HTML tags the user can use. It will also remove harmful content such as...'),
    'process callback' => '_filter_html',
    'settings callback' => '_filter_html_settings',
    'tips callback'  => '_filter_html_tips'
  );
  $filters[1] = array(
    'name' => t('Convert line breaks'),
    'description' => t('Converts line breaks into HTML (i.e. <br> and <p>) tags.'),
    'process callback' => '_filter_autop',
    'tips callback' => '_filter_autop_tips'
  );
  $filters[2] = array(
    'name' => t('Convert URLs into links'),
    'description' => t('Turns web and e-mail addresses into clickable links.'),
    'process callback' => '_filter_url',
    'settings callback' => '_filter_url_settings',
    'tips callback' => '_filter_url_tips'
  );
  $filters[3] = array(
    'name' =>  t('Correct broken HTML'),
    'description' => t('Corrects faulty and chopped off HTML in postings.'),
    'process callback' => '_filter_htmlcorrector',
  );
  $filters[4] = array(
    'name' => t('Escape all HTML'),
    'description' => t('Escapes all HTML tags, so they will be visible instead of being effective.'),
    'process callback' => '_filter_html_escape',
    'tips callback' => '_filter_html_escape_tips'
  );
  return $filters;
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch failed testing.

dropcube’s picture

Status: Needs work » Needs review
FileSize
11.64 KB

Fixed tests that failed. Oh, seems to be a lot of confusion in filter.test, the term filter and format are being used indistinctly.

Dries’s picture

Could you provide a motivation for this patch? That helps us put it in context as well as understand why this is 'critical'. Thanks dropcube. :)

dropcube’s picture

Dries, this issue comes from an old and ambitious one: #258939: Filter system revamp.

Motivations:

- Cleaning up the filter API to make it into an "info" hook, consistently with others info hooks in core (hook_menu, hook_theme, etc..)
- Benefits of registry-style info hooks is that they do NOT get re-called every time, which is a performance win and means less unused code loaded in every page request.
- If decided, filters definitions could be altered: hook_filter_info_alter(), so that this hook behaves like other "info" hooks.
- Code in nested switch for $op and $delta results is hard to maintain and debug.

Also see #546350: Remove hardcoded numeric deltas from hook_filter_info().

catch’s picture

subscribing. Looks good on a first pass, but don't have time to look in-depth at all now.

Status: Needs review » Needs work

The last submitted patch failed testing.

dropcube’s picture

Status: Needs work » Needs review
FileSize
12.97 KB

Include implementation of php_filter_info(), to pass the tests.

Dries’s picture

Status: Needs review » Reviewed & tested by the community

This looks like a nice clean-up, and one I support. Filters are used a _lot_ so I wonder if there is any performance regressions due to this patch. I doesn't look like there would be one. I think this is RTBC, really.

Berdir’s picture

Status: Reviewed & tested by the community » Needs work

Looks nice, however, you need to update hook_filter() in filter.api.php too....

alexanderpas’s picture

subscribe

dropcube’s picture

Status: Needs work » Needs review
FileSize
21.57 KB

This patch updates filter.api.php as pointed by Berdir. However, the documentation can be improved in follow-up issues, as there are other issues pending, for example #546350: Remove hardcoded numeric deltas from hook_filter_info()

Dries’s picture

Priority: Critical » Normal
Status: Needs review » Needs work

Looks great. Committed to CVS HEAD.

Marking this as 'code needs work' because we need to update the upgrade instructions and follow-up on the remaining issues.

jhodgdon’s picture

Issue tags: +Needs documentation

The upgrade doc definitely needs some work, see also #548308: Remove hook_filter_tips().

There's currently a section in the update guide that says the parameters for hook_filter() have changed - http://drupal.org/update/modules/6/7#hook_filter_params -- but hook_filter() no longer exists by that name at all, so that section needs to be removed or rewritten.

So it looks to me as though what needs to be documented is:
- hook_filter changed its name to hook_filter_info() (I think? not sure which issue that is)
- new parameter for hook_filter_info()
- No more hook_filter_tips()

Not all of that is related to this issue, but it all needs to be fixed, and possibly there are other things? I'm not sure. If someone could make a coherent list of the changes, that would be a good start.

dropcube’s picture

webchick’s picture

Issue tags: +Needs documentation

Adding the tag back, pending jhodgdon's review.

jhodgdon’s picture

Assigned: dropcube » jhodgdon

This does still need some work. I will take care of it. In particular, the old upgrade guide section on hook_filter() needs to be removed or updated. Not sure of other problems, as I haven't reviewed in detal yet.

sun’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
13.17 KB

I really should not be doing this...

Great, now let's fix the introduced coding-style issues and improper documentation.

Apparently, this patch is not guilty for all changes, but it seems like the other patch that changed the structure of hook_filter_info() is not tagged with FilterSystemRevamp.

sun’s picture

Of course, I didn't recognize the introduced code in the update function introduced by #546350: Remove hardcoded numeric deltas from hook_filter_info().

Status: Reviewed & tested by the community » Needs work

The last submitted patch failed testing.

dropcube’s picture

Status: Needs work » Needs review
FileSize
13.23 KB

This patch fixes some coding-style issues and improves/fixes the documentation of hook_filter_info().

sun’s picture

Status: Needs review » Reviewed & tested by the community
Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

jhodgdon’s picture

Status: Fixed » Needs work

Doc still needs work here, as per comment #16 above. Assigned to me, I'll take care of it.

jhodgdon’s picture

Status: Needs work » Fixed

Taken care of now.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

naught101’s picture

Status: Closed (fixed) » Active

Re-opening for a documentation question: What is $op="no cache" replaced with with the new hook_filter_info? Also, a module that I'm trying to upgrade has a

switch ($op) { 
  default: return $text;
}

Is that now completely irrelevant?

Also, it would be good to link the "Converting 6.x modules to 7.x" documentation back to this issue, for reference and context.

jhodgdon’s picture

Status: Active » Fixed

From http://api.drupal.org/api/function/hook_filter_info/7 it looks like the return value has a 'cache' element.

I have just added that to the upgrade guide.

I guess for your default op, you should see if you need to make a prepare/process function?

naught101’s picture

Thanks, there's already a process function, that was just shorthand. Anyway, I'll see how it goes, the default option probably wasn't previously used anyway...

Status: Fixed » Closed (fixed)
Issue tags: -Needs documentation, -FilterSystemRevamp

Automatically closed -- issue fixed for 2 weeks with no activity.