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.
I'm not sure if this is a bug (at least it's not documented this way), but #default_value
isn't working with type 'text_format'
. I changed to #value and now the next is properly shown in the text area.
Code that is used in admin settings form:
Broken:
$site_map_message = variable_get('site_map_message', array('value' => '', 'format' => NULL));
$form['site_map_message'] = array(
'#type' => 'text_format',
'#format' => isset($site_map_message['format']) ? $site_map_message['format'] : NULL,
'#title' => t('Site map message'),
'#default_value' => $site_map_message['value'],
'#description' => t('Define a message to be displayed above the site map.'),
);
Works:
$site_map_message = variable_get('site_map_message', array('value' => '', 'format' => NULL));
$form['site_map_message'] = array(
'#type' => 'text_format',
'#format' => isset($site_map_message['format']) ? $site_map_message['format'] : NULL,
'#title' => t('Site map message'),
'#value' => $site_map_message['value'],
'#description' => t('Define a message to be displayed above the site map.'),
);
Comment | File | Size | Author |
---|---|---|---|
#22 | core_fix_autoload_bug.patch | 1.12 KB | hass |
#5 | FAPI_text_format_bug1.png | 52.6 KB | hass |
Comments
Comment #1
hass CreditAttribution: hass commentedDamn it... it's not working with
#value
, too :-(. The textarea is populated with text saved in the database, but does not save it if #value is used and the text have changed.Comment #2
hass CreditAttribution: hass commentedThe current site_map D7 code has been committed to HEAD of http://drupal.org/project/site_map - if an easy repro case is required.
Comment #3
sun"isn't working"? What's happening and what did you expect to happen?
Comment #4
hass CreditAttribution: hass commentedI do expect that the text that is fired into default value is shown in the user interface, but it isn't.
Comment #5
hass CreditAttribution: hass commentedOnly to make sure nobody need to guess anything... here is the code that can also be downloaded from CVS (site_map module) -> HEAD.
and here is the screenshot after pressing save - with the debug output. And you can also see the "site map message" text is not shown :-(. If pressing Save again, the message is deleted (expected result).
Comment #6
hass CreditAttribution: hass commentedI have opened a new bug for the htmlspecialchars() issue at #823362: htmlspecialchars() expects parameter 1 to be string, array given in check_plain().
Comment #7
sunThat htmlspecialchars() error message usually means that you are expecting to pass a string in a variable somewhere, but the variable actually is an array. Most likely, that's also the cause for this entire issue.
Comment #8
hass CreditAttribution: hass commentedI'm not doing this... This is only core fapi. :-)
Comment #9
sunIf that would be the case, don't you think that various places in Drupal core shouldn't be totally broken right now?
http://api.drupal.org/api/function/text_field_widget_form/7
http://api.drupal.org/api/function/user_account_form/7
Make sure that
$site_map_message['value']
actually contains what it is supposed to contain.Since I infrequently ran into this error since Drupal 5 and it was always caused by false assumptions on variable contents, I'm still speculating on a won't fix here. So please make absolutely sure that your variable data is correct. Otherwise, we need more debugging information here from you, since I suspect that no one else will be able to replicate this error.
Comment #10
sunbtw, is this a system_settings_form() ? If it is, then your variable does not contain what you expect. Your value is actually located in
You need an additional form validation handler to shift the submitted form values around, prior to the submit handlers being called. #type 'text_format' is not really compatible with system_settings_form(). Perhaps we should document that. Probably a good idea, but not sure, where.
Comment #11
hass CreditAttribution: hass commentedIt sounds like you have not taken a look to the screenshots in #5 and http://drupal.org/node/823362#comment-3071368. Really annoying as it would have stopped you from wasting time. I'm sorry to say that you are simply wrong with $site_map_message['value']['value'], See krumo output in screenshot in #5. I made the debugging screenshots only for this reason.
I have also tested for verification purpose by hard coding text into the form fields and the sentence
'This is text that is not shown in the FORM field.'
is NOT shown in the UI field. See below code example:It would be much easier if you install site_map module and do a quick test. Should take only 5 minutes to figure out the new form field type is broken. If the text would be populated to the UI it would simply work. The #823362: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() nailed out later. I have not implemented a special form handler and I see no reason why I need to do it as the data is correctly saved in the variables table - only the form field is broken and something in the deep background that cause #823362: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() on form save what you can also see here:
Comment #12
hass CreditAttribution: hass commented*All* information is in the case.
Comment #13
Damien Tournoud CreditAttribution: Damien Tournoud commentedPlease read #10 and understand the new behavior of
system_settings_form()
when$automatic_defaults
is true (which is the default).Comment #14
hass CreditAttribution: hass commented@Damien: Have you tried to repro? No idea why you change to support. Changing
system_settings_form($form)
tosystem_settings_form($form, FALSE)
does not change anything. The form element need to work whatever need to be documented (or not), this form element is the only one that does not work like any other. This could be bad DX, it could be bad design, but it's not the way how it's expected to work and not the way how it seems to work. As said - all data is correctly saved in the variables table, but the populated static text fired into '#default_value' is not shown anywhere. Should I really expect this if all other form api fields work this way? I do not think so and I'm sorry I do not understand sun's comment why I need to shift anything around. All data is saved where it should be, but '#default_value' on form load do not work. An answer why - would be more than great.Comment #15
Damien Tournoud CreditAttribution: Damien Tournoud commentedTrying to explain. Please follow carefully.
When
$automatic_defaults
is true, system_settings_form() uses the #name of the element as the variable name and the #default_value of the element as the default value of the variable, and automatically calls variable_get() for you.This code assume that the automatic defaults loading is not enabled:
When automatic defaults loading is enabled, this will result in a call to
variable_get('site_map_message', $site_map_message['value'])
, which will result in the #default_value being populated with an array. This is certainly not what you want.You want either to disable the automatic defaults loading. Contrary to what you are saying, this will make it work, but you also need to populate the #format key.
Comment #16
hass CreditAttribution: hass commentedNot sure why this haven't worked while testing myslef. But now - it works, maybe the cache clear solved it. THX.
But now we are at the point we must clearly say - this is a bug! The http://api.drupal.org/api/function/_system_settings_form_automatic_defau... need to do something like if '#type' == 'text_format' read '#default_value' from variables table in $value['value'] and '#format' from $value['format']. It could be easy with is_array($value).
Than the new $automatic_defaults would work like a charm - also we the new 'text_format' form filed.
What do you think?
Comment #17
hass CreditAttribution: hass commentedOtherwise or additionally I strongly suggest to change the doc in http://api.drupal.org/api/function/system_settings_form/7 by adding some more detailed text that explains the new $automatic_defaults feature with a few more words. I'm sorry for having this missed as all other stuff worked as before nevertheless I do have '#default_value' with variable_get() calls in every form field array.
Comment #18
hass CreditAttribution: hass commentedComment #19
hass CreditAttribution: hass commentedI'm assigning this to me and hope to get a patch ready that fixes above bugs. My plan is to get the following solved:
1. If '#default_value' is defined in the form array there is no default loaded in _system_settings_form_automatic_defaults(). We expect that a developer does something at it own. It will not ->
// If the property (key) '#default_value' exists, replace it.
as this will duplicate variable_get() calls.2. Only load the value from variable table if there is no '#default_value' defined.
3. Make
'#type' == 'text_format'
work with system_settings_form() with $automatic_defaults.4. Write better documentation for system_settings_form()'s $automatic_defaults.
Comment #20
sunComment #21
Damien Tournoud CreditAttribution: Damien Tournoud commentedThis automatic defaults loading is basically a hack. I passionately hates it, but it is difficult to do better because current FAPI doesn't have a #element_submit option.
Comment #22
hass CreditAttribution: hass commentedOk, after looking more on this code I only fixed #3. See the attached patch, please.
I think the rest could also been solved by writing an update doc. I suggest something like the below, please review. If you know - where to add it on http://drupal.org/update/modules/6/7 it would be great.
system_settings_form() automatically populates form values
D6:
D7 with autoloading of form value defaults:
D7 with custom form value loading it's like it was in D6, but requires
FALSE
as second parameter insystem_settings_form()
:Comment #23
Damien Tournoud CreditAttribution: Damien Tournoud commentedPlease see #266246: Add smart defaults for system_settings_form.
Comment #24
sunHowever, we should seriously consider to allow #type 'text_format' to accept an *array* as #default_value now. I.e., 'value' + 'format'.
That, to some extent, could also lead to the rollback of the rollback of automated settings, although I wouldn't necessarily support that.
#type 'date' similarly expands an array as #default_value into new-born child elements via #process. So, that would add some consistency of some sort.
Opinions?
Comment #25
slefevre1 CreditAttribution: slefevre1 commentedWhich issue is this a duplicate of? I'm running into this now and it seems to be a conflict with the current dev branch of the media module.
Comment #26
Anybody@slefevre1 and all others running into this issue:
See #1761944: Using text_format field in settings forms : htmlspecialchars() expects parameter 1 to be string, the workaround in a system_settings_form seems to be to set
'#base_type' => 'textarea',
and use
... still I think sun is right in#24 and this is still an issue after 7 years.