You are here

PLUGIN.txt in Freelinking 6.3

PLUGINS FOR FREELINKING VER. 3
==============================

Freelinking ver. 3 uses a plugin system to enhance the freelinking
syntax and enable different kinds of links. Plugins can be
simple URL constructors, or can use more sophisticated methods and
have complete control over the link and target text. The output of
plugins can also be customized through various hooks and theme
functions for better integration with other modules and site design.

Having a small set of generally useful "easy-linking" plugins bundled
with the project is only the first step Freelinking takes to help
authors create links.  Freelinking also has a framework that help
developers quickly create new "easy-linking" plugins for custom
websites.

Plugins can be created in two different ways. The recommended method
is to create a new module, as a submodule of freelinking.  However,
you may also create a plugin as an "include file".


SUBMODULE
---------

By packaging your plugin as a module, you may distribute it through
Drupal.Org, make use of other modules with proper tracking of
requirements, and make use of any Drupal hook functions without
worrying about collissions with Freelinking's own behavior.

If you publish your submodule on Drupal.Org, post a message in the
Freelinking Issue Queue: http://drupal.org/project/issues/freelinking
naming your project so it can be linked to from Freelinking's project
page.

A submodule can create a Freelinking plugin by implementing
hook_freelinking() to return the definition of your plugin as an
element of an array, like so:

  function my_module_freelinking() {
    $freelinking['myplugin'] => array(
      'indicator' => '/myplugin/',
      'translate' => array(' ' => '_'),
      'replacement' => 'http://example.com/node/%1',
    );
    return $freelinking;
  }


INCLUDE FILE
------------

To create a simple freelinking plugin, just drop a ".inc" file in the
plugins/ directory under Freelinking. By convention, these should be
named "freelinking_MYPLUGIN.inc", where "MYPLUGIN" is the name of the
plugin.

Each file should at least define one indicator, where the long version
of the indicator is identical to the name of the plugin.  However, it
is possible to define multiple plugin indicators per file to denote
slightly changed semantics.  See for example freelinking_wiki.inc for
an example of this.

The only difference in structure between an include file and a
submodule is the lack of a hook_freelinking() function wrapper around
the freelinking array that defines your plugin. In an include file,
the example above would simple be:

    $freelinking['myplugin'] => array(
      'indicator' => '/myplugin/',
      'translate' => array(' ' => '_'),
      'replacement' => 'http://example.com/node/%1',
    );

This functionality primarily exists for the ease of packaging default
plugins with the Freelinking module, and to allow developers to add
custom plugins to their site without going to the trouble of wrapping
it in a module.

However, if you create a format you think others may want to use,
please create a new issue ("Contributed plugin ...") and upload the
plugin code to the project's issue queue. It will be included as a
plugin with the next release if it passes community review.


THE $freelinking ARRAY
----------------------

Your plugin's element in the $freelinking array is named after your
plugin. In the example above of a plugin called "myplugin," the
element your plugin would add to the array would be
$freelinking['myplugin']. Your element defines an array, with the
following elements:

* 'indicator' [REQUIRED]
  The indicator is a string that defines a regular expression that
  will be used to differentiate this freelink from other freelink
  types. For our example, a good indicator value might be
  '/myplugin/'. This means that freelinks for this plugin will look
  like `[[myplugin:something]]`.  Freelinking uses the colon (":") as
  the separator between the indicator and the link text. It should not
  be part of the indicator string.

