ctools.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. <?php
  2. /**
  3. * @file
  4. * CTools primary module file.
  5. *
  6. * Most of the CTools tools are in their own .inc files. This contains
  7. * nothing more than a few convenience functions and some hooks that
  8. * must be implemented in the module file.
  9. */
  10. define('CTOOLS_API_VERSION', '2.0.8');
  11. /**
  12. * The current working ctools version.
  13. *
  14. * In a release, it should be 7.x-1.x, which should match what drush make will
  15. * create. In a dev format, it should be 7.x-1.(x+1)-dev, which will allow
  16. * modules depending on new features in ctools to depend on ctools > 7.x-1.x.
  17. *
  18. * To define a specific version of CTools as a dependency for another module,
  19. * simply include a dependency line in that module's info file, e.g.:
  20. * ; Requires CTools v7.x-1.4 or newer.
  21. * dependencies[] = ctools (>=1.4)
  22. */
  23. define('CTOOLS_MODULE_VERSION', '7.x-1.6');
  24. /**
  25. * Test the CTools API version.
  26. *
  27. * This function can always be used to safely test if CTools has the minimum
  28. * API version that your module can use. It can also try to protect you from
  29. * running if the CTools API version is too new, but if you do that you need
  30. * to be very quick about watching CTools API releases and release new versions
  31. * of your software as soon as the new release is made, or people might end up
  32. * updating CTools and having your module shut down without any recourse.
  33. *
  34. * It is recommended that every hook of your module that might use CTools or
  35. * might lead to a use of CTools be guarded like this:
  36. *
  37. * @code
  38. * if (!module_invoke('ctools', 'api_version', '1.0')) {
  39. * return;
  40. * }
  41. * @endcode
  42. *
  43. * Note that some hooks such as _menu() or _theme() must return an array().
  44. *
  45. * You can use it in your hook_requirements to report this error condition
  46. * like this:
  47. *
  48. * @code
  49. * define('MODULENAME_MINIMUM_CTOOLS_API_VERSION', '1.0');
  50. * define('MODULENAME_MAXIMUM_CTOOLS_API_VERSION', '1.1');
  51. *
  52. * function MODULENAME_requirements($phase) {
  53. * $requirements = array();
  54. * if (!module_invoke('ctools', 'api_version', MODULENAME_MINIMUM_CTOOLS_API_VERSION, MODULENAME_MAXIMUM_CTOOLS_API_VERSION)) {
  55. * $requirements['MODULENAME_ctools'] = array(
  56. * 'title' => $t('MODULENAME required Chaos Tool Suite (CTools) API Version'),
  57. * 'value' => t('Between @a and @b', array('@a' => MODULENAME_MINIMUM_CTOOLS_API_VERSION, '@b' => MODULENAME_MAXIMUM_CTOOLS_API_VERSION)),
  58. * 'severity' => REQUIREMENT_ERROR,
  59. * );
  60. * }
  61. * return $requirements;
  62. * }
  63. * @endcode
  64. *
  65. * Please note that the version is a string, not an floating point number.
  66. * This will matter once CTools reaches version 1.10.
  67. *
  68. * A CTools API changes history will be kept in API.txt. Not every new
  69. * version of CTools will necessarily update the API version.
  70. * @param $minimum
  71. * The minimum version of CTools necessary for your software to run with it.
  72. * @param $maximum
  73. * The maximum version of CTools allowed for your software to run with it.
  74. */
  75. function ctools_api_version($minimum, $maximum = NULL) {
  76. if (version_compare(CTOOLS_API_VERSION, $minimum, '<')) {
  77. return FALSE;
  78. }
  79. if (isset($maximum) && version_compare(CTOOLS_API_VERSION, $maximum, '>')) {
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84. // -----------------------------------------------------------------------
  85. // General utility functions
  86. /**
  87. * Include .inc files as necessary.
  88. *
  89. * This fuction is helpful for including .inc files for your module. The
  90. * general case is including ctools funcitonality like this:
  91. *
  92. * @code
  93. * ctools_include('plugins');
  94. * @endcode
  95. *
  96. * Similar funcitonality can be used for other modules by providing the $module
  97. * and $dir arguments like this:
  98. *
  99. * @code
  100. * // include mymodule/includes/import.inc
  101. * ctools_include('import', 'mymodule');
  102. * // include mymodule/plugins/foobar.inc
  103. * ctools_include('foobar', 'mymodule', 'plugins');
  104. * @endcode
  105. *
  106. * @param $file
  107. * The base file name to be included.
  108. * @param $module
  109. * Optional module containing the include.
  110. * @param $dir
  111. * Optional subdirectory containing the include file.
  112. */
  113. function ctools_include($file, $module = 'ctools', $dir = 'includes') {
  114. static $used = array();
  115. $dir = '/' . ($dir ? $dir . '/' : '');
  116. if (!isset($used[$module][$dir][$file])) {
  117. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "$dir$file.inc";
  118. $used[$module][$dir][$file] = TRUE;
  119. }
  120. }
  121. /**
  122. * Include .inc files in a form context.
  123. *
  124. * This is a variant of ctools_include that will save information in the
  125. * the form_state so that cached forms will properly include things.
  126. */
  127. function ctools_form_include(&$form_state, $file, $module = 'ctools', $dir = 'includes') {
  128. if (!isset($form_state['build_info']['args'])) {
  129. $form_state['build_info']['args'] = array();
  130. }
  131. $dir = '/' . ($dir ? $dir . '/' : '');
  132. form_load_include($form_state, 'inc', $module, $dir . $file);
  133. }
  134. /**
  135. * Add an arbitrary path to the $form_state so it can work with form cache.
  136. *
  137. * module_load_include uses an unfortunately annoying syntax to work, making it
  138. * difficult to translate the more simple $path + $file syntax.
  139. */
  140. function ctools_form_include_file(&$form_state, $filename) {
  141. if (!isset($form_state['build_info']['args'])) {
  142. $form_state['build_info']['args'] = array();
  143. }
  144. // Now add this to the build info files so that AJAX requests will know to load it.
  145. $form_state['build_info']['files']["$filename"] = $filename;
  146. require_once DRUPAL_ROOT . '/' . $filename;
  147. }
  148. /**
  149. * Provide the proper path to an image as necessary.
  150. *
  151. * This helper function is used by ctools but can also be used in other
  152. * modules in the same way as explained in the comments of ctools_include.
  153. *
  154. * @param $image
  155. * The base file name (with extension) of the image to be included.
  156. * @param $module
  157. * Optional module containing the include.
  158. * @param $dir
  159. * Optional subdirectory containing the include file.
  160. */
  161. function ctools_image_path($image, $module = 'ctools', $dir = 'images') {
  162. return drupal_get_path('module', $module) . "/$dir/" . $image;
  163. }
  164. /**
  165. * Include css files as necessary.
  166. *
  167. * This helper function is used by ctools but can also be used in other
  168. * modules in the same way as explained in the comments of ctools_include.
  169. *
  170. * @param $file
  171. * The base file name to be included.
  172. * @param $module
  173. * Optional module containing the include.
  174. * @param $dir
  175. * Optional subdirectory containing the include file.
  176. */
  177. function ctools_add_css($file, $module = 'ctools', $dir = 'css') {
  178. drupal_add_css(drupal_get_path('module', $module) . "/$dir/$file.css");
  179. }
  180. /**
  181. * Format a css file name for use with $form['#attached']['css'].
  182. *
  183. * This helper function is used by ctools but can also be used in other
  184. * modules in the same way as explained in the comments of ctools_include.
  185. *
  186. * @code
  187. * $form['#attached']['css'] = array(ctools_attach_css('collapsible-div'));
  188. * $form['#attached']['css'][ctools_attach_css('collapsible-div')] = array('preprocess' => FALSE);
  189. * @endcode
  190. *
  191. * @param $file
  192. * The base file name to be included.
  193. * @param $module
  194. * Optional module containing the include.
  195. * @param $dir
  196. * Optional subdirectory containing the include file.
  197. */
  198. function ctools_attach_css($file, $module = 'ctools', $dir = 'css') {
  199. return drupal_get_path('module', $module) . "/$dir/$file.css";
  200. }
  201. /**
  202. * Include js files as necessary.
  203. *
  204. * This helper function is used by ctools but can also be used in other
  205. * modules in the same way as explained in the comments of ctools_include.
  206. *
  207. * @param $file
  208. * The base file name to be included.
  209. * @param $module
  210. * Optional module containing the include.
  211. * @param $dir
  212. * Optional subdirectory containing the include file.
  213. */
  214. function ctools_add_js($file, $module = 'ctools', $dir = 'js') {
  215. drupal_add_js(drupal_get_path('module', $module) . "/$dir/$file.js");
  216. }
  217. /**
  218. * Format a javascript file name for use with $form['#attached']['js'].
  219. *
  220. * This helper function is used by ctools but can also be used in other
  221. * modules in the same way as explained in the comments of ctools_include.
  222. *
  223. * @code
  224. * $form['#attached']['js'] = array(ctools_attach_js('auto-submit'));
  225. * @endcode
  226. *
  227. * @param $file
  228. * The base file name to be included.
  229. * @param $module
  230. * Optional module containing the include.
  231. * @param $dir
  232. * Optional subdirectory containing the include file.
  233. */
  234. function ctools_attach_js($file, $module = 'ctools', $dir = 'js') {
  235. return drupal_get_path('module', $module) . "/$dir/$file.js";
  236. }
  237. /**
  238. * Get a list of roles in the system.
  239. *
  240. * @return
  241. * An array of role names keyed by role ID.
  242. *
  243. * @deprecated
  244. * user_roles() should be used instead.
  245. */
  246. function ctools_get_roles() {
  247. return user_roles();
  248. }
  249. /*
  250. * Break x,y,z and x+y+z into an array. Numeric only.
  251. *
  252. * @param $str
  253. * The string to parse.
  254. *
  255. * @return $object
  256. * An object containing
  257. * - operator: Either 'and' or 'or'
  258. * - value: An array of numeric values.
  259. */
  260. function ctools_break_phrase($str) {
  261. $object = new stdClass();
  262. if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {
  263. // The '+' character in a query string may be parsed as ' '.
  264. $object->operator = 'or';
  265. $object->value = preg_split('/[+ ]/', $str);
  266. }
  267. else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
  268. $object->operator = 'and';
  269. $object->value = explode(',', $str);
  270. }
  271. // Keep an 'error' value if invalid strings were given.
  272. if (!empty($str) && (empty($object->value) || !is_array($object->value))) {
  273. $object->value = array(-1);
  274. $object->invalid_input = TRUE;
  275. return $object;
  276. }
  277. if (empty($object->value)) {
  278. $object->value = array();
  279. }
  280. // Doubly ensure that all values are numeric only.
  281. foreach ($object->value as $id => $value) {
  282. $object->value[$id] = intval($value);
  283. }
  284. return $object;
  285. }
  286. /**
  287. * Set a token/value pair to be replaced later in the request, specifically in
  288. * ctools_page_token_processing().
  289. *
  290. * @param $token
  291. * The token to be replaced later, during page rendering. This should
  292. * ideally be a string inside of an HTML comment, so that if there is
  293. * no replacement, the token will not render on the page.
  294. * @param $type
  295. * The type of the token. Can be either 'variable', which will pull data
  296. * directly from the page variables
  297. * @param $argument
  298. * If $type == 'variable' then argument should be the key to fetch from
  299. * the $variables. If $type == 'callback' then it should either be the
  300. * callback, or an array that will be sent to call_user_func_array().
  301. *
  302. * @return
  303. * A array of token/variable names to be replaced.
  304. */
  305. function ctools_set_page_token($token = NULL, $type = NULL, $argument = NULL) {
  306. static $tokens = array();
  307. if (isset($token)) {
  308. $tokens[$token] = array($type, $argument);
  309. }
  310. return $tokens;
  311. }
  312. /**
  313. * Easily set a token from the page variables.
  314. *
  315. * This function can be used like this:
  316. * $token = ctools_set_variable_token('tabs');
  317. *
  318. * $token will then be a simple replacement for the 'tabs' about of the
  319. * variables available in the page template.
  320. */
  321. function ctools_set_variable_token($token) {
  322. $string = '<!-- ctools-page-' . $token . ' -->';
  323. ctools_set_page_token($string, 'variable', $token);
  324. return $string;
  325. }
  326. /**
  327. * Easily set a token from the page variables.
  328. *
  329. * This function can be used like this:
  330. * $token = ctools_set_variable_token('id', 'mymodule_myfunction');
  331. */
  332. function ctools_set_callback_token($token, $callback) {
  333. // If the callback uses arguments they are considered in the token.
  334. if (is_array($callback)) {
  335. $token .= '-' . md5(serialize($callback));
  336. }
  337. $string = '<!-- ctools-page-' . $token . ' -->';
  338. ctools_set_page_token($string, 'callback', $callback);
  339. return $string;
  340. }
  341. /**
  342. * Tell CTools that sidebar blocks should not be rendered.
  343. *
  344. * It is often desirable to not display sidebars when rendering a page,
  345. * particularly when using Panels. This informs CTools to alter out any
  346. * sidebar regions during block render.
  347. */
  348. function ctools_set_no_blocks($blocks = FALSE) {
  349. $status = &drupal_static(__FUNCTION__, TRUE);
  350. $status = $blocks;
  351. }
  352. /**
  353. * Wrapper function to create UUIDs via ctools, falls back on UUID module
  354. * if it is enabled. This code is a copy of uuid.inc from the uuid module.
  355. * @see http://php.net/uniqid#65879
  356. */
  357. function ctools_uuid_generate() {
  358. if (!module_exists('uuid')) {
  359. ctools_include('uuid');
  360. $callback = drupal_static(__FUNCTION__);
  361. if (empty($callback)) {
  362. if (function_exists('uuid_create') && !function_exists('uuid_make')) {
  363. $callback = '_ctools_uuid_generate_pecl';
  364. }
  365. elseif (function_exists('com_create_guid')) {
  366. $callback = '_ctools_uuid_generate_com';
  367. }
  368. else {
  369. $callback = '_ctools_uuid_generate_php';
  370. }
  371. }
  372. return $callback();
  373. }
  374. else {
  375. return uuid_generate();
  376. }
  377. }
  378. /**
  379. * Check that a string appears to be in the format of a UUID.
  380. * @see http://drupal.org/project/uuid
  381. *
  382. * @param $uuid
  383. * The string to test.
  384. *
  385. * @return
  386. * TRUE if the string is well formed.
  387. */
  388. function ctools_uuid_is_valid($uuid = '') {
  389. if (empty($uuid)) {
  390. return FALSE;
  391. }
  392. if (function_exists('uuid_is_valid') || module_exists('uuid')) {
  393. return uuid_is_valid($uuid);
  394. }
  395. else {
  396. ctools_include('uuid');
  397. return uuid_is_valid($uuid);
  398. }
  399. }
  400. /**
  401. * Add an array of classes to the body.
  402. *
  403. * @param mixed $classes
  404. * A string or an array of class strings to add.
  405. * @param string $hook
  406. * The theme hook to add the class to. The default is 'html' which will
  407. * affect the body tag.
  408. */
  409. function ctools_class_add($classes, $hook = 'html') {
  410. if (!is_array($classes)) {
  411. $classes = array($classes);
  412. }
  413. $static = &drupal_static('ctools_process_classes', array());
  414. if (!isset($static[$hook]['add'])) {
  415. $static[$hook]['add'] = array();
  416. }
  417. foreach ($classes as $class) {
  418. $static[$hook]['add'][] = $class;
  419. }
  420. }
  421. /**
  422. * Remove an array of classes from the body.
  423. *
  424. * @param mixed $classes
  425. * A string or an array of class strings to remove.
  426. * @param string $hook
  427. * The theme hook to remove the class from. The default is 'html' which will
  428. * affect the body tag.
  429. */
  430. function ctools_class_remove($classes, $hook = 'html') {
  431. if (!is_array($classes)) {
  432. $classes = array($classes);
  433. }
  434. $static = &drupal_static('ctools_process_classes', array());
  435. if (!isset($static[$hook]['remove'])) {
  436. $static[$hook]['remove'] = array();
  437. }
  438. foreach ($classes as $class) {
  439. $static[$hook]['remove'][] = $class;
  440. }
  441. }
  442. // -----------------------------------------------------------------------
  443. // Drupal core hooks
  444. /**
  445. * Implement hook_init to keep our global CSS at the ready.
  446. */
  447. function ctools_init() {
  448. ctools_add_css('ctools');
  449. // If we are sure that CTools' AJAX is in use, change the error handling.
  450. if (!empty($_REQUEST['ctools_ajax'])) {
  451. ini_set('display_errors', 0);
  452. register_shutdown_function('ctools_shutdown_handler');
  453. }
  454. // Clear plugin cache on the module page submit.
  455. if ($_GET['q'] == 'admin/modules/list/confirm' && !empty($_POST)) {
  456. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  457. }
  458. }
  459. /**
  460. * Shutdown handler used during ajax operations to help catch fatal errors.
  461. */
  462. function ctools_shutdown_handler() {
  463. if (function_exists('error_get_last') AND ($error = error_get_last())) {
  464. switch ($error['type']) {
  465. case E_ERROR:
  466. case E_CORE_ERROR:
  467. case E_COMPILE_ERROR:
  468. case E_USER_ERROR:
  469. // Do this manually because including files here is dangerous.
  470. $commands = array(
  471. array(
  472. 'command' => 'alert',
  473. 'title' => t('Error'),
  474. 'text' => t('Unable to complete operation. Fatal error in @file on line @line: @message', array(
  475. '@file' => $error['file'],
  476. '@line' => $error['line'],
  477. '@message' => $error['message'],
  478. )),
  479. ),
  480. );
  481. // Change the status code so that the client will read the AJAX returned.
  482. header('HTTP/1.1 200 OK');
  483. drupal_json($commands);
  484. }
  485. }
  486. }
  487. /**
  488. * Implements hook_theme().
  489. */
  490. function ctools_theme() {
  491. ctools_include('utility');
  492. $items = array();
  493. ctools_passthrough('ctools', 'theme', $items);
  494. return $items;
  495. }
  496. /**
  497. * Implements hook_menu().
  498. */
  499. function ctools_menu() {
  500. ctools_include('utility');
  501. $items = array();
  502. ctools_passthrough('ctools', 'menu', $items);
  503. return $items;
  504. }
  505. /**
  506. * Implements hook_permission().
  507. */
  508. function ctools_permission() {
  509. return array(
  510. 'use ctools import' => array(
  511. 'title' => t('Use CTools importer'),
  512. 'description' => t('The import functionality allows users to execute arbitrary PHP code, so extreme caution must be taken.'),
  513. 'restrict access' => TRUE,
  514. ),
  515. );
  516. }
  517. /**
  518. * Implementation of hook_cron. Clean up old caches.
  519. */
  520. function ctools_cron() {
  521. ctools_include('utility');
  522. $items = array();
  523. ctools_passthrough('ctools', 'cron', $items);
  524. }
  525. /**
  526. * Implements hook_flush_caches().
  527. */
  528. function ctools_flush_caches() {
  529. // Only return the CSS cache bin if it has been activated, to avoid
  530. // drupal_flush_all_caches() from trying to truncate a non-existing table.
  531. return variable_get('cache_class_cache_ctools_css', FALSE) ? array('cache_ctools_css') : array();
  532. }
  533. /**
  534. * Implements hook_element_info_alter().
  535. *
  536. */
  537. function ctools_element_info_alter(&$type) {
  538. ctools_include('dependent');
  539. ctools_dependent_element_info_alter($type);
  540. }
  541. /**
  542. * Implementation of hook_file_download()
  543. *
  544. * When using the private file system, we have to let Drupal know it's ok to
  545. * download CSS and image files from our temporary directory.
  546. */
  547. function ctools_file_download($filepath) {
  548. if (strpos($filepath, 'ctools') === 0) {
  549. $mime = file_get_mimetype($filepath);
  550. // For safety's sake, we allow only text and images.
  551. if (strpos($mime, 'text') === 0 || strpos($mime, 'image') === 0) {
  552. return array('Content-type:' . $mime);
  553. }
  554. }
  555. }
  556. /**
  557. * Implements hook_registry_files_alter().
  558. *
  559. * Alter the registry of files to automagically include all classes in
  560. * class-based plugins.
  561. */
  562. function ctools_registry_files_alter(&$files, $indexed_modules) {
  563. ctools_include('registry');
  564. return _ctools_registry_files_alter($files, $indexed_modules);
  565. }
  566. // -----------------------------------------------------------------------
  567. // CTools hook implementations.
  568. /**
  569. * Implementation of hook_ctools_plugin_directory() to let the system know
  570. * where all our own plugins are.
  571. */
  572. function ctools_ctools_plugin_directory($owner, $plugin_type) {
  573. if ($owner == 'ctools') {
  574. return 'plugins/' . $plugin_type;
  575. }
  576. }
  577. /**
  578. * Implements hook_ctools_plugin_type().
  579. */
  580. function ctools_ctools_plugin_type() {
  581. ctools_include('utility');
  582. $items = array();
  583. // Add all the plugins that have their own declaration space elsewhere.
  584. ctools_passthrough('ctools', 'plugin-type', $items);
  585. return $items;
  586. }
  587. // -----------------------------------------------------------------------
  588. // Drupal theme preprocess hooks that must be in the .module file.
  589. /**
  590. * A theme preprocess function to automatically allow panels-based node
  591. * templates based upon input when the panel was configured.
  592. */
  593. function ctools_preprocess_node(&$vars) {
  594. // The 'ctools_template_identifier' attribute of the node is added when the pane is
  595. // rendered.
  596. if (!empty($vars['node']->ctools_template_identifier)) {
  597. $vars['ctools_template_identifier'] = check_plain($vars['node']->ctools_template_identifier);
  598. $vars['theme_hook_suggestions'][] = 'node__panel__' . check_plain($vars['node']->ctools_template_identifier);
  599. }
  600. }
  601. /**
  602. * Implements hook_page_alter().
  603. *
  604. * Last ditch attempt to remove sidebar regions if the "no blocks"
  605. * functionality has been activated.
  606. *
  607. * @see ctools_block_list_alter().
  608. */
  609. function ctools_page_alter(&$page) {
  610. $check = drupal_static('ctools_set_no_blocks', TRUE);
  611. if (!$check) {
  612. foreach ($page as $region_id => $region) {
  613. // @todo -- possibly we can set configuration for this so that users can
  614. // specify which blocks will not get rendered.
  615. if (strpos($region_id, 'sidebar') !== FALSE) {
  616. unset($page[$region_id]);
  617. }
  618. }
  619. }
  620. $page['#post_render'][] = 'ctools_page_token_processing';
  621. }
  622. /**
  623. * A theme post_render callback to allow content type plugins to use page
  624. * template variables which are not yet available when the content type is
  625. * rendered.
  626. */
  627. function ctools_page_token_processing($children, $elements) {
  628. $tokens = ctools_set_page_token();
  629. if (!empty($tokens)) {
  630. foreach ($tokens as $token => $key) {
  631. list($type, $argument) = $key;
  632. switch ($type) {
  633. case 'variable':
  634. $tokens[$token] = isset($elements[$argument]) ? $elements[$argument] : '';
  635. break;
  636. case 'callback':
  637. if (is_string($argument) && function_exists($argument)) {
  638. $tokens[$token] = $argument($elements);
  639. }
  640. if (is_array($argument) && function_exists($argument[0])) {
  641. $function = array_shift($argument);
  642. $argument = array_merge(array(&$elements), $argument);
  643. $tokens[$token] = call_user_func_array($function, $argument);
  644. }
  645. break;
  646. }
  647. }
  648. $children = strtr($children, $tokens);
  649. }
  650. return $children;
  651. }
  652. /**
  653. * Implements hook_process().
  654. *
  655. * Add and remove CSS classes from the variables array. We use process so that
  656. * we alter anything added in the preprocess hooks.
  657. */
  658. function ctools_process(&$variables, $hook) {
  659. if (!isset($variables['classes'])) {
  660. return;
  661. }
  662. $classes = drupal_static('ctools_process_classes', array());
  663. // Process the classses to add.
  664. if (!empty($classes[$hook]['add'])) {
  665. $add_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['add']);
  666. $variables['classes_array'] = array_unique(array_merge($variables['classes_array'], $add_classes));
  667. }
  668. // Process the classes to remove.
  669. if (!empty($classes[$hook]['remove'])) {
  670. $remove_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['remove']);
  671. $variables['classes_array'] = array_diff($variables['classes_array'], $remove_classes);
  672. }
  673. // Since this runs after template_process(), we need to re-implode the
  674. // classes array.
  675. $variables['classes'] = implode(' ', $variables['classes_array']);
  676. }
  677. // -----------------------------------------------------------------------
  678. // Menu callbacks that must be in the .module file.
  679. /**
  680. * Determine if the current user has access via a plugin.
  681. *
  682. * This function is meant to be embedded in the Drupal menu system, and
  683. * therefore is in the .module file since sub files can't be loaded, and
  684. * takes arguments a little bit more haphazardly than ctools_access().
  685. *
  686. * @param $access
  687. * An access control array which contains the following information:
  688. * - 'logic': and or or. Whether all tests must pass or one must pass.
  689. * - 'plugins': An array of access plugins. Each contains:
  690. * - - 'name': The name of the plugin
  691. * - - 'settings': The settings from the plugin UI.
  692. * - - 'context': Which context to use.
  693. * @param ...
  694. * zero or more context arguments generated from argument plugins. These
  695. * contexts must have an 'id' attached to them so that they can be
  696. * properly associated. The argument plugin system should set this, but
  697. * if the context is coming from elsewhere it will need to be set manually.
  698. *
  699. * @return
  700. * TRUE if access is granted, false if otherwise.
  701. */
  702. function ctools_access_menu($access) {
  703. // Short circuit everything if there are no access tests.
  704. if (empty($access['plugins'])) {
  705. return TRUE;
  706. }
  707. $contexts = array();
  708. foreach (func_get_args() as $arg) {
  709. if (is_object($arg) && get_class($arg) == 'ctools_context') {
  710. $contexts[$arg->id] = $arg;
  711. }
  712. }
  713. ctools_include('context');
  714. return ctools_access($access, $contexts);
  715. }
  716. /**
  717. * Determine if the current user has access via checks to multiple different
  718. * permissions.
  719. *
  720. * This function is a thin wrapper around user_access that allows multiple
  721. * permissions to be easily designated for use on, for example, a menu callback.
  722. *
  723. * @param ...
  724. * An indexed array of zero or more permission strings to be checked by
  725. * user_access().
  726. *
  727. * @return
  728. * Iff all checks pass will this function return TRUE. If an invalid argument
  729. * is passed (e.g., not a string), this function errs on the safe said and
  730. * returns FALSE.
  731. */
  732. function ctools_access_multiperm() {
  733. foreach (func_get_args() as $arg) {
  734. if (!is_string($arg) || !user_access($arg)) {
  735. return FALSE;
  736. }
  737. }
  738. return TRUE;
  739. }
  740. /**
  741. * Check to see if the incoming menu item is js capable or not.
  742. *
  743. * This can be used as %ctools_js as part of a path in hook menu. CTools
  744. * ajax functions will automatically change the phrase 'nojs' to 'ajax'
  745. * when it attaches ajax to a link. This can be used to autodetect if
  746. * that happened.
  747. */
  748. function ctools_js_load($js) {
  749. if ($js == 'ajax') {
  750. return TRUE;
  751. }
  752. return 0;
  753. }
  754. /**
  755. * Provides the default value for %ctools_js.
  756. *
  757. * This allows drupal_valid_path() to work with %ctools_js.
  758. */
  759. function ctools_js_to_arg($arg) {
  760. return empty($arg) || $arg == '%' ? 'nojs' : $arg;
  761. }
  762. /**
  763. * Menu _load hook.
  764. *
  765. * This function will be called to load an object as a replacement for
  766. * %ctools_export_ui in menu paths.
  767. */
  768. function ctools_export_ui_load($item_name, $plugin_name) {
  769. $return = &drupal_static(__FUNCTION__, FALSE);
  770. if (!$return) {
  771. ctools_include('export-ui');
  772. $plugin = ctools_get_export_ui($plugin_name);
  773. $handler = ctools_export_ui_get_handler($plugin);
  774. if ($handler) {
  775. return $handler->load_item($item_name);
  776. }
  777. }
  778. return $return;
  779. }
  780. // -----------------------------------------------------------------------
  781. // Caching callbacks on behalf of export-ui.
  782. /**
  783. * Menu access callback for various tasks of export-ui.
  784. */
  785. function ctools_export_ui_task_access($plugin_name, $op, $item = NULL) {
  786. ctools_include('export-ui');
  787. $plugin = ctools_get_export_ui($plugin_name);
  788. $handler = ctools_export_ui_get_handler($plugin);
  789. if ($handler) {
  790. return $handler->access($op, $item);
  791. }
  792. // Deny access if the handler cannot be found.
  793. return FALSE;
  794. }
  795. /**
  796. * Callback for access control ajax form on behalf of export ui.
  797. *
  798. * Returns the cached access config and contexts used.
  799. * Note that this is assuming that access will be in $item->access -- if it
  800. * is not, an export UI plugin will have to make its own callbacks.
  801. */
  802. function ctools_export_ui_ctools_access_get($argument) {
  803. ctools_include('export-ui');
  804. list($plugin_name, $key) = explode(':', $argument, 2);
  805. $plugin = ctools_get_export_ui($plugin_name);
  806. $handler = ctools_export_ui_get_handler($plugin);
  807. if ($handler) {
  808. ctools_include('context');
  809. $item = $handler->edit_cache_get($key);
  810. if (!$item) {
  811. $item = ctools_export_crud_load($handler->plugin['schema'], $key);
  812. }
  813. $contexts = ctools_context_load_contexts($item);
  814. return array($item->access, $contexts);
  815. }
  816. }
  817. /**
  818. * Callback for access control ajax form on behalf of export ui
  819. *
  820. * Returns the cached access config and contexts used.
  821. * Note that this is assuming that access will be in $item->access -- if it
  822. * is not, an export UI plugin will have to make its own callbacks.
  823. */
  824. function ctools_export_ui_ctools_access_set($argument, $access) {
  825. ctools_include('export-ui');
  826. list($plugin_name, $key) = explode(':', $argument, 2);
  827. $plugin = ctools_get_export_ui($plugin_name);
  828. $handler = ctools_export_ui_get_handler($plugin);
  829. if ($handler) {
  830. ctools_include('context');
  831. $item = $handler->edit_cache_get($key);
  832. if (!$item) {
  833. $item = ctools_export_crud_load($handler->plugin['schema'], $key);
  834. }
  835. $item->access = $access;
  836. return $handler->edit_cache_set_key($item, $key);
  837. }
  838. }
  839. /**
  840. * Implements hook_menu_local_tasks_alter().
  841. */
  842. function ctools_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  843. ctools_include('menu');
  844. _ctools_menu_add_dynamic_items($data, $router_item, $root_path);
  845. }
  846. /**
  847. * Implement hook_block_list_alter() to potentially remove blocks.
  848. *
  849. * This exists in order to replicate Drupal 6's "no blocks" functionality.
  850. */
  851. function ctools_block_list_alter(&$blocks) {
  852. $check = drupal_static('ctools_set_no_blocks', TRUE);
  853. if (!$check) {
  854. foreach ($blocks as $block_id => $block) {
  855. // @todo -- possibly we can set configuration for this so that users can
  856. // specify which blocks will not get rendered.
  857. if (strpos($block->region, 'sidebar') !== FALSE) {
  858. unset($blocks[$block_id]);
  859. }
  860. }
  861. }
  862. }
  863. /**
  864. * Implements hook_modules_enabled().
  865. *
  866. * Clear caches for detecting new plugins.
  867. */
  868. function ctools_modules_enabled($modules) {
  869. ctools_include('plugins');
  870. ctools_get_plugins_reset();
  871. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  872. }
  873. /**
  874. * Implements hook_modules_disabled().
  875. *
  876. * Clear caches for removing disabled plugins.
  877. */
  878. function ctools_modules_disabled($modules) {
  879. ctools_include('plugins');
  880. ctools_get_plugins_reset();
  881. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  882. }
  883. /**
  884. * Menu theme callback.
  885. *
  886. * This simply ensures that Panels ajax calls are rendered in the same
  887. * theme as the original page to prevent .css file confusion.
  888. *
  889. * To use this, set this as the theme callback on AJAX related menu
  890. * items. Since the ajax page state won't be sent during ajax requests,
  891. * it should be safe to use even if ajax isn't invoked.
  892. */
  893. function ctools_ajax_theme_callback() {
  894. if (!empty($_POST['ajax_page_state']['theme'])) {
  895. return $_POST['ajax_page_state']['theme'];
  896. }
  897. }
  898. /**
  899. * Implements hook_ctools_entity_context_alter().
  900. */
  901. function ctools_ctools_entity_context_alter(&$plugin, &$entity, $plugin_id) {
  902. ctools_include('context');
  903. switch ($plugin_id) {
  904. case 'entity_id:taxonomy_term':
  905. $plugin['no ui'] = TRUE;
  906. break;
  907. case 'entity:user':
  908. $plugin = ctools_get_context('user');
  909. unset($plugin['no ui']);
  910. unset($plugin['no required context ui']);
  911. break;
  912. }
  913. // Apply restrictions on taxonomy term reverse relationships whose
  914. // restrictions are in the settings on the field.
  915. if (!empty($plugin['parent']) &&
  916. $plugin['parent'] == 'entity_from_field' &&
  917. !empty($plugin['reverse']) &&
  918. $plugin['to entity'] == 'taxonomy_term') {
  919. $field = field_info_field($plugin['field name']);
  920. if (isset($field['settings']['allowed_values'][0]['vocabulary'])) {
  921. $plugin['required context']->restrictions = array('vocabulary' => array($field['settings']['allowed_values'][0]['vocabulary']));
  922. }
  923. }
  924. }