-
Notifications
You must be signed in to change notification settings - Fork 0
Converting CTools plugins
The primary parts of CTools that is not in core are "CTools plugins". The reason we did not include these in core is that they would have been a replacement for all *_info() hooks. And rather than having 2 ways of doing the same thing, we decided to stick with _info() hooks instead of converting them all to plugins. As we only moved a few things from CTools (e.g. Views "wizards" and the handlers from Panels and Page manager), it was much easier and more consistent with core to switch these to info hooks.
So in general, if you're porting a module that used CTools plugins, it's preferable to convert it to using info hooks instead. But for cases where that appears to be difficult, the Plugin Manager is a dependency that is drop-in replacement for CTools Plugin. Read the documentation as part of that module on how to switch.
Take for example our conversion of Views wizards into Backdrop. In Drupal 7, the existence of a wizard class was done via a file placed in a special directory (views_wizard). Here are the plugins for node and user.
In Backdrop we instead made a hook, hook_views_ui_wizards()
and included the same information that had been in the plugin in it. See view_ui_views_ui_wizards()
.
Then when actually getting a list of wizards, both Backdrop and Drupal 7 have the same parent function views_ui_get_wizards()
. The only difference is how we build the list:
Backdrop views_ui_get_wizards()
:
$wizard_plugins = module_invoke_all('views_ui_wizards');
Drupal 7 views_ui_get_wizards()
:
ctools_include('plugins');
$wizard_plugins = ctools_get_plugins('views_ui', 'views_wizard');
So, in general you can avoid using a plugin by doing the following:
- Move any
$plugin = array()
definitions from CTools plugin files into a new *_info() hook of your making. - Find the place where previously your module called
ctools_get_plugins()
and change it instead to bemodule_invoke_all('your_new_info')
. - If necessary, include a 'file' value in the info hook to point at the plugin file. Then use
module_load_include()
to this file before calling its functions. In the case of Views wizards, that wasn't necessary because they are all classes and use the autoloader to load themselves.
- Define an info hook that to define everything in the
$plugin
array at the top of each ctools plugin file. Add an extra'class' => 'NAME'
line and add the class name.
See examples:
hook_metatag_info()
hook_field_validation_validator_info()
(Tip: Include a modulename.api.php
file to document any hooks.)
- Add a
modulename_get_whatever()
function to call the info hook.
Remove all instances of:
ctools_include('plugins');
Replace:
ctools_get_plugins('whatever');
with:
modulename_get_whatever()