plugins.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Contains routines to organize and load plugins. It allows a special
  6. * variation of the hook system so that plugins can be kept in separate
  7. * .inc files, and can be either loaded all at once or loaded only when
  8. * necessary.
  9. */
  10. /**
  11. * Get an array of information about modules that support an API.
  12. *
  13. * This will ask each module if they support the given API, and if they do
  14. * it will return an array of information about the modules that do.
  15. *
  16. * This function invokes hook_ctools_api. This invocation is statically
  17. * cached, so feel free to call it as often per page run as you like, it
  18. * will cost very little.
  19. *
  20. * This function can be used as an alternative to module_implements and can
  21. * thus be used to find a precise list of modules that not only support
  22. * a given hook (aka 'api') but also restrict to only modules that use
  23. * the given version. This will allow multiple modules moving at different
  24. * paces to still be able to work together and, in the event of a mismatch,
  25. * either fall back to older behaviors or simply cease loading, which is
  26. * still better than a crash.
  27. *
  28. * @param $owner
  29. * The name of the module that controls the API.
  30. * @param $api
  31. * The name of the api. The api name forms the file name:
  32. * $module.$api.inc
  33. * @param $minimum_version
  34. * The lowest version API that is compatible with this one. If a module
  35. * reports its API as older than this, its files will not be loaded. This
  36. * should never change during operation.
  37. * @param $current_version
  38. * The current version of the api. If a module reports its minimum API as
  39. * higher than this, its files will not be loaded. This should never change
  40. * during operation.
  41. *
  42. * @return
  43. * An array of API information, keyed by module. Each module's information will
  44. * contain:
  45. * - 'version': The version of the API required by the module. The module
  46. * should use the lowest number it can support so that the widest range
  47. * of supported versions can be used.
  48. * - 'path': If not provided, this will be the module's path. This is
  49. * where the module will store any subsidiary files. This differs from
  50. * plugin paths which are figured separately.
  51. *
  52. * APIs can request any other information to be placed here that they might
  53. * need. This should be in the documentation for that particular API.
  54. */
  55. function ctools_plugin_api_info($owner, $api, $minimum_version, $current_version) {
  56. $cache = &drupal_static(__FUNCTION__, array());
  57. if (!isset($cache[$owner][$api])) {
  58. $cache[$owner][$api] = array();
  59. $hook = ctools_plugin_api_get_hook($owner, $api);
  60. foreach (module_implements($hook) as $module) {
  61. $function = $module . '_' . $hook;
  62. $info = $function($owner, $api);
  63. $version = NULL;
  64. // This is added to make hook_views_api() compatible with this, since
  65. // views used a different version key.
  66. if (isset($info['version'])) {
  67. $version = $info['version'];
  68. }
  69. else if (isset($info['api'])) {
  70. $version = $info['api'];
  71. }
  72. if (!isset($version)) {
  73. continue;
  74. }
  75. // Only process if version is between minimum and current, inclusive.
  76. if (version_compare($version, $minimum_version, '>=') && version_compare($version, $current_version, '<=')) {
  77. if (!isset($info['path'])) {
  78. $info['path'] = drupal_get_path('module', $module);
  79. }
  80. $cache[$owner][$api][$module] = $info;
  81. }
  82. }
  83. // And allow themes to implement these as well.
  84. $themes = _ctools_list_themes();
  85. foreach ($themes as $name => $theme) {
  86. if (!empty($theme->info['api'][$owner][$api])) {
  87. $info = $theme->info['api'][$owner][$api];
  88. if (!isset($info['version'])) {
  89. continue;
  90. }
  91. // Only process if version is between minimum and current, inclusive.
  92. if (version_compare($info['version'], $minimum_version, '>=') && version_compare($info['version'], $current_version, '<=')) {
  93. if (!isset($info['path'])) {
  94. $info['path'] = '';
  95. }
  96. // Because themes can't easily specify full path, we add it here
  97. // even though we do not for modules:
  98. $info['path'] = drupal_get_path('theme', $name) . '/' . $info['path'];
  99. $cache[$owner][$api][$name] = $info;
  100. }
  101. }
  102. }
  103. // Allow other modules to hook in.
  104. drupal_alter($hook, $cache[$owner][$api]);
  105. }
  106. return $cache[$owner][$api];
  107. }
  108. /**
  109. * Load a group of API files.
  110. *
  111. * This will ask each module if they support the given API, and if they do
  112. * it will load the specified file name. The API and the file name
  113. * coincide by design.
  114. *
  115. * @param $owner
  116. * The name of the module that controls the API.
  117. * @param $api
  118. * The name of the api. The api name forms the file name:
  119. * $module.$api.inc, though this can be overridden by the module's response.
  120. * @param $minimum_version
  121. * The lowest version API that is compatible with this one. If a module
  122. * reports its API as older than this, its files will not be loaded. This
  123. * should never change during operation.
  124. * @param $current_version
  125. * The current version of the api. If a module reports its minimum API as
  126. * higher than this, its files will not be loaded. This should never change
  127. * during operation.
  128. *
  129. * @return
  130. * The API information, in case you need it.
  131. */
  132. function ctools_plugin_api_include($owner, $api, $minimum_version, $current_version) {
  133. static $already_done = array();
  134. $info = ctools_plugin_api_info($owner, $api, $minimum_version, $current_version);
  135. foreach ($info as $module => $plugin_info) {
  136. if (!isset($already_done[$owner][$api][$module])) {
  137. if (isset($plugin_info["$api file"])) {
  138. $file = $plugin_info["$api file"];
  139. }
  140. else if (isset($plugin_info['file'])) {
  141. $file = $plugin_info['file'];
  142. }
  143. else {
  144. $file = "$module.$api.inc";
  145. }
  146. if (file_exists(DRUPAL_ROOT . "/$plugin_info[path]/$file")) {
  147. require_once DRUPAL_ROOT . "/$plugin_info[path]/$file";
  148. }
  149. else if (file_exists(DRUPAL_ROOT . "/$file")) {
  150. require_once DRUPAL_ROOT . "/$file";
  151. }
  152. $already_done[$owner][$api][$module] = TRUE;
  153. }
  154. }
  155. return $info;
  156. }
  157. /**
  158. * Find out what hook to use to determine if modules support an API.
  159. *
  160. * By default, most APIs will use hook_ctools_plugin_api, but some modules
  161. * want sole ownership. This technique lets modules define what hook
  162. * to use.
  163. */
  164. function ctools_plugin_api_get_hook($owner, $api) {
  165. // Allow modules to use their own hook for this. The only easy way to do
  166. // this right now is with a magically named function.
  167. if (function_exists($function = $owner . '_' . $api . '_hook_name')) {
  168. $hook = $function();
  169. }
  170. else if (function_exists($function = $owner . '_ctools_plugin_api_hook_name')) {
  171. $hook = $function();
  172. }
  173. // Do this last so that if the $function above failed to return, we have a
  174. // sane default.
  175. if (empty($hook)) {
  176. $hook = 'ctools_plugin_api';
  177. }
  178. return $hook;
  179. }
  180. /**
  181. * Fetch a group of plugins by name.
  182. *
  183. * @param $module
  184. * The name of the module that utilizes this plugin system. It will be
  185. * used to call hook_ctools_plugin_$plugin() to get more data about the plugin.
  186. * @param $type
  187. * The type identifier of the plugin.
  188. * @param $id
  189. * If specified, return only information about plugin with this identifier.
  190. * The system will do its utmost to load only plugins with this id.
  191. *
  192. * @return
  193. * An array of information arrays about the plugins received. The contents
  194. * of the array are specific to the plugin.
  195. */
  196. function ctools_get_plugins($module, $type, $id = NULL) {
  197. // Store local caches of plugins and plugin info so we don't have to do full
  198. // lookups everytime.
  199. $plugins = &drupal_static('ctools_plugins', array());
  200. $info = ctools_plugin_get_plugin_type_info();
  201. // Bail out noisily if an invalid module/type combination is requested.
  202. if (!isset($info[$module][$type])) {
  203. watchdog('ctools', 'Invalid plugin module/type combination requested: module @module and type @type', array('@module' => $module, '@type' => $type), WATCHDOG_ERROR);
  204. return array();
  205. }
  206. // Make sure our plugins array is populated.
  207. if (!isset($plugins[$module][$type])) {
  208. $plugins[$module][$type] = array();
  209. }
  210. // Attempt to shortcut this whole piece of code if we already have
  211. // the requested plugin:
  212. if ($id && array_key_exists($id, $plugins[$module][$type])) {
  213. return $plugins[$module][$type][$id];
  214. }
  215. // Store the status of plugin loading. If a module plugin type pair is true,
  216. // then it is fully loaded and no searching or setup needs to be done.
  217. $setup = &drupal_static('ctools_plugin_setup', array());
  218. // We assume we don't need to build a cache.
  219. $build_cache = FALSE;
  220. // If the plugin info says this can be cached, check cache first.
  221. if ($info[$module][$type]['cache'] && empty($setup[$module][$type])) {
  222. $cache = cache_get("plugins:$module:$type", $info[$module][$type]['cache table']);
  223. if (!empty($cache->data)) {
  224. // Cache load succeeded so use the cached plugin list.
  225. $plugins[$module][$type] = $cache->data;
  226. // Set $setup to true so we know things where loaded.
  227. $setup[$module][$type] = TRUE;
  228. }
  229. else {
  230. // Cache load failed so store that we need to build and write the cache.
  231. $build_cache = TRUE;
  232. }
  233. }
  234. // Always load all hooks if we need them. Note we only need them now if the
  235. // plugin asks for them. We can assume that if we have plugins we've already
  236. // called the global hook.
  237. if (!empty($info[$module][$type]['use hooks']) && empty($plugins[$module][$type])) {
  238. $plugins[$module][$type] = ctools_plugin_load_hooks($info[$module][$type]);
  239. }
  240. // Then see if we should load all files. We only do this if we
  241. // want a list of all plugins or there was a cache miss.
  242. if (empty($setup[$module][$type]) && ($build_cache || !$id)) {
  243. $setup[$module][$type] = TRUE;
  244. $plugins[$module][$type] = array_merge($plugins[$module][$type], ctools_plugin_load_includes($info[$module][$type]));
  245. // If the plugin can have child plugins, and we're loading all plugins,
  246. // go through the list of plugins we have and find child plugins.
  247. if (!$id && !empty($info[$module][$type]['child plugins'])) {
  248. // If a plugin supports children, go through each plugin and ask.
  249. $temp = array();
  250. foreach ($plugins[$module][$type] as $name => $plugin) {
  251. // The strpos ensures that we don't try to find children for plugins
  252. // that are already children.
  253. if (!empty($plugin['get children']) && function_exists($plugin['get children']) && strpos($name, ':') === FALSE) {
  254. $temp = array_merge($plugin['get children']($plugin, $name), $temp);
  255. }
  256. else {
  257. $temp[$name] = $plugin;
  258. }
  259. }
  260. $plugins[$module][$type] = $temp;
  261. }
  262. }
  263. // If we were told earlier that this is cacheable and the cache was
  264. // empty, give something back.
  265. if ($build_cache) {
  266. cache_set("plugins:$module:$type", $plugins[$module][$type], $info[$module][$type]['cache table']);
  267. }
  268. // If no id was requested, we are finished here:
  269. if (!$id) {
  270. // Use array_filter because looking for unknown plugins could cause NULL
  271. // entries to appear in the list later.
  272. return array_filter($plugins[$module][$type]);
  273. }
  274. // Check to see if we need to look for the file
  275. if (!array_key_exists($id, $plugins[$module][$type])) {
  276. // If we can have child plugins, check to see if the plugin name is in the
  277. // format of parent:child and break it up if it is.
  278. if (!empty($info[$module][$type]['child plugins']) && strpos($id, ':') !== FALSE) {
  279. list($parent, $child) = explode(':', $id, 2);
  280. }
  281. else {
  282. $parent = $id;
  283. }
  284. if (!array_key_exists($parent, $plugins[$module][$type])) {
  285. $result = ctools_plugin_load_includes($info[$module][$type], $parent);
  286. // Set to either what was returned or NULL.
  287. $plugins[$module][$type][$parent] = isset($result[$parent]) ? $result[$parent] : NULL;
  288. }
  289. // If we are looking for a child, and have the parent, ask the parent for the child.
  290. if (!empty($child) && !empty($plugins[$module][$type][$parent]) && function_exists($plugins[$module][$type][$parent]['get child'])) {
  291. $plugins[$module][$type][$id] = $plugins[$module][$type][$parent]['get child']($plugins[$module][$type][$parent], $parent, $child);
  292. }
  293. }
  294. // At this point we should either have the plugin, or a NULL.
  295. return $plugins[$module][$type][$id];
  296. }
  297. /**
  298. * Return the full list of plugin type info for all plugin types registered in
  299. * the current system.
  300. *
  301. * This function manages its own cache getting/setting, and should always be
  302. * used as the way to initially populate the list of plugin types. Make sure you
  303. * call this function to properly populate the ctools_plugin_type_info static
  304. * variable.
  305. *
  306. * @return array
  307. * A multilevel array of plugin type info, the outer array keyed on module
  308. * name and each inner array keyed on plugin type name.
  309. */
  310. function ctools_plugin_get_plugin_type_info($flush = FALSE) {
  311. $info_loaded = &drupal_static('ctools_plugin_type_info_loaded', FALSE);
  312. $all_type_info = &drupal_static('ctools_plugin_type_info', array());
  313. // Only trigger info loading once.
  314. if ($info_loaded && !$flush) {
  315. return $all_type_info;
  316. }
  317. $info_loaded = TRUE;
  318. $cache = cache_get('ctools_plugin_type_info');
  319. if (!empty($cache->data) && !$flush) {
  320. // Plugin type info cache is warm, use it.
  321. $all_type_info = $cache->data;
  322. }
  323. else {
  324. // Cache expired, refill it.
  325. foreach (module_implements('ctools_plugin_type') as $module) {
  326. $module_infos = array();
  327. $function = $module . '_ctools_plugin_type';
  328. $module_infos = $function();
  329. foreach ($module_infos as $plugin_type_name => $plugin_type_info) {
  330. // Apply defaults. Array addition will not overwrite pre-existing keys.
  331. $plugin_type_info += array(
  332. 'module' => $module,
  333. 'type' => $plugin_type_name,
  334. 'cache' => FALSE,
  335. 'cache table' => 'cache',
  336. 'classes' => array(),
  337. 'use hooks' => FALSE,
  338. 'defaults' => array(),
  339. 'process' => '',
  340. 'alterable' => TRUE,
  341. 'extension' => 'inc',
  342. 'info file' => FALSE,
  343. 'hook' => $module . '_' . $plugin_type_name,
  344. 'load themes' => FALSE,
  345. );
  346. $all_type_info[$module][$plugin_type_name] = $plugin_type_info;
  347. }
  348. }
  349. cache_set('ctools_plugin_type_info', $all_type_info);
  350. }
  351. return $all_type_info;
  352. }
  353. /**
  354. * Reset all static caches that affect the result of ctools_get_plugins().
  355. */
  356. function ctools_get_plugins_reset() {
  357. drupal_static_reset('ctools_plugins');
  358. drupal_static_reset('ctools_plugin_setup');
  359. drupal_static_reset('ctools_plugin_load_includes');
  360. drupal_static_reset('ctools_plugin_api_info');
  361. }
  362. /**
  363. * Load plugins from a directory.
  364. *
  365. * @param $info
  366. * The plugin info as returned by ctools_plugin_get_info()
  367. * @param $file
  368. * The file to load if we're looking for just one particular plugin.
  369. *
  370. * @return
  371. * An array of information created for this plugin.
  372. */
  373. function ctools_plugin_load_includes($info, $filename = NULL) {
  374. // Keep a static array so we don't hit file_scan_directory more than necessary.
  375. $all_files = &drupal_static(__FUNCTION__, array());
  376. // store static of plugin arrays for reference because they can't be reincluded.
  377. static $plugin_arrays = array();
  378. // If we're being asked for all plugins of a type, skip any caching
  379. // we may have done because this is an admin task and it's ok to
  380. // spend the extra time.
  381. if (!isset($filename)) {
  382. $all_files[$info['module']][$info['type']] = NULL;
  383. }
  384. if (!isset($all_files[$info['module']][$info['type']])) {
  385. // If a filename was set, we will try to load our list of files from
  386. // cache. This is considered normal operation and we try to reduce
  387. // the time spent finding files.
  388. if (isset($filename)) {
  389. $cache = cache_get("ctools_plugin_files:$info[module]:$info[type]");
  390. if ($cache) {
  391. $all_files[$info['module']][$info['type']] = $cache->data;
  392. }
  393. }
  394. if (!isset($all_files[$info['module']][$info['type']])) {
  395. $all_files[$info['module']][$info['type']] = array();
  396. // Load all our plugins.
  397. $directories = ctools_plugin_get_directories($info);
  398. $extension = (empty($info['info file']) || ($info['extension'] != 'inc')) ? $info['extension'] : 'info';
  399. foreach ($directories as $module => $path) {
  400. $all_files[$info['module']][$info['type']][$module] = file_scan_directory($path, '/\.' . $extension . '$/', array('key' => 'name'));
  401. }
  402. cache_set("ctools_plugin_files:$info[module]:$info[type]", $all_files[$info['module']][$info['type']]);
  403. }
  404. }
  405. $file_list = $all_files[$info['module']][$info['type']];
  406. $plugins = array();
  407. // Iterate through all the plugin .inc files, load them and process the hook
  408. // that should now be available.
  409. foreach (array_filter($file_list) as $module => $files) {
  410. if ($filename) {
  411. $files = isset($files[$filename]) ? array($filename => $files[$filename]) : array();
  412. }
  413. foreach ($files as $file) {
  414. if (!empty($info['info file'])) {
  415. // Parse a .info file
  416. $result = ctools_plugin_process_info($info, $module, $file);
  417. }
  418. else {
  419. // Parse a hook.
  420. $plugin = NULL; // ensure that we don't have something leftover from earlier.
  421. if (isset($plugin_arrays[$file->uri])) {
  422. $identifier = $plugin_arrays[$file->uri];
  423. }
  424. else {
  425. require_once DRUPAL_ROOT . '/' . $file->uri;
  426. // .inc files have a special format for the hook identifier.
  427. // For example, 'foo.inc' in the module 'mogul' using the plugin
  428. // whose hook is named 'borg_type' should have a function named (deep breath)
  429. // mogul_foo_borg_type()
  430. // If, however, the .inc file set the quasi-global $plugin array, we
  431. // can use that and not even call a function. Set the $identifier
  432. // appropriately and ctools_plugin_process() will handle it.
  433. if (isset($plugin)) {
  434. $plugin_arrays[$file->uri] = $plugin;
  435. $identifier = $plugin;
  436. }
  437. else {
  438. $identifier = $module . '_' . $file->name;
  439. }
  440. }
  441. $result = ctools_plugin_process($info, $module, $identifier, dirname($file->uri), basename($file->uri), $file->name);
  442. }
  443. if (is_array($result)) {
  444. $plugins = array_merge($plugins, $result);
  445. }
  446. }
  447. }
  448. return $plugins;
  449. }
  450. /**
  451. * Get a list of directories to search for plugins of the given type.
  452. *
  453. * This utilizes hook_ctools_plugin_directory() to determine a complete list of
  454. * directories. Only modules that implement this hook and return a string
  455. * value will have their directories included.
  456. *
  457. * @param $info
  458. * The $info array for the plugin as returned by ctools_plugin_get_info().
  459. *
  460. * @return array $directories
  461. * An array of directories to search.
  462. */
  463. function ctools_plugin_get_directories($info) {
  464. $directories = array();
  465. foreach (module_implements('ctools_plugin_directory') as $module) {
  466. $function = $module . '_ctools_plugin_directory';
  467. $result = $function($info['module'], $info['type']);
  468. if ($result && is_string($result)) {
  469. $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
  470. }
  471. }
  472. if (!empty($info['load themes'])) {
  473. $themes = _ctools_list_themes();
  474. foreach ($themes as $name => $theme) {
  475. if (!empty($theme->info['plugins'][$info['module']][$info['type']])) {
  476. $directories[$name] = drupal_get_path('theme', $name) . '/' . $theme->info['plugins'][$info['module']][$info['type']];
  477. }
  478. }
  479. }
  480. return $directories;
  481. }
  482. /**
  483. * Helper function to build a ctools-friendly list of themes capable of
  484. * providing plugins.
  485. *
  486. * @return array $themes
  487. * A list of themes that can act as plugin providers, sorted parent-first with
  488. * the active theme placed last.
  489. */
  490. function _ctools_list_themes() {
  491. static $themes;
  492. if (is_null($themes)) {
  493. $current = variable_get('theme_default', FALSE);
  494. $themes = $active = array();
  495. $all_themes = list_themes();
  496. foreach ($all_themes as $name => $theme) {
  497. // Only search from active themes
  498. if (empty($theme->status) && $theme->name != $current) {
  499. continue;
  500. }
  501. $active[$name] = $theme;
  502. // Prior to drupal 6.14, $theme->base_themes does not exist. Build it.
  503. if (!isset($theme->base_themes) && !empty($theme->base_theme)) {
  504. $active[$name]->base_themes = ctools_find_base_themes($all_themes, $name);
  505. }
  506. }
  507. // Construct a parent-first list of all themes
  508. foreach ($active as $name => $theme) {
  509. $base_themes = isset($theme->base_themes) ? $theme->base_themes : array();
  510. $themes = array_merge($themes, $base_themes, array($name => $theme->info['name']));
  511. }
  512. // Put the actual theme info objects into the array
  513. foreach (array_keys($themes) as $name) {
  514. if (isset($all_themes[$name])) {
  515. $themes[$name] = $all_themes[$name];
  516. }
  517. }
  518. // Make sure the current default theme always gets the last word
  519. if ($current_key = array_search($current, array_keys($themes))) {
  520. $themes += array_splice($themes, $current_key, 1);
  521. }
  522. }
  523. return $themes;
  524. }
  525. /**
  526. * Find all the base themes for the specified theme.
  527. *
  528. * Themes can inherit templates and function implementations from earlier themes.
  529. *
  530. * NOTE: this is a verbatim copy of system_find_base_themes(), which was not
  531. * implemented until 6.14. It is included here only as a fallback for outdated
  532. * versions of drupal core.
  533. *
  534. * @param $themes
  535. * An array of available themes.
  536. * @param $key
  537. * The name of the theme whose base we are looking for.
  538. * @param $used_keys
  539. * A recursion parameter preventing endless loops.
  540. * @return
  541. * Returns an array of all of the theme's ancestors; the first element's value
  542. * will be NULL if an error occurred.
  543. */
  544. function ctools_find_base_themes($themes, $key, $used_keys = array()) {
  545. $base_key = $themes[$key]->info['base theme'];
  546. // Does the base theme exist?
  547. if (!isset($themes[$base_key])) {
  548. return array($base_key => NULL);
  549. }
  550. $current_base_theme = array($base_key => $themes[$base_key]->info['name']);
  551. // Is the base theme itself a child of another theme?
  552. if (isset($themes[$base_key]->info['base theme'])) {
  553. // Do we already know the base themes of this theme?
  554. if (isset($themes[$base_key]->base_themes)) {
  555. return $themes[$base_key]->base_themes + $current_base_theme;
  556. }
  557. // Prevent loops.
  558. if (!empty($used_keys[$base_key])) {
  559. return array($base_key => NULL);
  560. }
  561. $used_keys[$base_key] = TRUE;
  562. return ctools_find_base_themes($themes, $base_key, $used_keys) + $current_base_theme;
  563. }
  564. // If we get here, then this is our parent theme.
  565. return $current_base_theme;
  566. }
  567. /**
  568. * Load plugin info for the provided hook; this is handled separately from
  569. * plugins from files.
  570. *
  571. * @param $info
  572. * The info array about the plugin as created by ctools_plugin_get_info()
  573. *
  574. * @return
  575. * An array of info supplied by any hook implementations.
  576. */
  577. function ctools_plugin_load_hooks($info) {
  578. $hooks = array();
  579. foreach (module_implements($info['hook']) as $module) {
  580. $result = ctools_plugin_process($info, $module, $module, drupal_get_path('module', $module));
  581. if (is_array($result)) {
  582. $hooks = array_merge($hooks, $result);
  583. }
  584. }
  585. return $hooks;
  586. }
  587. /**
  588. * Process a single hook implementation of a ctools plugin.
  589. *
  590. * @param $info
  591. * The $info array about the plugin as returned by ctools_plugin_get_info()
  592. * @param $module
  593. * The module that implements the plugin being processed.
  594. * @param $identifier
  595. * The plugin identifier, which is used to create the name of the hook
  596. * function being called.
  597. * @param $path
  598. * The path where files utilized by this plugin will be found.
  599. * @param $file
  600. * The file that was loaded for this plugin, if it exists.
  601. * @param $base
  602. * The base plugin name to use. If a file was loaded for the plugin, this
  603. * is the plugin to assume must be present. This is used to automatically
  604. * translate the array to make the syntax more friendly to plugin
  605. * implementors.
  606. */
  607. function ctools_plugin_process($info, $module, $identifier, $path, $file = NULL, $base = NULL) {
  608. if (is_array($identifier)) {
  609. $result = $identifier;
  610. }
  611. else {
  612. $function = $identifier . '_' . $info['hook'];
  613. if (!function_exists($function)) {
  614. return NULL;
  615. }
  616. $result = $function();
  617. if (!isset($result) || !is_array($result)) {
  618. return NULL;
  619. }
  620. }
  621. // Automatically convert to the proper format that lets plugin implementations
  622. // not nest arrays as deeply as they used to, but still support the older
  623. // format where they do:
  624. if ($base && (!isset($result[$base]) || !is_array($result[$base]))) {
  625. $result = array($base => $result);
  626. }
  627. return _ctools_process_data($result, $info, $module, $path, $file);
  628. }
  629. /**
  630. * Fill in default values and run hooks for data loaded for one or
  631. * more plugins.
  632. */
  633. function _ctools_process_data($result, $plugin_type_info, $module, $path, $file) {
  634. // Fill in global defaults.
  635. foreach ($result as $name => $plugin) {
  636. $result[$name] += array(
  637. 'module' => $module,
  638. 'name' => $name,
  639. 'path' => $path,
  640. 'file' => $file,
  641. 'plugin module' => $plugin_type_info['module'],
  642. 'plugin type' => $plugin_type_info['type'],
  643. );
  644. // Fill in plugin-specific defaults, if they exist.
  645. if (!empty($plugin_type_info['defaults'])) {
  646. if (is_array($plugin_type_info['defaults'])) {
  647. $result[$name] += $plugin_type_info['defaults'];
  648. }
  649. }
  650. // Allow the plugin to be altered before processing.
  651. if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) {
  652. drupal_alter('ctools_plugin_pre', $result[$name], $plugin_type_info);
  653. }
  654. // Allow the plugin owner to do additional processing.
  655. if (!empty($plugin_type_info['process']) && $function = ctools_plugin_get_function($plugin_type_info, 'process')) {
  656. $function($result[$name], $plugin_type_info);
  657. }
  658. // Allow the plugin to be altered after processing.
  659. if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) {
  660. drupal_alter('ctools_plugin_post', $result[$name], $plugin_type_info);
  661. }
  662. }
  663. return $result;
  664. }
  665. /**
  666. * Process an info file for plugin information, rather than a hook.
  667. */
  668. function ctools_plugin_process_info($info, $module, $file) {
  669. $result = drupal_parse_info_file($file->uri);
  670. if ($result) {
  671. $result = array($file->name => $result);
  672. return _ctools_process_data($result, $info, $module, dirname($file->uri), basename($file->uri));
  673. }
  674. }
  675. /**
  676. * Ask a module for info about a particular plugin type.
  677. */
  678. function ctools_plugin_get_info($module, $type) {
  679. $all_info = ctools_plugin_get_plugin_type_info();
  680. return isset($all_info[$module][$type]) ? $all_info[$module][$type] : array();
  681. }
  682. /**
  683. * Get a function from a plugin, if it exists. If the plugin is not already
  684. * loaded, try ctools_plugin_load_function() instead.
  685. *
  686. * @param $plugin_definition
  687. * The loaded plugin type.
  688. * @param $function_name
  689. * The identifier of the function. For example, 'settings form'.
  690. *
  691. * @return
  692. * The actual name of the function to call, or NULL if the function
  693. * does not exist.
  694. */
  695. function ctools_plugin_get_function($plugin_definition, $function_name) {
  696. // If cached the .inc file may not have been loaded. require_once is quite safe
  697. // and fast so it's okay to keep calling it.
  698. if (isset($plugin_definition['file'])) {
  699. // Plugins that are loaded from info files have the info file as
  700. // $plugin['file']. Don't try to run those.
  701. $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
  702. if (empty($info['info file'])) {
  703. require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
  704. }
  705. }
  706. if (!isset($plugin_definition[$function_name])) {
  707. return;
  708. }
  709. if (is_array($plugin_definition[$function_name]) && isset($plugin_definition[$function_name]['function'])) {
  710. $function = $plugin_definition[$function_name]['function'];
  711. if (isset($plugin_definition[$function_name]['file'])) {
  712. $file = $plugin_definition[$function_name]['file'];
  713. if (isset($plugin_definition[$function_name]['path'])) {
  714. $file = $plugin_definition[$function_name]['path'] . '/' . $file;
  715. }
  716. require_once DRUPAL_ROOT . '/' . $file;
  717. }
  718. }
  719. else {
  720. $function = $plugin_definition[$function_name];
  721. }
  722. if (function_exists($function)) {
  723. return $function;
  724. }
  725. }
  726. /**
  727. * Load a plugin and get a function name from it, returning success only
  728. * if the function exists.
  729. *
  730. * @param $module
  731. * The module that owns the plugin type.
  732. * @param $type
  733. * The type of plugin.
  734. * @param $id
  735. * The id of the specific plugin to load.
  736. * @param $function_name
  737. * The identifier of the function. For example, 'settings form'.
  738. *
  739. * @return
  740. * The actual name of the function to call, or NULL if the function
  741. * does not exist.
  742. */
  743. function ctools_plugin_load_function($module, $type, $id, $function_name) {
  744. $plugin = ctools_get_plugins($module, $type, $id);
  745. return ctools_plugin_get_function($plugin, $function_name);
  746. }
  747. /**
  748. * Get a class from a plugin, if it exists. If the plugin is not already
  749. * loaded, try ctools_plugin_load_class() instead.
  750. *
  751. * @param $plugin_definition
  752. * The loaded plugin type.
  753. * @param $class_name
  754. * The identifier of the class. For example, 'handler'.
  755. *
  756. * @return
  757. * The actual name of the class to call, or NULL if the class does not exist.
  758. */
  759. function ctools_plugin_get_class($plugin_definition, $class_name) {
  760. // If cached the .inc file may not have been loaded. require_once is quite safe
  761. // and fast so it's okay to keep calling it.
  762. if (isset($plugin_definition['file'])) {
  763. // Plugins that are loaded from info files have the info file as
  764. // $plugin['file']. Don't try to run those.
  765. $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
  766. if (empty($info['info file'])) {
  767. require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
  768. }
  769. }
  770. $return = FALSE;
  771. if (!isset($plugin_definition[$class_name])) {
  772. return;
  773. }
  774. else if (is_string($plugin_definition[$class_name])) {
  775. // Plugin uses the string form shorthand.
  776. $return = $plugin_definition[$class_name];
  777. }
  778. else if (isset($plugin_definition[$class_name]['class'])) {
  779. // Plugin uses the verbose array form.
  780. $return = $plugin_definition[$class_name]['class'];
  781. }
  782. // @todo consider adding an else {watchdog(...)} here
  783. return ($return && class_exists($return)) ? $return : NULL;
  784. }
  785. /**
  786. * Load a plugin and get a class name from it, returning success only if the
  787. * class exists.
  788. *
  789. * @param $module
  790. * The module that owns the plugin type.
  791. * @param $type
  792. * The type of plugin.
  793. * @param $id
  794. * The id of the specific plugin to load.
  795. * @param $class_name
  796. * The identifier of the class. For example, 'handler'.
  797. *
  798. * @return
  799. * The actual name of the class to call, or NULL if the class does not exist.
  800. */
  801. function ctools_plugin_load_class($module, $type, $id, $class_name) {
  802. $plugin = ctools_get_plugins($module, $type, $id);
  803. return ctools_plugin_get_class($plugin, $class_name);
  804. }
  805. /**
  806. * Sort callback for sorting plugins naturally.
  807. *
  808. * Sort first by weight, then by title.
  809. */
  810. function ctools_plugin_sort($a, $b) {
  811. if (is_object($a)) {
  812. $a = (array) $a;
  813. }
  814. if (is_object($b)) {
  815. $b = (array) $b;
  816. }
  817. if (empty($a['weight'])) {
  818. $a['weight'] = 0;
  819. }
  820. if (empty($b['weight'])) {
  821. $b['weight'] = 0;
  822. }
  823. if ($a['weight'] == $b['weight']) {
  824. return strnatcmp(strtolower($a['title']), strtolower($b['title']));
  825. }
  826. return ($a['weight'] < $b['weight']) ? -1 : 1;
  827. }