From 18ec6180f99f17a4b70dd9af81f4add2c1e77b2f Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Fri, 6 Dec 2019 15:33:08 -0500 Subject: [PATCH] Add new hooks for defining farmOS map behaviors in Drupal, and a farm_map_add_behavior() function for adding them. --- modules/farm/farm_map/farm_map.api.php | 33 +++++++++++++++- modules/farm/farm_map/farm_map.module | 52 ++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/modules/farm/farm_map/farm_map.api.php b/modules/farm/farm_map/farm_map.api.php index 0a8badb5c..651025c32 100644 --- a/modules/farm/farm_map/farm_map.api.php +++ b/modules/farm/farm_map/farm_map.api.php @@ -21,6 +21,33 @@ * Hooks that can be implemented by other modules in order to extend farm_map. */ +/** + * Define farmOS-map behaviors provided by this module. Modules can add a + * behavior to a map with farm_map_add_behavior('mybehavior'). This will add the + * JavaScript file to the page and invoke hook_farm_map_behavior_settings() to + * add necessary Drupal JS settings to the page. + */ +function hook_farm_map_behaviors() { + return array( + 'my_behavior' => array( + 'js' => 'js/my_behavior.js', + ), + ); +} + +/** + * Return an array of settings for a given behavior. These will be added to the + * page as Drupal JS settings in: + * Drupal.settings.farm_map.behaviors.[behaviorname] + */ +function hook_farm_map_behavior_settings($behavior) { + $settings = array(); + if ($behavior == 'my_behavior') { + $settings['foo'] = 'bar'; + } + return $settings; +} + /** * Perform logic when a map is viewed. * @@ -31,8 +58,10 @@ */ function hook_farm_map_view($name, $element) { - // Add a farmOS map behavior JS file. - drupal_add_js(drupal_get_path('module', 'mymodule'), 'mymodule.mybehavior.js'); + // Add my farmOS map behavior. + if ($name == 'my_map') { + farm_map_add_behavior('my_behavior'); + } } /** diff --git a/modules/farm/farm_map/farm_map.module b/modules/farm/farm_map/farm_map.module index 7d4ff1349..0e173963a 100644 --- a/modules/farm/farm_map/farm_map.module +++ b/modules/farm/farm_map/farm_map.module @@ -11,6 +11,12 @@ include_once 'farm_map.geo.inc'; * Implements hook_hook_info(). */ function farm_map_hook_info() { + $hooks['farm_map_behaviors'] = array( + 'group' => 'farm_map', + ); + $hooks['farm_map_behavior_settings'] = array( + 'group' => 'farm_map', + ); $hooks['farm_map_view'] = array( 'group' => 'farm_map', ); @@ -216,6 +222,52 @@ function farm_map_build($map_name, $fieldset = FALSE, $title = '', $collapsed = return $build; } +/** + * Add a farmOS-map behavior to the page. + * + * @param string $behavior + * The behavior name. + * @param array $settings + * Optionally provide settings to override defaults. + */ +function farm_map_add_behavior($behavior, $settings = array()) { + + // Ask modules for behaviors. + $hook = 'farm_map_behaviors'; + $modules = module_implements($hook); + foreach ($modules as $module) { + + // Get the module's behaviors. + $behaviors = module_invoke($module, $hook); + + // If the desired behavior isn't defined, skip. + if (!(array_key_exists($behavior, $behaviors) && !empty($behaviors[$behavior]['js']))) { + continue; + } + + // If the module defines settings for the behavior, add them as JS settings. + $settings_hook = 'farm_map_behavior_settings'; + $default_settings = array(); + if (function_exists($module . '_' . $settings_hook)) { + $default_settings = module_invoke($module, $settings_hook, $behavior); + } + $settings = array_merge($default_settings, $settings); + if (!empty($settings)) { + drupal_add_js(array( + 'farm_map' => array( + 'behaviors' => array( + $behavior => $settings, + ), + ), + ), array('type' => 'setting')); + } + + // Add the behavior JS to the page. + $path = drupal_get_path('module', $module) . '/' . $behaviors[$behavior]['js']; + drupal_add_js($path); + } +} + /** * Map settings form. */