Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda