From 9ca51f4c9ae058888cdf527579ca5ad7e2ed3ba6 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Wed, 1 Apr 2020 13:57:53 -0400 Subject: [PATCH] Serve client module JS files through Drupal. --- modules/farm/farm_client/farm_client.info | 1 + modules/farm/farm_client/farm_client.module | 57 +++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/modules/farm/farm_client/farm_client.info b/modules/farm/farm_client/farm_client.info index ea29a31bb..1fafbd31a 100644 --- a/modules/farm/farm_client/farm_client.info +++ b/modules/farm/farm_client/farm_client.info @@ -2,4 +2,5 @@ name = Farm Client description = Integrates the farmOS server and client. core = 7.x package = farmOS +dependencies[] = farm_access dependencies[] = farm_api diff --git a/modules/farm/farm_client/farm_client.module b/modules/farm/farm_client/farm_client.module index 8efccb85a..891a4aa36 100644 --- a/modules/farm/farm_client/farm_client.module +++ b/modules/farm/farm_client/farm_client.module @@ -20,6 +20,24 @@ function farm_client_farm_api_oauth2_client() { return $clients; } +/** + * Implements hook_menu(). + */ +function farm_client_menu() { + $items = array(); + + // Serve client JavaScript files. + $items['farm/client/js/%/index.js'] = array( + 'title' => 'Deliver client module JS', + 'page callback' => 'farm_client_js_deliver', + 'page arguments' => array(3), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + + return $items; +} + /** * Implements hook_farm_info(). */ @@ -28,6 +46,13 @@ function farm_client_farm_info() { // Ask modules for client module info. $client_modules = module_invoke_all('farm_client_module_info'); + // Replace the JavaScript file path with the one that is served by Drupal. + foreach ($client_modules as $name => &$client_module) { + if (!empty($client_module['js'])) { + $client_module['js'] = 'farm/client/js/' . $name . '/index.js'; + } + } + // Add the client module info to /farm.json. $info = array(); if (!empty($client_modules)) { @@ -37,3 +62,35 @@ function farm_client_farm_info() { } return $info; } + +/** + * Page callback for delivering client JS. + * + * This will load the JavaScript file for a client module and output the content + * as if it were being loaded directly from the web server. This allows the + * response to include receive the Access-Control-Allow-Origin and other headers + * added by farm_access_init(). + * + * @param string $module + * The machine name of the client module. + * + * @return NULL|int + * This function will print the contents of the JavaScript file, if found, and + * will not return anything. Otherwise, it will return MENU_NOT_FOUND. + */ +function farm_client_js_deliver($module) { + + // Ask modules for client module info. + $client_modules = module_invoke_all('farm_client_module_info'); + + // If the requested module doesn't exist, or if it doesn't provide JS, bail + // with a 404. + if (!(array_key_exists($module, $client_modules) && !empty($client_modules[$module]['js']) && file_exists($client_modules[$module]['js']))) { + return MENU_NOT_FOUND; + } + + // Print the contents of the Javascript file and exit. + drupal_add_http_header('Content-Type', 'application/javascript; utf-8'); + print file_get_contents($client_modules[$module]['js']); + drupal_exit(); +}