Inconsistent behaviour in \Drupal\filter\FilterFormatInterface::setFilterConfig()

Created on 10 October 2023, about 1 year ago
Updated 13 November 2023, about 1 year ago

Problem/Motivation

Discovered while fixing 🐛 Consistently sort filter formats to simplify config exports Fixed . \Drupal\filter\FilterFormatInterface::setFilterConfig() has different effects depending on whether the config entity has had the filter plugin collection initialised.

Steps to reproduce

Install standard. Run the following PHP to change the weight to a non-standard weight:

$format = Drupal\filter\Entity\FilterFormat::load('plain_text');
$format->filters(); // Instantiate the plugin collection
$format->setFilterConfig('filter_html_escape', ['weight' => -12]);
$format->save();
dump($format->filters('filter_html_escape')->weight); // Returns -12 yay!

Then run:

$format = Drupal\filter\Entity\FilterFormat::load('plain_text');
$format->filters(); // Instantiate the plugin collection
$format->setFilterConfig('filter_html_escape', ['status' => FALSE]);
$format->save();
$format->filters('filter_html_escape')->weight; // Returns -12 yay! maybe?

Then run:

$format = Drupal\filter\Entity\FilterFormat::load('plain_text');
$format->setFilterConfig('filter_html_escape', ['status' => FALSE]);
$format->save();
$format->filters('filter_html_escape')->weight; // Returns -10 huh? why?

The only difference between the last two calls is the last one does not call $format->filters();. This is very surprising behaviour.

Proposed resolution

The problem here is that \Drupal\filter\Plugin\FilterBase::setConfiguration() does not merge in the default configuration unlike many other configurable plugins - see \Drupal\Core\Block\BlockPluginTrait::setConfiguration() for example.

To fix this behaviour we should change \Drupal\filter\Plugin\FilterBase::setConfiguration() to behave more like block's.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Filter 

Last updated 1 day ago

No maintainer
Created by

🇬🇧United Kingdom alexpott 🇪🇺🌍

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024