Projet

Général

Profil

Paste
Télécharger (10,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / help / plugins-creating.html @ 7e72b748

1
There are two primary pieces to using plugins. The first is getting the data, and the second is using the data.
2

    
3
<h2>Defining a plugin</h2>
4
To define that you offer a plugin that modules can implement, you first must implement hook_ctools_plugin_type() to tell the plugin system about your plugin.
5

    
6
<pre>
7
/**
8
 * Implements hook_ctools_plugin_type() to inform CTools about the layout plugin.
9
 */
10
function panels_ctools_plugin_type() {
11
  $plugins['layouts'] = array(
12
    'load themes' => TRUE,
13
  );
14

    
15
  return $plugins;
16
}
17
</pre>
18

    
19
The following information can be specified for each plugin type:
20
<dl>
21
<dt>cache</dt>
22
<dd><em>Defaults to:</em> <strong>FALSE</strong></dd>
23
<dd>If set to TRUE, the results of ctools_get_plugins will be cached in the 'cache' table (by default), thus preventing .inc files from being loaded. ctools_get_plugins looking for a specific plugin will always load the appropriate .inc file.</dd>
24
<dt>cache table</dt>
25
<dd><em>Defaults to:</em> <strong>'cache'</strong></dd>
26
<dd>If 'cache' is TRUE, then this value specifies the cache table where the cached plugin information will be stored.</dd>
27
<dt>classes</dt>
28
<dd><em>Defaults to:</em> <strong>array()</strong></dd>
29
<dd>An array of <em>class identifiers</em>(i.e. plugin array keys) which a plugin of this type uses to provide classes to the CTools autoloader. For example, if <strong>classes</strong> is set to array('class'), then CTools will search each <strong>$plugin['class']</strong> for a class to autoload. Depending of the plugin structure, a <em>class identifier</em> may be either:
30
<dl>
31
<dt>- a file name:</dt>
32
<dd>the file which holds the class with the name structure as: <em>[filename].[class].php</em></dd>
33
<dd>in this case the class name can be different than the <em>class identifier</em></dd>
34
<dt>- the class name:</dt>
35
<dd>if the class is in the same file as the $plugin</dd>
36
<dd>the plugin <em>.inc</em> file can have a different name than the <em>class identifier</em></dd>
37
</dl>
38
<dd>
39
<dt>defaults</dt>
40
<dd><em>Defaults to:</em> <strong>array()</strong></dd>
41
<dd>An array of defaults that should be added to each plugin; this can be used to ensure that every plugin has the basic data necessary. These defaults will not ovewrite data supplied by the plugin. This could also be a function name, in which case the callback will be used to provide defaults. NOTE, however, that the callback-based approach is deprecated as it is redundant with the 'process' callback, and as such will be removed in later versions. Consequently, you should only use the array form for maximum cross-version compatibility.</dd>
42
<dt>load themes</dt>
43
<dd><em>Defaults to:</em> <strong>FALSE</strong></dd>
44
<dd>If set to TRUE, then plugins of this type can be supplied by themes as well as modules. If this is the case, all themes that are currently enabled will provide a plugin: NOTE: Due to a slight UI bug in Drupal, it is possible for the default theme to be active but not enabled. If this is the case, that theme will NOT provide plugins, so if you are using this feature, be sure to document that issue. Also, themes set via $custom_theme do not necessarily need to be enabled, but the system has no way of knowing what those themes are, so the enabled flag is the only true method of identifying which themes can provide layouts.</dd>
45
<dt>hook</dt>
46
<dd><em>Defaults to:</em> (dynamic value)</dd>
47
<dd>The name of the hook used to collect data for this plugin. Normally this is <strong>$module . '_' . $type</strong> -- but this can be changed here. If you change this, you MUST be sure to document this for your plugin implementors as it will change the format of the specially named hook.
48
<dt>process</dt>
49
<dd><em>Defaults to:</em> <strong>''</strong></dd>
50
<dd>An optional function callback to use for processing a plugin. This can be used to provide automated settings that must be calculated per-plugin instance (i.e., it is not enough to simply append an array via 'defaults'). The parameters on this callback are: <strong>callback(&$plugin, $info)</strong> where $plugin is a reference to the plugin as processed and $info is the fully processed result of hook_ctools_plugin_api_info().
51
<dt>extension</dt>
52
<dd><em>Defaults to:</em> <strong>'inc'</strong></dd>
53
<dd>Can be used to change the extension on files containing plugins of this type. By default the extension will be "inc", though it will default to "info" if "info files" is set to true. Do not include the dot in the extension if changing it, that will be added automatically.</dd>
54
<dt>info file</dt>
55
<dd><em>Defaults to:</em> <strong>FALSE</strong></dd>
56
<dd>If set to TRUE, then the plugin will look for a .info file instead of a .inc. Internally, this will look exactly the same, though obviously a .info file cannot contain functions. This can be good for styles that may not need to contain code.</dd>
57
<dt>use hooks</dt>
58
<dd><em>Defaults to:</em> <strong>TRUE</strong>*</dd>
59
<dd>Use to enable support for plugin definition hooks instead of plugin definition files. NOTE: using a central plugin definition hook is less optimal for the plugins system, and as such this will default to FALSE in later versions.</dd>
60
<dt>child plugins</dt>
61
<dd><em>Defaults to:</em> <strong>FALSE</strong></dd>
62
<dd>If set to TRUE, the plugin type can automatically have 'child plugins' meaning each plugin can actually provide multiple plugins. This is mostly used for plugins that store some of their information in the database, such as views, blocks or exportable custom versions of plugins.</dd>
63
<dd>To implement, each plugin can have a 'get child' and 'get children' callback. Both of these should be implemented for performance reasons, since it is best to avoid getting all children if necessary, but if 'get child' is not implemented, it will fall back to 'get children' if it has to.</dd>
64
<dd>Child plugins should be named parent:child, with the : being the separator, so that it knows which parent plugin to ask for the child. The 'get children' method should at least return the parent plugin as part of the list, unless it wants the parent plugin itself to not be a choosable option, which is not unheard of. </dd>
65
<dd>'get children' arguments are ($plugin, $parent) and 'get child' arguments are ($plugin, $parent, $child).
66
</dl>
67

    
68
In addition, there is a 'module' and 'type' settings; these are for internal use of the plugin system and you should not change these.
69
<h2>Getting the data</h2>
70
To create a plugin, a module only has to execute ctools_get_plugins with the right data:
71

    
72
<pre>
73
  ctools_include('plugins');
74
  ctools_get_plugins($module, $type, [$id])
75
</pre>
76

    
77
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:
78

    
79
<pre>
80
/**
81
 * Fetch metadata on a specific content_type plugin.
82
 *
83
 * @param $content type
84
 *   Name of a panel content type.
85
 *
86
 * @return
87
 *   An array with information about the requested panel content type.
88
 */
89
function panels_get_content_type($content_type) {
90
  ctools_include('context');
91
  ctools_include('plugins');
92
  return ctools_get_plugins('panels', 'content_types', $content_type);
93
}
94

    
95
/**
96
 * Fetch metadata for all content_type plugins.
97
 *
98
 * @return
99
 *   An array of arrays with information about all available panel content types.
100
 */
101
function panels_get_content_types() {
102
  ctools_include('context');
103
  ctools_include('plugins');
104
  return ctools_get_plugins('panels', 'content_types');
105
}
106
</pre>
107

    
108
<h2>Using the data</h2>
109

    
110
Each plugin returns a packet of data, which is added to with a few defaults. Each plugin is guaranteed to always have the following data:
111
<dl>
112
<dt>name</dt>
113
<dd>The name of the plugin. This is also the key in the array, of the full list of plugins, and is placed here since that is not always available.</dd>
114
<dt>module</dt>
115
<dd>The module that supplied the plugin.</dd>
116
<dt>file</dt>
117
<dd>The actual file containing the plugin.</dd>
118
<dt>path</dt>
119
<dd>The path to the file containing the plugin. This is useful for using secondary files, such as templates, css files, images, etc, that may come with a plugin.</dd>
120
</dl>
121

    
122
<p>Any of the above items can be overridden by the plugin itself, though the most likely one to be modified is the 'path'.</p>
123

    
124
<p>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.</p>
125

    
126
<pre>
127
/**
128
 * Get a function from a plugin, if it exists. If the plugin is not already
129
 * loaded, try ctools_plugin_load_function() instead.
130
 *
131
 * @param $plugin
132
 *   The loaded plugin type.
133
 * @param $callback_name
134
 *   The identifier of the function. For example, 'settings form'.
135
 *
136
 * @return
137
 *   The actual name of the function to call, or NULL if the function
138
 *   does not exist.
139
 */
140
function ctools_plugin_get_function($plugin, $callback_name)
141
</pre>
142

    
143
<p>The second does not require the full $plugin object, and will load it:</p>
144

    
145
<pre>
146
/**
147
 * Load a plugin and get a function name from it, returning success only
148
 * if the function exists.
149
 *
150
 * @param $module
151
 *   The module that owns the plugin type.
152
 * @param $type
153
 *   The type of plugin.
154
 * @param $id
155
 *   The id of the specific plugin to load.
156
 * @param $callback_name
157
 *   The identifier of the function. For example, 'settings form'.
158
 *
159
 * @return
160
 *   The actual name of the function to call, or NULL if the function
161
 *   does not exist.
162
 */
163
function ctools_plugin_load_function($module, $type, $id, $callback_name) {
164
</pre>
165

    
166
<p>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:</p>
167

    
168
<pre>
169
  'callback_name' => 'actual_name_of_function_here',
170
</pre>
171

    
172
<p>Or like this:</p>
173
<pre>
174
  'callback_name' => array(
175
    'file' => 'filename',
176
    'path' => 'filepath', // optional, will use plugin path if absent
177
    'function' => 'actual_name_of_function_here',
178
  ),
179
</pre>
180

    
181
<p>An example, for 'plugin_example' type</p>
182

    
183
<pre>
184
$plugin = array(
185
  'name' => 'my_plugin',
186
  'module' => 'my_module',
187
  'example_callback' => array(
188
    'file' => 'my_plugin.extrafile.inc',
189
    'function' => 'my_module_my_plugin_example_callback',
190
  ),
191
);
192
</pre>
193

    
194
<p>To utilize this callback on this plugin:</p>
195

    
196
<pre>
197
if ($function = ctools_plugin_get_function($plugin, 'example_callback')) {
198
  $function($arg1, $arg2, $etc);
199
}
200
</pre>
201

    
202
<h2>Document your plugins!</h2>
203

    
204
<p>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.</p>