Problem/Motivation
The documentation of the locale_add_language()
function in locale.inc
explicitly says:
Optionally TRUE to enable the language when created or FALSE to disable
However calling the function with FALSE
as the value of the $enabled
parameter results in the following error:
PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'enabled' at row 1: INSERT INTO {languages} (language, name, native, direction, domain, prefix, enabled) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => pt-pt [:db_insert_placeholder_1] => Portuguese [:db_insert_placeholder_2] => Português [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => pt-pt [:db_insert_placeholder_6] => ) in locale_add_language() (line 584 of /var/www/example/htdocs/includes/locale.inc).
Using integer 0
as the value for $enabled
does work without errors. The "enabled" column in the schema is type integer.
Steps to reproduce:
Create a function calling locale_add_language()
with FALSE
as the value for the $enabled
parameter. For example in an implementation of hook_update_N()
:
function example_update_7100() {
locale_add_language('pt-pt', NULL, 'NULL, LANGUAGE_LTR, '', '', FALSE, FALSE);
}
Proposed resolution
Since we probably do not want an API change, we can cast the value of $enabled
to an integer when writing the value to the database:
db_insert('languages')
->fields(array(
'language' => $langcode,
'name' => $name,
'native' => $native,
'direction' => $direction,
'domain' => $domain,
'prefix' => $prefix,
'enabled' => (int) $enabled,
))
->execute();
Remaining tasks
Review needed
User interface changes
None
API changes
None
Data model changes
None
Comments
Comment #2
Marty2081 CreditAttribution: Marty2081 at LimoenGroen commentedAnd here is a patch.
Comment #3
Marty2081 CreditAttribution: Marty2081 at LimoenGroen commentedComment #4
Marty2081 CreditAttribution: Marty2081 at LimoenGroen commentedComment #5
vimalgoradiya CreditAttribution: vimalgoradiya commentedBut if you manually execute insert query with INSERT INTO using value TRUE or FALSE will successfully add one row in that table. So I might think conversion thing is not required to (int). It will automatically convert in to 1 or 0 accordingly.
Comment #6
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commented@vimalgoradiya Not all DB drivers will do this conversion. For example PostgreSQL is very strict regarding such "assumptions". You will get an error
ERROR: column "xxx" is of type integer but expression is of type boolean
.Adding a test to this bug so it has better chance to get fixed. Also I think it will be better to use this expression
'enabled' => $enabled ? 1 : 0,
instead of casting'enabled' => (int) $enabled,
, just to be sure.Comment #8
mcdruidPatch looks good and great to have a test with it, thanks.
Explicitly sending 0 or 1 to the db matches the schema for the languages table:
https://git.drupalcode.org/project/drupal/-/blob/7.91/modules/locale/loc...
SQLite doesn't seem to care either way :)
Comment #10
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedThanks all!