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. static $drupal_static_fast;
  200. if (!isset($drupal_static_fast)) {
  201. $drupal_static_fast['plugins'] = &drupal_static('ctools_plugins', array());
  202. }
  203. $plugins = &$drupal_static_fast['plugins'];
  204. $info = ctools_plugin_get_plugin_type_info();
  205. // Bail out noisily if an invalid module/type combination is requested.
  206. if (!isset($info[$module][$type])) {
  207. watchdog('ctools', 'Invalid plugin module/type combination requested: module @module and type @type', array('@module' => $module, '@type' => $type), WATCHDOG_ERROR);
  208. return array();
  209. }
  210. // Make sure our plugins array is populated.
  211. if (!isset($plugins[$module][$type])) {
  212. $plugins[$module][$type] = array();
  213. }
  214. // Attempt to shortcut this whole piece of code if we already have
  215. // the requested plugin:
  216. if ($id && array_key_exists($id, $plugins[$module][$type])) {
  217. return $plugins[$module][$type][$id];
  218. }
  219. // Store the status of plugin loading. If a module plugin type pair is true,
  220. // then it is fully loaded and no searching or setup needs to be done.
  221. $setup = &drupal_static('ctools_plugin_setup', array());
  222. // We assume we don't need to build a cache.
  223. $build_cache = FALSE;
  224. // If the plugin info says this can be cached, check cache first.
  225. if ($info[$module][$type]['cache'] && empty($setup[$module][$type])) {
  226. $cache = cache_get("plugins:$module:$type", $info[$module][$type]['cache table']);
  227. if (!empty($cache->data)) {
  228. // Cache load succeeded so use the cached plugin list.
  229. $plugins[$module][$type] = $cache->data;
  230. // Set $setup to true so we know things where loaded.
  231. $setup[$module][$type] = TRUE;
  232. }
  233. else {
  234. // Cache load failed so store that we need to build and write the cache.
  235. $build_cache = TRUE;
  236. }
  237. }
  238. // Always load all hooks if we need them. Note we only need them now if the
  239. // plugin asks for them. We can assume that if we have plugins we've already
  240. // called the global hook.
  241. if (!empty($info[$module][$type]['use hooks']) && empty($plugins[$module][$type])) {
  242. $plugins[$module][$type] = ctools_plugin_load_hooks($info[$module][$type]);
  243. }
  244. // Then see if we should load all files. We only do this if we
  245. // want a list of all plugins or there was a cache miss.
  246. if (empty($setup[$module][$type]) && ($build_cache || !$id)) {
  247. $setup[$module][$type] = TRUE;
  248. $plugins[$module][$type] = array_merge($plugins[$module][$type], ctools_plugin_load_includes($info[$module][$type]));
  249. // If the plugin can have child plugins, and we're loading all plugins,
  250. // go through the list of plugins we have and find child plugins.
  251. if (!$id && !empty($info[$module][$type]['child plugins'])) {
  252. // If a plugin supports children, go through each plugin and ask.
  253. $temp = array();
  254. foreach ($plugins[$module][$type] as $name => $plugin) {
  255. // The strpos ensures that we don't try to find children for plugins
  256. // that are already children.
  257. if (!empty($plugin['get children']) && function_exists($plugin['get children']) && strpos($name, ':') === FALSE) {
  258. $temp = array_merge($plugin['get children']($plugin, $name), $temp);
  259. }
  260. else {
  261. $temp[$name] = $plugin;
  262. }
  263. }
  264. $plugins[$module][$type] = $temp;
  265. }
  266. }
  267. // If we were told earlier that this is cacheable and the cache was
  268. // empty, give something back.
  269. if ($build_cache) {
  270. cache_set("plugins:$module:$type", $plugins[$module][$type], $info[$module][$type]['cache table']);
  271. }
  272. // If no id was requested, we are finished here:
  273. if (!$id) {
  274. // Use array_filter because looking for unknown plugins could cause NULL
  275. // entries to appear in the list later.
  276. return array_filter($plugins[$module][$type]);
  277. }
  278. // Check to see if we need to look for the file
  279. if (!array_key_exists($id, $plugins[$module][$type])) {
  280. // If we can have child plugins, check to see if the plugin name is in the
  281. // format of parent:child and break it up if it is.
  282. if (!empty($info[$module][$type]['child plugins']) && strpos($id, ':') !== FALSE) {
  283. list($parent, $child) = explode(':', $id, 2);
  284. }
  285. else {
  286. $parent = $id;
  287. }
  288. if (!array_key_exists($parent, $plugins[$module][$type])) {
  289. $result = ctools_plugin_load_includes($info[$module][$type], $parent);
  290. // Set to either what was returned or NULL.
  291. $plugins[$module][$type][$parent] = isset($result[$parent]) ? $result[$parent] : NULL;
  292. }
  293. // If we are looking for a child, and have the parent, ask the parent for the child.
  294. if (!empty($child) && !empty($plugins[$module][$type][$parent]) && function_exists($plugins[$module][$type][$parent]['get child'])) {
  295. $plugins[$module][$type][$id] = $plugins[$module][$type][$parent]['get child']($plugins[$module][$type][$parent], $parent, $child);
  296. }
  297. }
  298. // At this point we should either have the plugin, or a NULL.
  299. return $plugins[$module][$type][$id];
  300. }
  301. /**
  302. * Return the full list of plugin type info for all plugin types registered in
  303. * the current system.
  304. *
  305. * This function manages its own cache getting/setting, and should always be
  306. * used as the way to initially populate the list of plugin types. Make sure you
  307. * call this function to properly populate the ctools_plugin_type_info static
  308. * variable.
  309. *
  310. * @return array
  311. * A multilevel array of plugin type info, the outer array keyed on module
  312. * name and each inner array keyed on plugin type name.
  313. */
  314. function ctools_plugin_get_plugin_type_info($flush = FALSE) {
  315. static $drupal_static_fast;
  316. if (!isset($drupal_static_fast)) {
  317. $drupal_static_fast['info_loaded'] = &drupal_static('ctools_plugin_type_info_loaded', FALSE);
  318. $drupal_static_fast['all_type_info'] = &drupal_static('ctools_plugin_type_info', array());
  319. }
  320. $info_loaded = &$drupal_static_fast['info_loaded'];
  321. $all_type_info = &$drupal_static_fast['all_type_info'];
  322. // Only trigger info loading once.
  323. if ($info_loaded && !$flush) {
  324. return $all_type_info;
  325. }
  326. $info_loaded = TRUE;
  327. $cache = cache_get('ctools_plugin_type_info');
  328. if (!empty($cache->data) && !$flush) {
  329. // Plugin type info cache is warm, use it.
  330. $all_type_info = $cache->data;
  331. }
  332. else {
  333. // Cache expired, refill it.
  334. foreach (module_implements('ctools_plugin_type') as $module) {
  335. $module_infos = array();
  336. $function = $module . '_ctools_plugin_type';
  337. $module_infos = $function();
  338. foreach ($module_infos as $plugin_type_name => $plugin_type_info) {
  339. // Apply defaults. Array addition will not overwrite pre-existing keys.
  340. $plugin_type_info += array(
  341. 'module' => $module,
  342. 'type' => $plugin_type_name,
  343. 'cache' => FALSE,
  344. 'cache table' => 'cache',
  345. 'classes' => array(),
  346. 'use hooks' => FALSE,
  347. 'defaults' => array(),
  348. 'process' => '',
  349. 'alterable' => TRUE,
  350. 'extension' => 'inc',
  351. 'info file' => FALSE,
  352. 'hook' => $module . '_' . $plugin_type_name,
  353. 'load themes' => FALSE,
  354. );
  355. $all_type_info[$module][$plugin_type_name] = $plugin_type_info;
  356. }
  357. }
  358. cache_set('ctools_plugin_type_info', $all_type_info);
  359. }
  360. return $all_type_info;
  361. }
  362. /**
  363. * Reset all static caches that affect the result of ctools_get_plugins().
  364. */
  365. function ctools_get_plugins_reset() {
  366. drupal_static_reset('ctools_plugins');
  367. drupal_static_reset('ctools_plugin_setup');
  368. drupal_static_reset('ctools_plugin_load_includes');
  369. drupal_static_reset('ctools_plugin_api_info');
  370. }
  371. /**
  372. * Load plugins from a directory.
  373. *
  374. * @param $info
  375. * The plugin info as returned by ctools_plugin_get_info()
  376. * @param $file
  377. * The file to load if we're looking for just one particular plugin.
  378. *
  379. * @return
  380. * An array of information created for this plugin.
  381. */
  382. function ctools_plugin_load_includes($info, $filename = NULL) {
  383. // Keep a static array so we don't hit file_scan_directory more than necessary.
  384. $all_files = &drupal_static(__FUNCTION__, array());
  385. // store static of plugin arrays for reference because they can't be reincluded.
  386. static $plugin_arrays = array();
  387. if (!isset($all_files[$info['module']][$info['type']])) {
  388. $cache = cache_get("ctools_plugin_files:$info[module]:$info[type]");
  389. if ($cache) {
  390. $all_files[$info['module']][$info['type']] = $cache->data;
  391. }
  392. // Do not attempt any file scan even if the cached entry was empty.
  393. // A NULL entry here would mean the plugin just does not exists, and we
  394. // cannot afford to run file scan on production sites normal run.
  395. elseif (!isset($all_files[$info['module']][$info['type']])) {
  396. $all_files[$info['module']][$info['type']] = array();
  397. // Load all our plugins.
  398. $directories = ctools_plugin_get_directories($info);
  399. $extension = (empty($info['info file']) || ($info['extension'] != 'inc')) ? $info['extension'] : 'info';
  400. foreach ($directories as $module => $path) {
  401. $all_files[$info['module']][$info['type']][$module] = file_scan_directory($path, '/\.' . $extension . '$/', array('key' => 'name'));
  402. }
  403. cache_set("ctools_plugin_files:$info[module]:$info[type]", $all_files[$info['module']][$info['type']]);
  404. }
  405. }
  406. $file_list = $all_files[$info['module']][$info['type']];
  407. $plugins = array();
  408. // Iterate through all the plugin .inc files, load them and process the hook
  409. // that should now be available.
  410. foreach (array_filter($file_list) as $module => $files) {
  411. if ($filename) {
  412. $files = isset($files[$filename]) ? array($filename => $files[$filename]) : array();
  413. }
  414. foreach ($files as $file) {
  415. if (!empty($info['info file'])) {
  416. // Parse a .info file
  417. $result = ctools_plugin_process_info($info, $module, $file);
  418. }
  419. else {
  420. // Parse a hook.
  421. $plugin = NULL; // ensure that we don't have something leftover from earlier.
  422. if (isset($plugin_arrays[$file->uri])) {
  423. $identifier = $plugin_arrays[$file->uri];
  424. }
  425. else {
  426. require_once DRUPAL_ROOT . '/' . $file->uri;
  427. // .inc files have a special format for the hook identifier.
  428. // For example, 'foo.inc' in the module 'mogul' using the plugin
  429. // whose hook is named 'borg_type' should have a function named (deep breath)
  430. // mogul_foo_borg_type()
  431. // If, however, the .inc file set the quasi-global $plugin array, we
  432. // can use that and not even call a function. Set the $identifier
  433. // appropriately and ctools_plugin_process() will handle it.
  434. if (isset($plugin)) {
  435. $plugin_arrays[$file->uri] = $plugin;
  436. $identifier = $plugin;
  437. }
  438. else {
  439. $identifier = $module . '_' . $file->name;
  440. }
  441. }
  442. $result = ctools_plugin_process($info, $module, $identifier, dirname($file->uri), basename($file->uri), $file->name);
  443. }
  444. if (is_array($result)) {
  445. $plugins = array_merge($plugins, $result);
  446. }
  447. }
  448. }
  449. return $plugins;
  450. }
  451. /**
  452. * Get a list of directories to search for plugins of the given type.
  453. *
  454. * This utilizes hook_ctools_plugin_directory() to determine a complete list of
  455. * directories. Only modules that implement this hook and return a string
  456. * value will have their directories included.
  457. *
  458. * @param $info
  459. * The $info array for the plugin as returned by ctools_plugin_get_info().
  460. *
  461. * @return array $directories
  462. * An array of directories to search.
  463. */
  464. function ctools_plugin_get_directories($info) {
  465. $directories = array();
  466. foreach (module_implements('ctools_plugin_directory') as $module) {
  467. $function = $module . '_ctools_plugin_directory';
  468. $result = $function($info['module'], $info['type']);
  469. if ($result && is_string($result)) {
  470. $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
  471. }
  472. }
  473. if (!empty($info['load themes'])) {
  474. $themes = _ctools_list_themes();
  475. foreach ($themes as $name => $theme) {
  476. if (!empty($theme->info['plugins'][$info['module']][$info['type']])) {
  477. $directories[$name] = drupal_get_path('theme', $name) . '/' . $theme->info['plugins'][$info['module']][$info['type']];
  478. }
  479. }
  480. }
  481. return $directories;
  482. }
  483. /**
  484. * Helper function to build a ctools-friendly list of themes capable of
  485. * providing plugins.
  486. *
  487. * @return array $themes
  488. * A list of themes that can act as plugin providers, sorted parent-first with
  489. * the active theme placed last.
  490. */
  491. function _ctools_list_themes() {
  492. static $themes;
  493. if (is_null($themes)) {
  494. $current = variable_get('theme_default', FALSE);
  495. $themes = $active = array();
  496. $all_themes = list_themes();
  497. foreach ($all_themes as $name => $theme) {
  498. // Only search from active themes
  499. if (empty($theme->status) && $theme->name != $current) {
  500. continue;
  501. }
  502. $active[$name] = $theme;
  503. // Prior to drupal 6.14, $theme->base_themes does not exist. Build it.
  504. if (!isset($theme->base_themes) && !empty($theme->base_theme)) {
  505. $active[$name]->base_themes = ctools_find_base_themes($all_themes, $name);
  506. }
  507. }
  508. // Construct a parent-first list of all themes
  509. foreach ($active as $name => $theme) {
  510. $base_themes = isset($theme->base_themes) ? $theme->base_themes : array();
  511. $themes = array_merge($themes, $base_themes, array($name => $theme->info['name']));
  512. }
  513. // Put the actual theme info objects into the array
  514. foreach (array_keys($themes) as $name) {
  515. if (isset($all_themes[$name])) {
  516. $themes[$name] = $all_themes[$name];
  517. }
  518. }
  519. // Make sure the current default theme always gets the last word
  520. if ($current_key = array_search($current, array_keys($themes))) {
  521. $themes += array_splice($themes, $current_key, 1);
  522. }
  523. }
  524. return $themes;
  525. }
  526. /**
  527. * Find all the base themes for the specified theme.
  528. *
  529. * Themes can inherit templates and function implementations from earlier themes.
  530. *
  531. * NOTE: this is a verbatim copy of system_find_base_themes(), which was not
  532. * implemented until 6.14. It is included here only as a fallback for outdated
  533. * versions of drupal core.
  534. *
  535. * @param $themes
  536. * An array of available themes.
  537. * @param $key
  538. * The name of the theme whose base we are looking for.
  539. * @param $used_keys
  540. * A recursion parameter preventing endless loops.
  541. * @return
  542. * Returns an array of all of the theme's ancestors; the first element's value
  543. * will be NULL if an error occurred.
  544. */
  545. function ctools_find_base_themes($themes, $key, $used_keys = array()) {
  546. $base_key = $themes[$key]->info['base theme'];
  547. // Does the base theme exist?
  548. if (!isset($themes[$base_key])) {
  549. return array($base_key => NULL);
  550. }
  551. $current_base_theme = array($base_key => $themes[$base_key]->info['name']);
  552. // Is the base theme itself a child of another theme?
  553. if (isset($themes[$base_key]->info['base theme'])) {
  554. // Do we already know the base themes of this theme?
  555. if (isset($themes[$base_key]->base_themes)) {
  556. return $themes[$base_key]->base_themes + $current_base_theme;
  557. }
  558. // Prevent loops.
  559. if (!empty($used_keys[$base_key])) {
  560. return array($base_key => NULL);
  561. }
  562. $used_keys[$base_key] = TRUE;
  563. return ctools_find_base_themes($themes, $base_key, $used_keys) + $current_base_theme;
  564. }
  565. // If we get here, then this is our parent theme.
  566. return $current_base_theme;
  567. }
  568. /**
  569. * Load plugin info for the provided hook; this is handled separately from
  570. * plugins from files.
  571. *
  572. * @param $info
  573. * The info array about the plugin as created by ctools_plugin_get_info()
  574. *
  575. * @return
  576. * An array of info supplied by any hook implementations.
  577. */
  578. function ctools_plugin_load_hooks($info) {
  579. $hooks = array();
  580. foreach (module_implements($info['hook']) as $module) {
  581. $result = ctools_plugin_process($info, $module, $module, drupal_get_path('module', $module));
  582. if (is_array($result)) {
  583. $hooks = array_merge($hooks, $result);
  584. }
  585. }
  586. return $hooks;
  587. }
  588. /**
  589. * Process a single hook implementation of a ctools plugin.
  590. *
  591. * @param $info
  592. * The $info array about the plugin as returned by ctools_plugin_get_info()
  593. * @param $module
  594. * The module that implements the plugin being processed.
  595. * @param $identifier
  596. * The plugin identifier, which is used to create the name of the hook
  597. * function being called.
  598. * @param $path
  599. * The path where files utilized by this plugin will be found.
  600. * @param $file
  601. * The file that was loaded for this plugin, if it exists.
  602. * @param $base
  603. * The base plugin name to use. If a file was loaded for the plugin, this
  604. * is the plugin to assume must be present. This is used to automatically
  605. * translate the array to make the syntax more friendly to plugin
  606. * implementors.
  607. */
  608. function ctools_plugin_process($info, $module, $identifier, $path, $file = NULL, $base = NULL) {
  609. if (is_array($identifier)) {
  610. $result = $identifier;
  611. }
  612. else {
  613. $function = $identifier . '_' . $info['hook'];
  614. if (!function_exists($function)) {
  615. return NULL;
  616. }
  617. $result = $function($info);
  618. if (!isset($result) || !is_array($result)) {
  619. return NULL;
  620. }
  621. }
  622. // Automatically convert to the proper format that lets plugin implementations
  623. // not nest arrays as deeply as they used to, but still support the older
  624. // format where they do:
  625. if ($base && (!isset($result[$base]) || !is_array($result[$base]))) {
  626. $result = array($base => $result);
  627. }
  628. return _ctools_process_data($result, $info, $module, $path, $file);
  629. }
  630. /**
  631. * Fill in default values and run hooks for data loaded for one or
  632. * more plugins.
  633. */
  634. function _ctools_process_data($result, $plugin_type_info, $module, $path, $file) {
  635. // Fill in global defaults.
  636. foreach ($result as $name => $plugin) {
  637. $result[$name] += array(
  638. 'module' => $module,
  639. 'name' => $name,
  640. 'path' => $path,
  641. 'file' => $file,
  642. 'plugin module' => $plugin_type_info['module'],
  643. 'plugin type' => $plugin_type_info['type'],
  644. );
  645. // Fill in plugin-specific defaults, if they exist.
  646. if (!empty($plugin_type_info['defaults'])) {
  647. if (is_array($plugin_type_info['defaults'])) {
  648. $result[$name] += $plugin_type_info['defaults'];
  649. }
  650. }
  651. // Allow the plugin to be altered before processing.
  652. if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) {
  653. drupal_alter('ctools_plugin_pre', $result[$name], $plugin_type_info);
  654. }
  655. // Allow the plugin owner to do additional processing.
  656. if (!empty($plugin_type_info['process']) && $function = ctools_plugin_get_function($plugin_type_info, 'process')) {
  657. $function($result[$name], $plugin_type_info);
  658. }
  659. // Allow the plugin to be altered after processing.
  660. if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) {
  661. drupal_alter('ctools_plugin_post', $result[$name], $plugin_type_info);
  662. }
  663. }
  664. return $result;
  665. }
  666. /**
  667. * Process an info file for plugin information, rather than a hook.
  668. */
  669. function ctools_plugin_process_info($info, $module, $file) {
  670. $result = drupal_parse_info_file($file->uri);
  671. if ($result) {
  672. $result = array($file->name => $result);
  673. return _ctools_process_data($result, $info, $module, dirname($file->uri), basename($file->uri));
  674. }
  675. }
  676. /**
  677. * Ask a module for info about a particular plugin type.
  678. */
  679. function ctools_plugin_get_info($module, $type) {
  680. $all_info = ctools_plugin_get_plugin_type_info();
  681. return isset($all_info[$module][$type]) ? $all_info[$module][$type] : array();
  682. }
  683. /**
  684. * Get a function from a plugin, if it exists. If the plugin is not already
  685. * loaded, try ctools_plugin_load_function() instead.
  686. *
  687. * @param $plugin_definition
  688. * The loaded plugin type.
  689. * @param $function_name
  690. * The identifier of the function. For example, 'settings form'.
  691. *
  692. * @return
  693. * The actual name of the function to call, or NULL if the function
  694. * does not exist.
  695. */
  696. function ctools_plugin_get_function($plugin_definition, $function_name) {
  697. // If cached the .inc file may not have been loaded. require_once is quite safe
  698. // and fast so it's okay to keep calling it.
  699. if (isset($plugin_definition['file'])) {
  700. // Plugins that are loaded from info files have the info file as
  701. // $plugin['file']. Don't try to run those.
  702. $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
  703. if (empty($info['info file'])) {
  704. require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
  705. }
  706. }
  707. if (!isset($plugin_definition[$function_name])) {
  708. return;
  709. }
  710. if (is_array($plugin_definition[$function_name]) && isset($plugin_definition[$function_name]['function'])) {
  711. $function = $plugin_definition[$function_name]['function'];
  712. if (isset($plugin_definition[$function_name]['file'])) {
  713. $file = $plugin_definition[$function_name]['file'];
  714. if (isset($plugin_definition[$function_name]['path'])) {
  715. $file = $plugin_definition[$function_name]['path'] . '/' . $file;
  716. }
  717. require_once DRUPAL_ROOT . '/' . $file;
  718. }
  719. }
  720. else {
  721. $function = $plugin_definition[$function_name];
  722. }
  723. if (function_exists($function)) {
  724. return $function;
  725. }
  726. }
  727. /**
  728. * Load a plugin and get a function name from it, returning success only
  729. * if the function exists.
  730. *
  731. * @param $module
  732. * The module that owns the plugin type.
  733. * @param $type
  734. * The type of plugin.
  735. * @param $id
  736. * The id of the specific plugin to load.
  737. * @param $function_name
  738. * The identifier of the function. For example, 'settings form'.
  739. *
  740. * @return
  741. * The actual name of the function to call, or NULL if the function
  742. * does not exist.
  743. */
  744. function ctools_plugin_load_function($module, $type, $id, $function_name) {
  745. $plugin = ctools_get_plugins($module, $type, $id);
  746. return ctools_plugin_get_function($plugin, $function_name);
  747. }
  748. /**
  749. * Get a class from a plugin, if it exists. If the plugin is not already
  750. * loaded, try ctools_plugin_load_class() instead.
  751. *
  752. * @param $plugin_definition
  753. * The loaded plugin type.
  754. * @param $class_name
  755. * The identifier of the class. For example, 'handler'.
  756. *
  757. * @return
  758. * The actual name of the class to call, or NULL if the class does not exist.
  759. */
  760. function ctools_plugin_get_class($plugin_definition, $class_name) {
  761. // If cached the .inc file may not have been loaded. require_once is quite safe
  762. // and fast so it's okay to keep calling it.
  763. if (isset($plugin_definition['file'])) {
  764. // Plugins that are loaded from info files have the info file as
  765. // $plugin['file']. Don't try to run those.
  766. $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
  767. if (empty($info['info file'])) {
  768. require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
  769. }
  770. }
  771. $return = FALSE;
  772. if (!isset($plugin_definition[$class_name])) {
  773. return;
  774. }
  775. else if (is_string($plugin_definition[$class_name])) {
  776. // Plugin uses the string form shorthand.
  777. $return = $plugin_definition[$class_name];
  778. }
  779. else if (isset($plugin_definition[$class_name]['class'])) {
  780. // Plugin uses the verbose array form.
  781. $return = $plugin_definition[$class_name]['class'];
  782. }
  783. // @todo consider adding an else {watchdog(...)} here
  784. return ($return && class_exists($return)) ? $return : NULL;
  785. }
  786. /**
  787. * Load a plugin and get a class name from it, returning success only if the
  788. * class exists.
  789. *
  790. * @param $module
  791. * The module that owns the plugin type.
  792. * @param $type
  793. * The type of plugin.
  794. * @param $id
  795. * The id of the specific plugin to load.
  796. * @param $class_name
  797. * The identifier of the class. For example, 'handler'.
  798. *
  799. * @return
  800. * The actual name of the class to call, or NULL if the class does not exist.
  801. */
  802. function ctools_plugin_load_class($module, $type, $id, $class_name) {
  803. $plugin = ctools_get_plugins($module, $type, $id);
  804. return ctools_plugin_get_class($plugin, $class_name);
  805. }
  806. /**
  807. * Sort callback for sorting plugins naturally.
  808. *
  809. * Sort first by weight, then by title.
  810. */
  811. function ctools_plugin_sort($a, $b) {
  812. if (is_object($a)) {
  813. $a = (array) $a;
  814. }
  815. if (is_object($b)) {
  816. $b = (array) $b;
  817. }
  818. if (empty($a['weight'])) {
  819. $a['weight'] = 0;
  820. }
  821. if (empty($b['weight'])) {
  822. $b['weight'] = 0;
  823. }
  824. if ($a['weight'] == $b['weight']) {
  825. return strnatcmp(strtolower($a['title']), strtolower($b['title']));
  826. }
  827. return ($a['weight'] < $b['weight']) ? -1 : 1;
  828. }