Skip to main content
added example code for an implementation of hook_page_build()
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

drupal_add_library() works only with libraries defined withfrom hook_library() implementations. Since the d3.js module implements hooks used from the Libraries API module, you need to use the functions this module make available.

See Using Libraries API 2.x (as a module-developer) or Using Libraries API 1.x (as a module-developer), basing on which version of the Libraries API module you are using.

Normally, with the 2.x branch of the Libraries API module, you just call libraries_load('d3') which will call drupal_add_js(), or drupal_add_css(), or both, depending on what the library contains. Otherwise, if you need to check when a library has not been loaded, you use code similar to the following one.

if (($library = libraries_load($name)) && !empty($library['loaded'])) {
  // The library has been loaded.
}

Even better, using a render array, you just need to use code similar to the following one, which is for the case the library needs to be attached to a form element.

$form['myelement']['#attached']['libraries_load'][] = array('d3');

For an hook_page_build() implementation, adding the library at the page bottom requires code similar to the following one.

function mymodule_page_build(&$page) {
    $page['page_bottom']['d3_library']['#attached']['libraries_load'][] = array('d3');
  }
}

drupal_add_library() works only with libraries defined with hook_library() implementations. Since the d3.js module implements hooks used from the Libraries API module, you need to use the functions this module make available.

See Using Libraries API 2.x (as a module-developer) or Using Libraries API 1.x (as a module-developer), basing on which version of the Libraries API module you are using.

Normally, with the 2.x branch of the Libraries API module, you just call libraries_load('d3') which will call drupal_add_js(), or drupal_add_css(), or both, depending on what the library contains. Otherwise, if you need to check when a library has not been loaded, you use code similar to the following one.

if (($library = libraries_load($name)) && !empty($library['loaded'])) {
  // The library has been loaded.
}

Even better, using a render array, you just need to use code similar to the following one, which is for the case the library needs to be attached to a form element.

$form['myelement']['#attached']['libraries_load'][] = array('d3');

drupal_add_library() works only with libraries defined from hook_library() implementations. Since the d3.js module implements hooks used from the Libraries API module, you need to use the functions this module make available.

See Using Libraries API 2.x (as a module-developer) or Using Libraries API 1.x (as a module-developer), basing on which version of the Libraries API module you are using.

Normally, with the 2.x branch of the Libraries API module, you just call libraries_load('d3') which will call drupal_add_js(), or drupal_add_css(), or both, depending on what the library contains. Otherwise, if you need to check when a library has not been loaded, you use code similar to the following one.

if (($library = libraries_load($name)) && !empty($library['loaded'])) {
  // The library has been loaded.
}

Even better, using a render array, you just need to use code similar to the following one, which is for the case the library needs to be attached to a form element.

$form['myelement']['#attached']['libraries_load'][] = array('d3');

For an hook_page_build() implementation, adding the library at the page bottom requires code similar to the following one.

function mymodule_page_build(&$page) {
    $page['page_bottom']['d3_library']['#attached']['libraries_load'][] = array('d3');
  }
}
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

drupal_add_library() works only with libraries defined with hook_library() implementations. Since the d3.js module implements hooks used from the Libraries API module, you need to use the functions this module make available.

See Using Libraries API 2.x (as a module-developer) or Using Libraries API 1.x (as a module-developer), basing on which version of the Libraries API module you are using.

Normally, with the 2.x branch of the Libraries API module, you just call libraries_load('d3') which will call drupal_add_js(), or drupal_add_css(), or both, depending on what the library contains. Otherwise, if you need to check when a library has not been loaded, you use code similar to the following one.

if (($library = libraries_load($name)) && !empty($library['loaded'])) {
  // The library has been loaded.
}

Even better, using a render array, you just need to use code similar to the following one, which is for the case the library needs to be attached to a form element.

$form['myelement']['#attached']['libraries_load'][] = array('d3');