* 'translate' [OPTIONAL]
  This is an array of characters that can be used to translate
  characters in the link text to other characters that will be used in
  the URL. The primary use case for this is to translate spaces into
  underscores or dashes as different systems require. In the example
  of our "mike" plugin, the website uses dashes instead of spaces, so
  we'll use a value for 'translate' of `array(' ' => '-')`. For
  reference, this array will be run through the PHP function
  [strtr()](http://http://us.php.net/manual/en/function.strtr.php).

* 'replacement' [REQUIRED (unless 'callback' exists)]
  For simple URL replacement freelinks, the 'replacement' is a string
  for the URL, where the special string '%1' gets replaced by the link
  text in the freelink. For our example, if we're going to
  http://example.com/mike/something, we'd use the replacement string
  "http://example.com/mike/%1" and our freelink text would be put in
  place of the %1. If you are using a URL replacement style of
  freelink, the 'replacement' string is *required*.

* 'callback' [REQUIRED (unless 'replacement' exists)]
  More complex plugins can use a callback element in their array to
  define a php function that will be used to come up with the
  link. This function will be passed the entire match array of the
  freelink as $target, with the target portion of the freelink in
  `$target[1]`. This function is expected to return an html fragment
  (most likely a link, but it wouldn't have to be a link). If your
  plugin is not using 'replacement,' then the 'callback' element is
  *required.*

* 'settings' [OPTIONAL]
  The specified callback function for Freelinking settings. The
  settings function should return an array of Form API elements under
  the index of the plugin name.

* 'html' [OPTIONAL]
  If set explicitly to FALSE, the plugin will not accept HTML as link
  text.

* 'tip' [OPTIONAL]
  A short (1-2 sentence) description of the plugin. It should be run
  through t() for localization as demonstrated below. This is used in
  the extended filter tips, and the default plugin's description is
  added to the short tips.

* 'weight' [OPTIONAL]
  Specify the weight to determine sort order. Otherwise counts as "0".

* 'enabled' [OPTIONAL]
  If set to FALSE, the indicator for the plugin will be ignored. As
  such, syntax in the text will not activate the plugin. (Though
  failover will still be able to.)

* 'failover' [OPTIONAL]
  If this is set to an array, a select form will be built so the site
  administrator can determine a preferred failover action. This does
  nothing unless the developer follows up with
  variable_get('freelinking_pluginname_failover', 'none') in the
  callback code. If you set it to a single value, that value will be
  displayed in a disabled textfield in the configuration of the
  plugin.

So, a simple freelinking plugin only needs to include this
array. Here's an entire plugin one might see in a very simple .inc
file to link to a wiki page and some specific site:

  $freelinking['myplugin'] => array(
    'indicator' => '/my(plugin)/',
    'translate' => array(' ' => '-'),
    'replacement' => 'http://example.org/wiki/%1',
    'tip' => t('Link to a wiki page at example.org.'),
  );


MORE COMPLEX PLUGINS
--------------------

The 'callback' element of your $freelinking['plugin'] array can define
a PHP function that will be used to create the link. This function
will get the target value for the link, and is expected to return a
link.  Freelinking will make the substition based on the return value
of this function.

By default, the target value passed to the callback function looks like so:

  Array(
    'target' => <target>,
    'dest' => <target>,
  );

However, if the text uses pipes ("|"), it will interpret that somewhat
further. This is the expanded usage:

  Array(
    'target' => <target match>,
    'dest' => <destination>,
    'text' => <anchor link text>,
    'tooltip' => <tool tip (title attribute)>,
    'other' => array(<miscellaneous arguments>),
  );

The syntax is:

    [[indicator:target|title|tooltip|arg1|arg2|...]]

Two plugins that ship with freelinking use the callback, which you can
use as examples.

* freelinking_dev.inc
  This plugin turns freelinks like [[drupalorg:17570]] into links to
  nodes at "Drupal.org" by nid. This plugin uses the callback function
  to do a `drupal_http_request` to find the title of the page.
  Therefore, this plugin can be used as an example to manipulate the
  link text. This plugin also demonstrates a settings callback in
  which you can toggle whether this http_request is submitted.

* freelinking_nodetitle.inc
  This plugin mimics the behavior of previous versions of freelinking.
  It attempts to find a node on the system with the same title as the
  target, and either creates a link to that content, or a link to a node
  creation form to create that content. This plugin can be used as an
  example to manipulate the link target.


Failover
--------

Your plugin callback may specify a "failover" or fallback action in
the event they choke. Quick example: The specified node title cannot
be found in the database, so a link to create a node with that title
is put in place.

Plugins specify failover by returning array('plugin' => 'failover
plugin'). The failover plugin is then triggered to take over trying to
process the target. You can pass a modified target in this way by just
adding 'target' => $target to the return array. You may also specify
array('error' => 'error message') to provide an array message.

You can use variable_get('freelinking_pluginname_failover', 'none') to
get the preferred failover for your plugin. This is based on the
failover element in the plugin definition or on creating a form
element by that name in your settings.


Settings
--------

If your plugin will require some settings, they can be defined in a
"freelinking_<plugin>_settings" function in your include file, or
explicitly using the 'settings' element of the plugin's freelinking
array (necessary for modules). This function should return a Drupal
FormAPI array of the various settings your plugin will need. The
freelinking module will add these form controls to the settings page
(admin/settings/freelinking). Your plugin can use these settings in
the $freelinking array, or the callback function, as necessary.

A simple example of how to use settings is part of the wiki plugins
(freelinking_wiki.inc). It uses one setting to control the language
code that the URL to Wikipedia should use. The setting is used in the
$freelinking['wikipedia']['replacement'] element, using the language
code as part of the URL.

The freelinking_nodetitle.inc plugin also uses the settings
array. This plugin has settings to control what happens when a link
cannot be found, and is able to restrict the lookup of content to
certain content types.  This is an example of using settings within
the callback function.


THE FREELINKING API
-------------------

The Freelinking API allows you to customize freelinking plugins you
create specfically for your site.


hook_freelink_alter()
---------------------

By implementing this function, you can adjust the elements of the link
array before it is rendered into an HTML link. For example:

  function my_module_freelink_alter(&$link, $target, $plugin_name, $plugin) {
    static $count;
    $link[2]['attributes']['name'] = 'freelink-' . $count++;
  }

This function will alter every link created by freelinking to insert
the number of links found in the filtered text as an anchor. You might
also make a more targeted alteration:

  function my_module_freelink_alter(&$link, $target, $plugin_name, $plugin) {
    if ($plugin_name == 'google') {
      $link[2]['attributes']['title'] .= ' Isn't Google nifty?';
    }
  }

For documentation on the structure of the $link array here, read up on
on the API entry for the l() function:
http://api.drupal.org/api/search/6/l


Theme functions
---------------

theme('freelink', $plugin, $link);

This is the function that renders the $link array we "altered" above
into HTML. You can override this, like any theme function.

theme('freelink_error', $plugin, $message);

This function renders an error message provided by a plugin for
display in the page.

File

PLUGIN.txt
View source
  1. PLUGINS FOR FREELINKING VER. 3
  2. ==============================
  3. Freelinking ver. 3 uses a plugin system to enhance the freelinking
  4. syntax and enable different kinds of links. Plugins can be
  5. simple URL constructors, or can use more sophisticated methods and
  6. have complete control over the link and target text. The output of
  7. plugins can also be customized through various hooks and theme
  8. functions for better integration with other modules and site design.
  9. Having a small set of generally useful "easy-linking" plugins bundled
  10. with the project is only the first step Freelinking takes to help
  11. authors create links. Freelinking also has a framework that help
  12. developers quickly create new "easy-linking" plugins for custom
  13. websites.
  14. Plugins can be created in two different ways. The recommended method
  15. is to create a new module, as a submodule of freelinking. However,
  16. you may also create a plugin as an "include file".
  17. SUBMODULE
  18. ---------
  19. By packaging your plugin as a module, you may distribute it through
  20. Drupal.Org, make use of other modules with proper tracking of
  21. requirements, and make use of any Drupal hook functions without
  22. worrying about collissions with Freelinking's own behavior.
  23. If you publish your submodule on Drupal.Org, post a message in the
  24. Freelinking Issue Queue: http://drupal.org/project/issues/freelinking
  25. naming your project so it can be linked to from Freelinking's project
  26. page.
  27. A submodule can create a Freelinking plugin by implementing
  28. hook_freelinking() to return the definition of your plugin as an
  29. element of an array, like so:
  30. function my_module_freelinking() {
  31. $freelinking['myplugin'] => array(
  32. 'indicator' => '/myplugin/',
  33. 'translate' => array(' ' => '_'),
  34. 'replacement' => 'http://example.com/node/%1',
  35. );
  36. return $freelinking;
  37. }
  38. INCLUDE FILE
  39. ------------
  40. To create a simple freelinking plugin, just drop a ".inc" file in the
  41. plugins/ directory under Freelinking. By convention, these should be
  42. named "freelinking_MYPLUGIN.inc", where "MYPLUGIN" is the name of the
  43. plugin.
  44. Each file should at least define one indicator, where the long version
  45. of the indicator is identical to the name of the plugin. However, it
  46. is possible to define multiple plugin indicators per file to denote
  47. slightly changed semantics. See for example freelinking_wiki.inc for
  48. an example of this.
  49. The only difference in structure between an include file and a
  50. submodule is the lack of a hook_freelinking() function wrapper around
  51. the freelinking array that defines your plugin. In an include file,
  52. the example above would simple be:
  53. $freelinking['myplugin'] => array(
  54. 'indicator' => '/myplugin/',
  55. 'translate' => array(' ' => '_'),
  56. 'replacement' => 'http://example.com/node/%1',
  57. );
  58. This functionality primarily exists for the ease of packaging default
  59. plugins with the Freelinking module, and to allow developers to add
  60. custom plugins to their site without going to the trouble of wrapping
  61. it in a module.
  62. However, if you create a format you think others may want to use,
  63. please create a new issue ("Contributed plugin ...") and upload the
  64. plugin code to the project's issue queue. It will be included as a
  65. plugin with the next release if it passes community review.
  66. THE $freelinking ARRAY
  67. ----------------------
  68. Your plugin's element in the $freelinking array is named after your
  69. plugin. In the example above of a plugin called "myplugin," the
  70. element your plugin would add to the array would be
  71. $freelinking['myplugin']. Your element defines an array, with the
  72. following elements:
  73. * 'indicator' [REQUIRED]
  74. The indicator is a string that defines a regular expression that
  75. will be used to differentiate this freelink from other freelink
  76. types. For our example, a good indicator value might be
  77. '/myplugin/'. This means that freelinks for this plugin will look
  78. like `[[myplugin:something]]`. Freelinking uses the colon (":") as
  79. the separator between the indicator and the link text. It should not
  80. be part of the indicator string.
  81. * 'translate' [OPTIONAL]
  82. This is an array of characters that can be used to translate
  83. characters in the link text to other characters that will be used in
  84. the URL. The primary use case for this is to translate spaces into
  85. underscores or dashes as different systems require. In the example
  86. of our "mike" plugin, the website uses dashes instead of spaces, so
  87. we'll use a value for 'translate' of `array(' ' => '-')`. For
  88. reference, this array will be run through the PHP function
  89. [strtr()](http://http://us.php.net/manual/en/function.strtr.php).
  90. * 'replacement' [REQUIRED (unless 'callback' exists)]
  91. For simple URL replacement freelinks, the 'replacement' is a string
  92. for the URL, where the special string '%1' gets replaced by the link
  93. text in the freelink. For our example, if we're going to
  94. http://example.com/mike/something, we'd use the replacement string
  95. "http://example.com/mike/%1" and our freelink text would be put in
  96. place of the %1. If you are using a URL replacement style of
  97. freelink, the 'replacement' string is *required*.
  98. * 'callback' [REQUIRED (unless 'replacement' exists)]
  99. More complex plugins can use a callback element in their array to
  100. define a php function that will be used to come up with the
  101. link. This function will be passed the entire match array of the
  102. freelink as $target, with the target portion of the freelink in
  103. `$target[1]`. This function is expected to return an html fragment
  104. (most likely a link, but it wouldn't have to be a link). If your
  105. plugin is not using 'replacement,' then the 'callback' element is
  106. *required.*
  107. * 'settings' [OPTIONAL]
  108. The specified callback function for Freelinking settings. The
  109. settings function should return an array of Form API elements under
  110. the index of the plugin name.
  111. * 'html' [OPTIONAL]
  112. If set explicitly to FALSE, the plugin will not accept HTML as link
  113. text.
  114. * 'tip' [OPTIONAL]
  115. A short (1-2 sentence) description of the plugin. It should be run
  116. through t() for localization as demonstrated below. This is used in
  117. the extended filter tips, and the default plugin's description is
  118. added to the short tips.
  119. * 'weight' [OPTIONAL]
  120. Specify the weight to determine sort order. Otherwise counts as "0".
  121. * 'enabled' [OPTIONAL]
  122. If set to FALSE, the indicator for the plugin will be ignored. As
  123. such, syntax in the text will not activate the plugin. (Though
  124. failover will still be able to.)
  125. * 'failover' [OPTIONAL]
  126. If this is set to an array, a select form will be built so the site
  127. administrator can determine a preferred failover action. This does
  128. nothing unless the developer follows up with
  129. variable_get('freelinking_pluginname_failover', 'none') in the
  130. callback code. If you set it to a single value, that value will be
  131. displayed in a disabled textfield in the configuration of the
  132. plugin.
  133. So, a simple freelinking plugin only needs to include this
  134. array. Here's an entire plugin one might see in a very simple .inc
  135. file to link to a wiki page and some specific site:
  136. $freelinking['myplugin'] => array(
  137. 'indicator' => '/my(plugin)/',
  138. 'translate' => array(' ' => '-'),
  139. 'replacement' => 'http://example.org/wiki/%1',
  140. 'tip' => t('Link to a wiki page at example.org.'),
  141. );
  142. MORE COMPLEX PLUGINS
  143. --------------------
  144. The 'callback' element of your $freelinking['plugin'] array can define
  145. a PHP function that will be used to create the link. This function
  146. will get the target value for the link, and is expected to return a
  147. link. Freelinking will make the substition based on the return value
  148. of this function.
  149. By default, the target value passed to the callback function looks like so:
  150. Array(
  151. 'target' => ,
  152. 'dest' => ,
  153. );
  154. However, if the text uses pipes ("|"), it will interpret that somewhat
  155. further. This is the expanded usage:
  156. Array(
  157. 'target' => ,
  158. 'dest' => ,
  159. 'text' => ,
  160. 'tooltip' => ,
  161. 'other' => array(),
  162. );
  163. The syntax is:
  164. [[indicator:target|title|tooltip|arg1|arg2|...]]
  165. Two plugins that ship with freelinking use the callback, which you can
  166. use as examples.
  167. * freelinking_dev.inc
  168. This plugin turns freelinks like [[drupalorg:17570]] into links to
  169. nodes at "Drupal.org" by nid. This plugin uses the callback function
  170. to do a `drupal_http_request` to find the title of the page.
  171. Therefore, this plugin can be used as an example to manipulate the
  172. link text. This plugin also demonstrates a settings callback in
  173. which you can toggle whether this http_request is submitted.
  174. * freelinking_nodetitle.inc
  175. This plugin mimics the behavior of previous versions of freelinking.
  176. It attempts to find a node on the system with the same title as the
  177. target, and either creates a link to that content, or a link to a node
  178. creation form to create that content. This plugin can be used as an
  179. example to manipulate the link target.
  180. Failover
  181. --------
  182. Your plugin callback may specify a "failover" or fallback action in
  183. the event they choke. Quick example: The specified node title cannot
  184. be found in the database, so a link to create a node with that title
  185. is put in place.
  186. Plugins specify failover by returning array('plugin' => 'failover
  187. plugin'). The failover plugin is then triggered to take over trying to
  188. process the target. You can pass a modified target in this way by just
  189. adding 'target' => $target to the return array. You may also specify
  190. array('error' => 'error message') to provide an array message.
  191. You can use variable_get('freelinking_pluginname_failover', 'none') to
  192. get the preferred failover for your plugin. This is based on the
  193. failover element in the plugin definition or on creating a form
  194. element by that name in your settings.
  195. Settings
  196. --------
  197. If your plugin will require some settings, they can be defined in a
  198. "freelinking__settings" function in your include file, or
  199. explicitly using the 'settings' element of the plugin's freelinking
  200. array (necessary for modules). This function should return a Drupal
  201. FormAPI array of the various settings your plugin will need. The
  202. freelinking module will add these form controls to the settings page
  203. (admin/settings/freelinking). Your plugin can use these settings in
  204. the $freelinking array, or the callback function, as necessary.
  205. A simple example of how to use settings is part of the wiki plugins
  206. (freelinking_wiki.inc). It uses one setting to control the language
  207. code that the URL to Wikipedia should use. The setting is used in the
  208. $freelinking['wikipedia']['replacement'] element, using the language
  209. code as part of the URL.
  210. The freelinking_nodetitle.inc plugin also uses the settings
  211. array. This plugin has settings to control what happens when a link
  212. cannot be found, and is able to restrict the lookup of content to
  213. certain content types. This is an example of using settings within
  214. the callback function.
  215. THE FREELINKING API
  216. -------------------
  217. The Freelinking API allows you to customize freelinking plugins you
  218. create specfically for your site.
  219. hook_freelink_alter()
  220. ---------------------
  221. By implementing this function, you can adjust the elements of the link
  222. array before it is rendered into an HTML link. For example:
  223. function my_module_freelink_alter(&$link, $target, $plugin_name, $plugin) {
  224. static $count;
  225. $link[2]['attributes']['name'] = 'freelink-' . $count++;
  226. }
  227. This function will alter every link created by freelinking to insert
  228. the number of links found in the filtered text as an anchor. You might
  229. also make a more targeted alteration:
  230. function my_module_freelink_alter(&$link, $target, $plugin_name, $plugin) {
  231. if ($plugin_name == 'google') {
  232. $link[2]['attributes']['title'] .= ' Isn't Google nifty?';
  233. }
  234. }
  235. For documentation on the structure of the $link array here, read up on
  236. on the API entry for the l() function:
  237. http://api.drupal.org/api/search/6/l
  238. Theme functions
  239. ---------------
  240. theme('freelink', $plugin, $link);
  241. This is the function that renders the $link array we "altered" above
  242. into HTML. You can override this, like any theme function.
  243. theme('freelink_error', $plugin, $message);
  244. This function renders an error message provided by a plugin for
  245. display in the page.