Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Is there a way to call a JavaScript function declared on another file en Drupal?

I want to create a custom module, to do consume an API, and I need to create a JavaScript function to return API data, and call this JavaScript function from another JavaScript file, in another custom module.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sure ! Here an example :

In the first module, name test1 in my_function javascript file :

(function ($, Drupal, drupalSettings) {

'use strict';

Drupal.behaviors.test1_my_functions = Drupal.behaviors.test1_my_functions || {};
Drupal.behaviors.test1_my_functions = {

  attach: function (context) {

  },

  targetFunction: function() {
    console.log('Use it !');
  }
}

})(jQuery, Drupal, drupalSettings);

Then in the module test2 in example javascript file :

(function ($, Drupal, drupalSettings) {

'use strict';

 Drupal.behaviors.test2_example = Drupal.behaviors.test2_example || {};
 Drupal.behaviors.test2_example = {

  attach: function (context) {
    Drupal.behaviors.test1_my_functions.targetFunction();
  }

}

})(jQuery, Drupal, drupalSettings);

Don't forget to define the dependency in info.yml for module "test2" with "test1" in "test2.info.yml" and the dependency in javascript library "test2.libraries.yml"

example:
 js:
  js/example.js: {}
 dependencies:
  - core/jquery
  - core/jquery.once
  - core/drupal
  - core/drupalSettings
  - test1/my_functions
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!