danielebarchiesi@0: There are two primary pieces to using plugins. The first is getting the data, and the second is using the data. danielebarchiesi@0: danielebarchiesi@0:
danielebarchiesi@0: /** danielebarchiesi@0: * Implements hook_ctools_plugin_type() to inform CTools about the layout plugin. danielebarchiesi@0: */ danielebarchiesi@0: function panels_ctools_plugin_type() { danielebarchiesi@0: $plugins['layouts'] = array( danielebarchiesi@0: 'load themes' => TRUE, danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: return $plugins; danielebarchiesi@0: } danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0: The following information can be specified for each plugin type: danielebarchiesi@0:
danielebarchiesi@0: ctools_include('plugins'); danielebarchiesi@0: ctools_get_plugins($module, $type, [$id]) danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0: In the above example, $module should be your module's name and $type is the type of the plugin. It is typically best practice to provide some kind of wrapper function to make this easier. For example, Panels provides the following functions to implement the 'content_types' plugin: danielebarchiesi@0: danielebarchiesi@0:
danielebarchiesi@0: /** danielebarchiesi@0: * Fetch metadata on a specific content_type plugin. danielebarchiesi@0: * danielebarchiesi@0: * @param $content type danielebarchiesi@0: * Name of a panel content type. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array with information about the requested panel content type. danielebarchiesi@0: */ danielebarchiesi@0: function panels_get_content_type($content_type) { danielebarchiesi@0: ctools_include('context'); danielebarchiesi@0: ctools_include('plugins'); danielebarchiesi@0: return ctools_get_plugins('panels', 'content_types', $content_type); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Fetch metadata for all content_type plugins. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array of arrays with information about all available panel content types. danielebarchiesi@0: */ danielebarchiesi@0: function panels_get_content_types() { danielebarchiesi@0: ctools_include('context'); danielebarchiesi@0: ctools_include('plugins'); danielebarchiesi@0: return ctools_get_plugins('panels', 'content_types'); danielebarchiesi@0: } danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0:
Any of the above items can be overridden by the plugin itself, though the most likely one to be modified is the 'path'.
danielebarchiesi@0: danielebarchiesi@0:The most likely data (beyond simple printable data) for a plugin to provide is a callback. The plugin system provides a pair of functions to make it easy and consistent for these callbacks to be used. The first is ctools_plugin_get_function, which requires the full $plugin object.
danielebarchiesi@0: danielebarchiesi@0:danielebarchiesi@0: /** danielebarchiesi@0: * Get a function from a plugin, if it exists. If the plugin is not already danielebarchiesi@0: * loaded, try ctools_plugin_load_function() instead. danielebarchiesi@0: * danielebarchiesi@0: * @param $plugin danielebarchiesi@0: * The loaded plugin type. danielebarchiesi@0: * @param $callback_name danielebarchiesi@0: * The identifier of the function. For example, 'settings form'. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The actual name of the function to call, or NULL if the function danielebarchiesi@0: * does not exist. danielebarchiesi@0: */ danielebarchiesi@0: function ctools_plugin_get_function($plugin, $callback_name) danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0:
The second does not require the full $plugin object, and will load it:
danielebarchiesi@0: danielebarchiesi@0:danielebarchiesi@0: /** danielebarchiesi@0: * Load a plugin and get a function name from it, returning success only danielebarchiesi@0: * if the function exists. danielebarchiesi@0: * danielebarchiesi@0: * @param $module danielebarchiesi@0: * The module that owns the plugin type. danielebarchiesi@0: * @param $type danielebarchiesi@0: * The type of plugin. danielebarchiesi@0: * @param $id danielebarchiesi@0: * The id of the specific plugin to load. danielebarchiesi@0: * @param $callback_name danielebarchiesi@0: * The identifier of the function. For example, 'settings form'. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The actual name of the function to call, or NULL if the function danielebarchiesi@0: * does not exist. danielebarchiesi@0: */ danielebarchiesi@0: function ctools_plugin_load_function($module, $type, $id, $callback_name) { danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0:
Both of these functions will ensure any needed files are included. In fact, it allows each callback to specify alternative include files. The plugin implementation could include code like this:
danielebarchiesi@0: danielebarchiesi@0:danielebarchiesi@0: 'callback_name' => 'actual_name_of_function_here', danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0:
Or like this:
danielebarchiesi@0:danielebarchiesi@0: 'callback_name' => array( danielebarchiesi@0: 'file' => 'filename', danielebarchiesi@0: 'path' => 'filepath', // optional, will use plugin path if absent danielebarchiesi@0: 'function' => 'actual_name_of_function_here', danielebarchiesi@0: ), danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0:
An example, for 'plugin_example' type
danielebarchiesi@0: danielebarchiesi@0:danielebarchiesi@0: $plugin = array( danielebarchiesi@0: 'name' => 'my_plugin', danielebarchiesi@0: 'module' => 'my_module', danielebarchiesi@0: 'example_callback' => array( danielebarchiesi@0: 'file' => 'my_plugin.extrafile.inc', danielebarchiesi@0: 'function' => 'my_module_my_plugin_example_callback', danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0:
To utilize this callback on this plugin:
danielebarchiesi@0: danielebarchiesi@0:danielebarchiesi@0: if ($function = ctools_plugin_get_function($plugin, 'example_callback')) { danielebarchiesi@0: $function($arg1, $arg2, $etc); danielebarchiesi@0: } danielebarchiesi@0:danielebarchiesi@0: danielebarchiesi@0:
Since the data provided by your plugin tends to be specific to your plugin type, you really need to document what the data returned in the hook in the .inc file will be or nobody will figure it out. Use advanced help and document it there. If every system that utilizes plugins does this, then plugin implementors will quickly learn to expect the documentation to be in the advanced help.