plugins.inc 32 KB

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