Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda