plugins.inc 33 KB

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