Serve client module JS files through Drupal.

This commit is contained in:
Michael Stenta 2020-04-01 13:57:53 -04:00
parent 5d17f01bca
commit 9ca51f4c9a
2 changed files with 58 additions and 0 deletions

View File

@ -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

View File

@ -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();
}