0

I'm building D3 libraries to visualize data. The visualizations require the topojson Javascript library to manipulate geo shapes.

I want to include the topojson javascript library as a dependency in my custom libraries and right now I'm simply including it in the .info file for the each library:

files[js][] = 'js/topojson.v0.min.js'

However, this adds the topojson file to <head> section of the page more than once when I have several of these includes.

I want to be able to load the file once and use it in anyone of my D3 libraries. How do I do that?

1 Answer 1

0

You can add another dependency by using custom module

function hook_library() {
  // Library One.
  $libraries['library-1'] = array(
    'title' => 'Library One',
    'website' => 'http://example.com/library-1',
    'version' => '1.2',
    'js' => array(
      drupal_get_path('module', 'my_module') . '/library-1.js' => array(),
    ),
    'css' => array(
      drupal_get_path('module', 'my_module') . '/library-2.css' => array(
        'type' => 'file',
        'media' => 'screen',
      ),
    ),
  );
  // Library Two.
  $libraries['library-2'] = array(
    'title' => 'Library Two',
    'website' => 'http://example.com/library-2',
    'version' => '3.1-beta1',
    'js' => array(
      // JavaScript settings may use the 'data' key.
      array(
        'type' => 'setting',
        'data' => array('library2' => TRUE),
      ),
    ),
    'dependencies' => array(
      // Require jQuery UI core by System module.
      array('system', 'ui'),
      // Require our other library.
      array('my_module', 'library-1'),
      // Require another library.
      array('other_module', 'library-3'),
    ),
  );
  return $libraries;
}
2
  • Thanks for the reply @Adi but is there any way I can do this through the Library API instead?
    – tsega
    Oct 6, 2016 at 11:22
  • yes you can add dependency library
    – Adi
    Oct 6, 2016 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.