Quiero crear un módulo personalizado, para consumir una API, y necesito crear una función JavaScript para devolver datos API y llamar a esta función JavaScript desde otro archivo JavaScript, en otro módulo personalizado.
Por supuesto ! Aquí un ejemplo:
En el primer módulo, nombre test1 en el archivo javascript my_function:
(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);Luego, en el módulo test2 en el archivo javascript de ejemplo:
(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);No olvide definir la dependencia en info.yml para el módulo "test2" con "test1" en "test2.info.yml" y la dependencia en la biblioteca javascript "test2.libraries.yml"
example: js: js/example.js: {} dependencies: - core/jquery - core/jquery.once - core/drupal - core/drupalSettings - test1/my_functions