If I use the following code in template.php

$options = array(
       'css' => array($type = 'theme', $media = 'all', $preprocess = TRUE),
       'js' => array($type = 'theme', $scope = 'header', $defer = FALSE, $cache = TRUE, $preprocess = TRUE),
     );

drupal_add_js_library('scrolltotop', '1', $options);

I get following warnings

    * warning: Illegal offset type in isset or empty in includes\common.inc on line 2127.
    * warning: Illegal offset type in includes\common.inc on line 2128.
    * warning: Illegal offset type in includes\common.inc on line 2140.
    * warning: Illegal offset type in isset or empty in includes\common.inc on line 2127.
    * warning: Illegal offset type in includes\common.inc on line 2128.
    * warning: Illegal offset type in includes\common.inc on line 2140.
    * warning: Illegal offset type in includes\common.inc on line 1794.

but if I use the following code everything works as expected

drupal_add_js_library('scrolltotop', '1', $options);

which means options are passed with default values

what does it seem to be the problem ?

Comments

skilip’s picture

Sorry for really, really late responding. Still experiencing this issue?

skilip’s picture

Status: Active » Closed (fixed)
Matt-H’s picture

Status: Closed (fixed) » Active

I have run into this problem as well. Fortunately, I have a fix (see the bottom of my post).

Let's say your in your library, your component files look like this:

Array
(
    [js] => Array
        (
            [foo.js] => sites/all/libraries/my_library/foo.js
        )

    [css] => Array
        (
            [bar.css] => sites/all/libraries/my_library/bar.css
        )

)

(which is what $files arrray looks like in drupal_add_library() after the file types are looped through)

And let's use the $options in the above example:

$options = array(
       'css' => array($type = 'theme', $media = 'all', $preprocess = TRUE),
       'js' => array($type = 'theme', $scope = 'header', $defer = FALSE, $cache = TRUE, $preprocess = TRUE),
     );

In drupal_add_library(), the paths from the $files array get merged with the $options, and the resulting arrays look like this:

Array
(
    [0] => sites/all/libraries/my_library/foo.js
    [css] => Array
        (
            [0] => theme
            [1] => all
            [2] => 1
        )
    [js] => Array
        (
            [0] => theme
            [1] => header
            [2] => 
            [3] => 1
            [4] => 1
        )
)

Array
(
    [0] => sites/all/libraries/my_library/bar.css
    [css] => Array
        (
            [0] => theme
            [1] => all
            [2] => 1
        )
    [js] => Array
        (
            [0] => theme
            [1] => header
            [2] => 
            [3] => 1
            [4] => 1
        )
)

It is merging the path with the entire $options array, causing the drupal_add_js and drupal_add_css to fail when that array is passed to them. (That's why the defaults work and passing an $options does not).

Instead, the arrays need to look like this:

Array
(
    [0] => sites/all/libraries/my_library/foo.js
    [1] => theme
    [2] => header
    [3] => 
    [4] => 1
    [5] => 1
)

Array
(
    [0] => sites/all/libraries/my_library/bar.css
    [1] => theme
    [2] => all
    [3] => 1
)

The drupal_add_library() needs modifying, changing how it is merges the whole $options with the $path.

lines 278-282

    foreach ($files[$key] as $path) {
      $args[0] = $path;
      $args = array_merge($args, $options);
      call_user_func_array("drupal_add_$key", $args);
    }

This should be:

    foreach ($files[$key] as $path) {
      $args = array(); // This resets the $args variable, so values don't keep getting merged over and over
      $args[0] = $path;
      $args = array_merge($args, $options[$key]);  // We don't want the js part of the array for the css array or vice-verse
      call_user_func_array("drupal_add_$key", $args);
    }

I hope this helps!

Matt-H’s picture

Title: warnings when using options with drupal_add_js_library in template.php » Problems when using options with drupal_add_library
Version: 6.x-2.0-beta1 » 6.x-2.3
Category: support » bug
Status: Active » Needs review
StatusFileSize
new425 bytes

I am adding a patch that should fix the problem with submitting $options into drupal_add_library().

Edit: Oops. I have a problem with the names in the patch. My apologies. I will upload a fixed patch.

Matt-H’s picture

StatusFileSize
new423 bytes

Here is a fixed and tested version of the patch. (The old one may be removed.) Please review at your convenience.

skilip’s picture

Status: Needs review » Reviewed & tested by the community

Hi Matt-H, Thanks for your patch! It looks rather straightforward, so marking this RTBC so it'll get included in the next release. Thanks again!

skilip’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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