From 4e479ba62f68c6bc03efe6723e6de2ec44e3f2e6 Mon Sep 17 00:00:00 2001
From: Peter Philipp <peter.philipp@cando-image.com>
Date: Tue, 28 Aug 2012 23:02:36 +0200
Subject: [PATCH] Make aloha editor extensible by other modules.

---
 edit_aloha/edit_aloha.api.php |   49 +++++++++++++++++++++++++++++++++++++++++
 edit_aloha/edit_aloha.module  |   47 +++++++++++++++++++++++++++++----------
 2 files changed, 84 insertions(+), 12 deletions(-)
 create mode 100644 edit_aloha/edit_aloha.api.php

diff --git a/edit_aloha/edit_aloha.api.php b/edit_aloha/edit_aloha.api.php
new file mode 100644
index 0000000..f6f2052
--- /dev/null
+++ b/edit_aloha/edit_aloha.api.php
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * @file
+ * Hooks provided by edit_aloha.
+ */
+
+
+/**
+ * Allow modules to provide their own aloha plugins.
+ *
+ * @link http://aloha-editor.org/builds/development/alohaeditor-0.21.0-SNAPSHOT-source-20120414-001007/doc/guides/output/using_aloha.html#plugin-bundles
+ *
+ * @return array
+ *   An array of information about the module's provided aloha plugins.
+ *   The array contains a sub-array for each plugin, with the bundle name as
+ *   the key. Bundle names may only contain lowercase alpha-numeric characters
+ *   and underscores and should be prefixed with the providing module name.
+ *   Possible attributes for each sub-array are:
+ *   - path: The path to the plugin location. Required.
+ *   - plugins: An array keyed by the plugin name as key and possible plugin
+ *     settings as value.
+ */
+function edit_aloha_bundle_info() {
+  $bundles = array();
+  if (module_exists('token_filter')) {
+    $bundles['token_filter'] = array(
+      'path' => file_create_url(drupal_get_path('module', 'token_filter') . '/aloha/'),
+      'plugins' => array(
+        'token-filter' => array(
+          'macroRender' => url('admin/macro/render/token_filter'),
+          'macroList'   => url('admin/macro/list/token_filter'),
+        ),
+      ),
+    );
+  }
+  return $bundles;
+}
+
+/**
+ * Allow modules to alter the bundles provided by other modules.
+ *
+ * @param array $bundle_infos
+ *   The bundles provided by other modules.
+ *
+ * @see edit_aloha_bundle_info()
+ */
+function edit_aloha_bundle_info_alter(&$bundle_infos) {
+}
diff --git a/edit_aloha/edit_aloha.module b/edit_aloha/edit_aloha.module
index 31854c0..ede74d3 100644
--- a/edit_aloha/edit_aloha.module
+++ b/edit_aloha/edit_aloha.module
@@ -72,8 +72,18 @@ function edit_aloha_library() {
     'extra/captioned-image',
     'edit_aloha/drupal',
   );
-  if (module_exists('token_filter')) {
-    $plugins[] = 'token_filter/token-filter';
+  // Invoke hook_edit_aloha_bundle_info().
+  $bundle_infos = module_invoke_all('edit_aloha_bundle_info');
+  // Invoke hook_edit_aloha_bundle_info_alter().
+  drupal_alter('edit_aloha_bundle_info', $bundle_infos);
+  $bundle_plugins = array();
+  $bundle_plugins_settings = array();
+  foreach ($bundle_infos as $name => $bundle_info) {
+    $bundle_plugins[$name] = $bundle_info['path'];
+    foreach ($bundle_info['plugins'] as $plugin => $settings) {
+      $plugins[] = $name . '/' . $plugin;
+      $bundle_plugins_settings[$plugin] = $settings;
+    }
   }
 
   $plugin_path = drupal_get_path('module', 'edit_aloha') . '/aloha/drupal-ui';
@@ -82,10 +92,8 @@ function edit_aloha_library() {
     // Let AE/RequireJS know where edit_aloha's plug-in bundle lives.
     'bundles' => array(
       'edit_aloha' => file_create_url(drupal_get_path('module', 'edit') . '/edit_aloha/aloha/'),
-      // @TODO: make sure this doesn't need to remain hardcoded.
-      'token_filter' => file_create_url(drupal_get_path('module', 'token_filter') . '/aloha/')
-    ),
-    // Overide parts of AE's default UI, so that we can have a custom UI with
+    ) + $bundle_plugins,
+    // Override parts of AE's default UI, so that we can have a custom UI with
     // minimal effort.
     'requireConfig' => array(
       'paths' => array(
@@ -95,7 +103,7 @@ function edit_aloha_library() {
         'ui/toolbar'      => file_create_url($plugin_path . '/lib/toolbar'),
         'ui/tab'      => file_create_url($plugin_path . '/lib/tab'),
         'ui/menuButton' => file_create_url($plugin_path . '/lib/menuButton'),
-      )
+      ),
     ),
     // Reduce delays for the "aloha-smart-content-changed" events to a minimum.
     'smartContentChange' => array(
@@ -116,10 +124,6 @@ function edit_aloha_library() {
         'removeFormat',
       )),
       'list' => array('config' => array('ul', 'ol')),
-      'token-filter' => array(
-        'macroRender' => url('admin/macro/render/token_filter'),
-        'macroList'   => url('admin/macro/list/token_filter'),
-      ),
       'captionedImage' => array(
         'allowLineBreak'      => FALSE,
         'selector'            => 'img',
@@ -128,7 +132,7 @@ function edit_aloha_library() {
         'blockClass'          => 'edit-captioned-image-block',
         // 'render' => function() {}, // function callback, we can't set that here.
       )
-    ),
+    ) + $bundle_plugins_settings,
     'contentHandler' => array(
       'insertHtml' => array('word', 'generic', 'drupal'),
       'handler' => array(
@@ -250,3 +254,22 @@ function edit_aloha_library() {
 
   return $libraries;
 }
+
+/**
+ * Implements hook_edit_aloha_bundle_info().
+ */
+function edit_aloha_edit_aloha_bundle_info() {
+  $bundles = array();
+  if (module_exists('token_filter')) {
+    $bundles['token_filter'] = array(
+      'path' => file_create_url(drupal_get_path('module', 'token_filter') . '/aloha/'),
+      'plugins' => array(
+        'token-filter' => array(
+          'macroRender' => url('admin/macro/render/token_filter'),
+          'macroList'   => url('admin/macro/list/token_filter'),
+        ),
+      ),
+    );
+  }
+  return $bundles;
+}
-- 
1.7.10.msysgit.1

