Adding a library without a version number causes 'Undefined index: version in drupal_get_library()'

Created on 31 January 2014, almost 11 years ago
Updated 10 June 2024, 7 months ago

drupal_get_library() will cause the following PHP notice if the library you are trying to get does not have an associated version number.

Notice: Undefined index: version in drupal_get_library() (line 4765 of /includes/common.inc).

<!--break-->

What are the steps required to reproduce the bug?

  1. Create a Drupal library by implementing hook_library(). Nothing that I saw in the documentation indicates that a library requires a version number.
  2. Load the library either by calling drupal_add_library() or drupal_get_library() (drupal_add_library calls drupal_get_library.)

What behavior were you expecting?

The documentation for hook_library() says that the library may contain a version. Therefore I didn't expect to run into any issues if I didn't include a version in the library I was adding.

What happened instead?

drupal_get_library() assumes that the library has a version and if it doesn't causes a PHP notice.

The drupal_get_library() function starts at line 4748 of common.inc and it includes the following lines (4764 - 4766):

        foreach ($module_libraries[$key]['js'] as $file => $options) {
          $module_libraries[$key]['js'][$file]['version'] = $module_libraries[$key]['version'];
        }

Proposed resolution

I'm proposing one of the following two solutions:

1. Add an isset() check to drupal_get_library()

I've attached a patch for this. One possible issue with this approach is that it might cause problems for downstream code that assumes the loaded library will have a version for each javascript file.

2. Change the hook_library() documentation to make version mandatory

🐛 Bug report
Status

Needs work

Version

7.0 ⚰️

Component
Asset library 

Last updated 7 days ago

No maintainer
Created by

🇨🇦Canada Dave Bagler

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024