Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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);
Comment | File | Size | Author |
---|---|---|---|
#13 | filter_formats_load_fully_populate-1304930-13.patch | 1.74 KB | joshf |
Comments
Comment #1
franzThis 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.
Comment #2
Andy Inman CreditAttribution: Andy Inman commentedLikewise, 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:
Comment #3
franzAdding keyword
Comment #4
TravisCarden CreditAttribution: TravisCarden commentedRe-tagging.
Comment #5
frobThe solution in comment #2 looks like a good one to me.
Comment #6
Andy Inman CreditAttribution: Andy Inman commentedTo cater for possible future needs, perhaps better:
Where setting $options['fully_populate'] = TRUE would cause the filter to be fully populated.
Comment #7
franzOk, 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.
Comment #8
Andy Inman CreditAttribution: Andy Inman commentedFranz, your work looks good to me - sorry but I have too much going on right now to contribute here.
Comment #9
tim.plunkettThat function was removed from D8
Comment #10
mgiffordComment #11
joshf CreditAttribution: joshf at Third and Grove commentedReroll of #7. Thanks for the patch @franz!
Comment #12
joshf CreditAttribution: joshf at Third and Grove commentedRemoved "fully-populated" from the @return documentation.
Comment #13
joshf CreditAttribution: joshf at Third and Grove commentedFixes up some errors and makes it a little easier to read.