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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Marty2081 created an issue. See original summary.

Marty2081’s picture

Marty2081’s picture

Status: Active » Needs review
Marty2081’s picture

Issue summary: View changes
vimalgoradiya’s picture

But 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.

poker10’s picture

@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.

The last submitted patch, 6: 2669254-6_test-only.patch, failed testing. View results

mcdruid’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +RTBM

Patch 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:

      'enabled' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Enabled flag (1 = Enabled, 0 = Disabled).',
      ),

https://git.drupalcode.org/project/drupal/-/blob/7.91/modules/locale/loc...

SQLite doesn't seem to care either way :)

  • poker10 committed 6fbeaa6 on 7.x
    Issue #2669254 by Marty2081, poker10: PDOException when calling...
poker10’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -RTBM

Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.