Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Everything is in the title: Wysiwyg assumes that #access on format means there is only one filter available, this is wrong and breaks format default value in hidden field.
This code:
// Use a hidden element for a single text format.
if (!$format_field['format']['#access']) {
$format_field['wysiwyg'] = array(
'#type' => 'hidden',
'#name' => $format_field['format']['#name'],
'#value' => $format_id,
'#attributes' => array(
'id' => $format_field['format']['#id'],
'class' => array('wysiwyg'),
),
);
Is both conceptually and alrorithmically wrong.
- Conceptually because #access to false is also valid when another module wants to force a format to be used
- Algorithmically because you should reuse the current #value or #default_value instead of the latest $format_id, which comes from a random loop and does not respect the current value.
What happens here is that this algorithm takes the element with the higher weight, erase the previsously set value and set this one instead.
Comment | File | Size | Author |
---|---|---|---|
#1 | 2210523-wysiwyg-wrong-format-value.patch | 546 bytes | pounard |
Comments
Comment #1
pounardHere is a patch supposedly working with the 7.x-2.2 version (manual diff, sorry).
Comment #2
TwoDYou're looking at old code, this was changed in the 7.x-2.x branch (and is thus in the 7.x-2.x-dev snapshots).
Wysiwyg now does
$format_id = $format_field['format']['#value'];
(and makes sure #access was explicitly set) just before adding the hidden field.We don't use the #default_value because a module would have to set #format to change the format to be used for a text_format field, which once it reaches the #pre_render callback has already been moved into #value (and #default_value, but since #value exists, it's ignored).
Comment #3
pounardOh right, my bad, I was sure I looked at the right code using drupalcode's gitweb, but I think now I must have clicked the wrong branch. Thank you for the detailed explaination, this is a non issue then.
Comment #4
TwoDNo problem, it happens. :)