Issue summary
Updating from 7.x-1.8 (possibly previous versions too, unconfirmed) triggers the following error:

Failed: PDOException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'rule_themes' at row 1: ALTER TABLE {css_injector_rule} CHANGE `rule_themes` `rule_themes` TEXT NOT NULL COMMENT 'Themes that CSS rule will be applied to'; Array ( ) in db_add_field() (line 2841 of includes/database/database.inc).

This is due to the incorrect default value (or actually, no default value) supplied for field 'rule_themes' newly introduced in 7.x-1.9.

Also, after update, site shows the following warning message:

Warning: in_array() expects parameter 2 to be array, boolean given in css_injector_init() (line 55 of sites/all/modules/css_injector/css_injector.module).

This update won't break your website, don't panic if you've already updated, see the following solution. If you haven't updated yet, stick with the current version before a fix is committed.

Proposed solution
a) If you have already updated
Manually go through every CSS Injector rule you have and check the "Themes to show on" field as desired. Then save the changes. Clearing cache after updating the rules is recommended to make sure the changes are reflected immediately.

b) For the module developer and maintainer
Either supply a:0:{} as the default value (if you want no themes to be checked initially), or a:1:{s:7:"@default_theme";s:7:"@default_theme";} if you want the current default theme to be selected by default (recommended). Note that @default_theme is a placeholder and should be replaced by the key of the default theme, which can be easily get using the $theme_key global.

Comments

vermontdevil’s picture

I have the same error.

Jeffrey C.’s picture

Ignore this.

Jeffrey C.’s picture

Title: Warning: in_array() expects parameter 2 to be array, boolean given in css_injector_init() » Provide the correct default value for field 'rule_themes'
Priority: Normal » Major
Issue summary: View changes
Jeffrey C.’s picture

Issue summary: View changes
Jeffrey C.’s picture

Issue summary: View changes
Jeffrey C.’s picture

Issue summary: View changes
Jeffrey C.’s picture

Issue summary: View changes
Jeffrey C.’s picture

Issue summary: View changes
fonant’s picture

I have this on my sites too. Editing each CSS Injector rule and selecting at least one theme for it to apply to fixes the errors.

gilsbert’s picture

Hi.

I have the very same situation and I did manual interactions to correct it.

I agree on what is suggested by @tuccio: the bug must be corrected by giving a default value for the new field "rule_themes".

The file css_injector.install should be changed to include a default value for the field.
The function to be changed is "css_injector_update_7001()".
It could be something like this:

/**
* Adds enabled rule and themes list.
*/
function css_injector_update_7001() {
$enabled = array(
'description' => 'Whether the rules should be enabled',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
);
$themes = array(
'description' => 'Themes that CSS rule will be applied to',
'type' => 'text',
'not null' => TRUE,
'default' => 'a:0:{}',
);
db_add_field( 'css_injector_rule', 'enabled', $enabled);
db_add_field( 'css_injector_rule', 'rule_themes', $themes);
}

The line in bold is "the one" that will fix the bug!

I used this version of the file with success and it avoided the PDOException reported.

Unfortunately in my tests the default value 'a:0:{}' was truncated to 'a:0' in the table.
I dont know why... perhaps the string '{}' is considered by drupal containing special characters and would need an escape character?
To fix this minor bug I had to make a manual update on the table to correct the value (update css_injector_rule set rule_themes = 'a:0:{}') . A second option to correct the value is to edit and save every single css rule.

If you edit and change the css_injector.install file as I suggested you will be able to update 1.8 to 1.9 without failures on the database update process.
After the database update remember to update the new field 'rule_themes' inside table css_injector_rule with the value 'a:0:{}' either by a manual update direct to the dtabase or by editing/saving each css rule in your site.

For those who used the original install file and got the PDOException please make a check in your site and you will note that the update 7001 for css_injector is pending. If you try to reaply it even using a version with default value for the new field "rule_themes" you will face a different error: "field already exists".

The reason for that is: the first process created the fields but did not finished correctly.

Now I can see three ways to fix this:

1) disable and uninstall css_injector and reinstall a fresh css_injector using 1.9 original scripts!
---> yes, you will loose everything so use this aproach if you can loose your css rules!

2) manually delete the new fields then reaply the database update using the script css_injector.install already corrected !
---> example code to manually delete fields (it works on postgresql and I dont garantee it will work in different databases).
alter table css_injector_rule drop column enabled;
alter table css_injector_rule drop column rule_themes;

3) wait for the maintainers give an official workaround!

Regards,
Gilsberty

iamwinner001’s picture

I also have the same problem. BTW, I looked through the revisions and would like to say thank you to Jeffrey C., who actually proposed the solutions and added all the details. We should really credit this to him.

oresh’s picture

I've uploaded a fix on the dev branch.
Can someone test if this works for both upgrading from 1.8 and having update failed from 1.9?

I'm sticking with the empty theme list as default - cause that's how it worked before the update. The update should not break stuff (unfortunately it did :[ ) so we're keeping the previous behavior.

Thanks for your updates, guys!

gilsbert’s picture

Thank you @tuccio and @Jeffrey C. for reporting the issue with good information.

Thank you maintainers for a fast answer!

I'm testing the dev version.

gilsbert’s picture

Ok.
Tests done.

1) fresh install of the dev version ==> just 1 warning.

Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 em _locale_parse_js_file() (linha 1488 de /srv/www/drupal/drupal-erika/includes/locale.inc).
Warning: file_get_contents(http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js): failed to open stream: no suitable wrapper could be found em _locale_parse_js_file() (linha 1488 de /srv/www/drupal/drupal-erika/includes/locale.inc).

---> my server doesn't allow remote files to be opened and this is a good solution for security.
---> please check if the remote file is really necessary and put it inside the module!

2) upgrade from 1.8 to dev version ==> same behavior reported in first test plus: 1 notice message and 1 warning message

Notice: Undefined index: enabled em css_injector_init() (linha 50 de /srv/www/drupal/drupal-erika/modules/css_injector/css_injector.module).

---> The notice message happens before the database update process. So I believe it is normal.

Warning: in_array() expects parameter 2 to be array, boolean given em css_injector_init() (linha 55 de /srv/www/drupal/drupal-erika/modules/css_injector/css_injector.module).

The warning message happens after the database update process.

To avoid this message without forcing a default value over the new field rule_themes we will need a better treatment inside de code. For example, test if the field is a array (php function is_array) before accessing it's value.

A second approach is to give the field a default value as I suggested in #10 or running an update after the field is created to set the value without the property "default".

Without anything this is a minor bug and can be fixed by editing and saving every single css rule in the site. Well not a charm at all!

3) update from 1.8 to 1.9 and then to dev version ==> same behavior reported in first and second tests plus: 1 error message

---> the error message is the PDO Exception already expected when we upgrade from 1.8 to 1.9

After the 1.9 to dev version update everything was working good, except by what is already informed in the second test.

Regards,
Gilsberty

tuccio’s picture

Hello, I ran into this issue just now and while this is a legitmate issue (and sorry for interfering with it), I, i.e. "tuccio", DID NOT open this thread. I am the owner of the Drupal.org "tuccio" account, and I did not open this issue.

Can the author of this thread please explain why / how he / she used my Drupal.org account to post this issue? Can anyone advise who to contact for this Drupal.org account breach or malfunctioning? Thank you.

Jeffrey C.’s picture

Hello tuccio,

I'm afraid there's a little misunderstanding here. If you look at the revisions of this issue, you can see that you actually opened this issue; however, the only information supplied is the error message, which makes it hard for the maintainer and developer to identify the issue and provide a fix as soon as possible. Drupal.org allows everyone to update an issue in order to make it more complete and able to directly address the core problems that possibly caused the described outcome. So, since I ran into the same issue, I investigated a little and went ahead and updated the issue with my findings. Your account isn't compromised, so don't worry.

