I find that you cannot simply load a format with filter_format_load() and then save it again with filter_format_save(). If you do, the filter configuration (which filters the format uses) are completely lost. This is with D7.8 at least - I am not sure if it's a bug or deliberate behaviour.

Here's my work-around:

// Load the format we want to work with.
$format = filter_format_load('my_format');

// Work-around for what *seems* to be a bug somewhere in Drupal filter
// format loading/saving.  There's probably a better way, but this seems to work.
if (empty($format->filters)) {
  // Get the filters used by this format.
  $filters = filter_list_format($format->format);
  // Build the $format->filters array...
  $format->filters = array();
  foreach($filters as $name => $filter) {
    foreach($filter as $k => $v) {
      $format->filters[$name][$k] = $v;
    }
  }
}

// Do whatever processing is required before saving.
// ...

// Save the updated format.
filter_format_save($format);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

franz’s picture

Version: 7.8 » 8.x-dev
Issue tags: +Needs documentation

This is probably due to performance. filter_format_load() is used by check_markup() to just load minimal information. Then it hits the cache, and if fails, it loads the filters the way you did.

So my guess is that it's an expected behavior. But I have to agree that the documentation is not clear about it and it states a misleading "fully-populated text format object" on it.

Let's get someone else to advice here.

Andy Inman’s picture

Likewise, I can understand it being deliberate behaviour for performance reasons. Maybe a solution would be an additional parameter passed to filter_format_load() something like:

function filter_format_load($format_id, $fully_populate = FALSE) {
  ...
}
franz’s picture

Issue tags: +Needs backport to D7

Adding keyword

TravisCarden’s picture

Re-tagging.

frob’s picture

Status: Active » Needs work

The solution in comment #2 looks like a good one to me.

Andy Inman’s picture

To cater for possible future needs, perhaps better:

function filter_format_load( $format_id, $options = array() ) {
  ...
}

Where setting $options['fully_populate'] = TRUE would cause the filter to be fully populated.

franz’s picture

Status: Needs work » Needs review
FileSize
1.51 KB

Ok, this patch should do it. Do we need to make a test for it? Or does it only makes sense if we had something to consume this API? I'm thinking we could improve this so that check_markup could use.

Andy Inman’s picture

Franz, your work looks good to me - sorry but I have too much going on right now to contribute here.

tim.plunkett’s picture

Version: 8.x-dev » 7.x-dev

That function was removed from D8

mgifford’s picture

Issue summary: View changes
Status: Needs review » Needs work
joshf’s picture

Category: Bug report » Feature request
Status: Needs work » Needs review
Issue tags: -Needs backport to D7
FileSize
1.49 KB

Reroll of #7. Thanks for the patch @franz!

joshf’s picture

Removed "fully-populated" from the @return documentation.

joshf’s picture

Fixes up some errors and makes it a little easier to read.