tuccio’s picture

Well thank you.

However it would seem to me that while updating the title makes sense, on the other hand writing a post on someone else's behalf would not be totally appropriate and it would seem more logical to add whatever comment under your own name.

Because otherwise, you make me look a lot smarter than I actually am ;)

Jeffrey C.’s picture

Well do let me apologize for the confusion. However, I do believe it's quite common on Drupal.org as it wouldn't make much sense to open another issue to address the same problem. Instead, it's probably a better approach to put all the effort in the same basket - that way, people encountering the same issue can report their findings in one place, again, makes it easier to work a fix.

gilsbert’s picture

HI @oresh.

I made the tests (details at #14).
Do you need more information?

Regards,
Gilsberty

oresh’s picture

@gilsbert
Thanks for your tests! :)
I added some fixes to the code and created a new release. I tested having 1.8 with some rules and updating to 7.x-1.9-alpha1.
I had no errors when updating and few other tests went well.
I think in this issue we should leave a solution for those who updated from 1.8 to 1.9 first and got some errors. The 1.9-alpha1 would fix some of the errors from that update, but that needs more tests.

arne_hortell’s picture

In file
/var/www/sites/all/modules/css_injector/css_injector.module
Fix number line #50 to this
if ((array_key_exists('enabled',$css_rule)) && ($css_rule['enabled'])) {

arne_hortell’s picture

if same file row 55
insert
if ($theme_rules)

So it becomes
--------
$theme_rules = unserialize($css_rule['rule_themes']);
global $theme;
if ($theme_rules)
if (in_array($theme, $theme_rules, true) || empty($theme_rules)) {
--------

arne_hortell’s picture

Might be violent but…

in
function css_injector_update_7001()

At very end…

db_drop_field('css_injector_rule','enabled'); // added by me
db_drop_field('css_injector_rule','rule_themes'); // added by me
db_add_field( 'css_injector_rule', 'enabled', $enabled);
db_add_field( 'css_injector_rule', 'rule_themes', $themes);

gilsbert’s picture

Status: Active » Reviewed & tested by the community

Hi @oresh.

Thank you for giving us fast answers.
I did new tests.

1) Upgrade process from 1.8 to 1.10

Only one notice message happening before the database update process.
Notice: Undefined index: rule_themes em css_injector_init() (linha 53 de /srv/www/drupal/drupal-erika/modules/css_injector/css_injector.module).

After the database update process everything working perfectly.
It is not necessary edit and save every css rule: cheers!

2) from 1.8 to 1.9 then 1.9 to 1.10

2.a) from 1.8 to 1.9

Just to be complete I will report the first step but here nothing is changed as we expected!

Before database update from 1.8 to 1.9:
Notice: Undefined index: enabled em css_injector_init() (linha 50 de /srv/www/drupal/drupal-erika/modules/css_injector/css_injector.module).

After database update from 1.8 to 1.9:
PDOException: SQLSTATE[23502]: Not null violation: 7 ERRO: coluna "rule_themes" contém valores nulos: ALTER TABLE {css_injector_rule} ALTER rule_themes SET NOT NULL; Array ( ) em db_add_field() (linha 2841 de /srv/www/drupal/drupal-erika/includes/database/database.inc).
--> database update will never finish and will stay pending.

Warning: in_array() expects parameter 2 to be array, boolean given em css_injector_init() (linha 55 de /srv/www/drupal/drupal-erika/modules/css_injector/css_injector.module).
--> this message can be solved by editing/saving each css rule.

2.b) from 1.9 to 1.10

All messages will be gone before and after the database upgrade.

In my personal oppinion we can consider this issue fixed! Thats why I'm marking as RTBC.

Regards,
Gilsberty

P.S.: Please take a look on #21-#23.