ctools.module 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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.9');
  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.11');
  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. // FAPI hooks that must be in the .module file.
  568. /**
  569. * Alter the comment form to get a little more control over it.
  570. */
  571. function ctools_form_comment_form_alter(&$form, &$form_state) {
  572. if (!empty($form_state['ctools comment alter'])) {
  573. // Force the form to post back to wherever we are.
  574. $form['#action'] = url($_GET['q'], array('fragment' => 'comment-form'));
  575. if (empty($form['#submit'])) {
  576. $form['#submit'] = array('comment_form_submit');
  577. }
  578. $form['#submit'][] = 'ctools_node_comment_form_submit';
  579. }
  580. }
  581. function ctools_node_comment_form_submit(&$form, &$form_state) {
  582. $form_state['redirect'][0] = $_GET['q'];
  583. }
  584. // -----------------------------------------------------------------------
  585. // CTools hook implementations.
  586. /**
  587. * Implementation of hook_ctools_plugin_directory() to let the system know
  588. * where all our own plugins are.
  589. */
  590. function ctools_ctools_plugin_directory($owner, $plugin_type) {
  591. if ($owner == 'ctools') {
  592. return 'plugins/' . $plugin_type;
  593. }
  594. }
  595. /**
  596. * Implements hook_ctools_plugin_type().
  597. */
  598. function ctools_ctools_plugin_type() {
  599. ctools_include('utility');
  600. $items = array();
  601. // Add all the plugins that have their own declaration space elsewhere.
  602. ctools_passthrough('ctools', 'plugin-type', $items);
  603. return $items;
  604. }
  605. // -----------------------------------------------------------------------
  606. // Drupal theme preprocess hooks that must be in the .module file.
  607. /**
  608. * A theme preprocess function to automatically allow panels-based node
  609. * templates based upon input when the panel was configured.
  610. */
  611. function ctools_preprocess_node(&$vars) {
  612. // The 'ctools_template_identifier' attribute of the node is added when the pane is
  613. // rendered.
  614. if (!empty($vars['node']->ctools_template_identifier)) {
  615. $vars['ctools_template_identifier'] = check_plain($vars['node']->ctools_template_identifier);
  616. $vars['theme_hook_suggestions'][] = 'node__panel__' . check_plain($vars['node']->ctools_template_identifier);
  617. }
  618. }
  619. /**
  620. * Implements hook_page_alter().
  621. *
  622. * Last ditch attempt to remove sidebar regions if the "no blocks"
  623. * functionality has been activated.
  624. *
  625. * @see ctools_block_list_alter().
  626. */
  627. function ctools_page_alter(&$page) {
  628. $check = drupal_static('ctools_set_no_blocks', TRUE);
  629. if (!$check) {
  630. foreach ($page as $region_id => $region) {
  631. // @todo -- possibly we can set configuration for this so that users can
  632. // specify which blocks will not get rendered.
  633. if (strpos($region_id, 'sidebar') !== FALSE) {
  634. unset($page[$region_id]);
  635. }
  636. }
  637. }
  638. $page['#post_render'][] = 'ctools_page_token_processing';
  639. }
  640. /**
  641. * A theme post_render callback to allow content type plugins to use page
  642. * template variables which are not yet available when the content type is
  643. * rendered.
  644. */
  645. function ctools_page_token_processing($children, $elements) {
  646. $tokens = ctools_set_page_token();
  647. if (!empty($tokens)) {
  648. foreach ($tokens as $token => $key) {
  649. list($type, $argument) = $key;
  650. switch ($type) {
  651. case 'variable':
  652. $tokens[$token] = isset($elements[$argument]) ? $elements[$argument] : '';
  653. break;
  654. case 'callback':
  655. if (is_string($argument) && function_exists($argument)) {
  656. $tokens[$token] = $argument($elements);
  657. }
  658. if (is_array($argument) && function_exists($argument[0])) {
  659. $function = array_shift($argument);
  660. $argument = array_merge(array(&$elements), $argument);
  661. $tokens[$token] = call_user_func_array($function, $argument);
  662. }
  663. break;
  664. }
  665. }
  666. $children = strtr($children, $tokens);
  667. }
  668. return $children;
  669. }
  670. /**
  671. * Implements hook_process().
  672. *
  673. * Add and remove CSS classes from the variables array. We use process so that
  674. * we alter anything added in the preprocess hooks.
  675. */
  676. function ctools_process(&$variables, $hook) {
  677. if (!isset($variables['classes'])) {
  678. return;
  679. }
  680. $classes = drupal_static('ctools_process_classes', array());
  681. // Process the classses to add.
  682. if (!empty($classes[$hook]['add'])) {
  683. $add_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['add']);
  684. $variables['classes_array'] = array_unique(array_merge($variables['classes_array'], $add_classes));
  685. }
  686. // Process the classes to remove.
  687. if (!empty($classes[$hook]['remove'])) {
  688. $remove_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['remove']);
  689. $variables['classes_array'] = array_diff($variables['classes_array'], $remove_classes);
  690. }
  691. // Update the classes within the attributes array to match the classes array
  692. if (isset($variables['attributes_array']['class'])) {
  693. $variables['attributes_array']['class'] = array_unique(array_merge($variables['classes_array'], $variables['attributes_array']['class']));
  694. $variables['attributes'] = $variables['attributes_array'] ? drupal_attributes($variables['attributes_array']) : '';
  695. }
  696. // Since this runs after template_process(), we need to re-implode the
  697. // classes array.
  698. $variables['classes'] = implode(' ', $variables['classes_array']);
  699. }
  700. // -----------------------------------------------------------------------
  701. // Menu callbacks that must be in the .module file.
  702. /**
  703. * Determine if the current user has access via a plugin.
  704. *
  705. * This function is meant to be embedded in the Drupal menu system, and
  706. * therefore is in the .module file since sub files can't be loaded, and
  707. * takes arguments a little bit more haphazardly than ctools_access().
  708. *
  709. * @param $access
  710. * An access control array which contains the following information:
  711. * - 'logic': and or or. Whether all tests must pass or one must pass.
  712. * - 'plugins': An array of access plugins. Each contains:
  713. * - - 'name': The name of the plugin
  714. * - - 'settings': The settings from the plugin UI.
  715. * - - 'context': Which context to use.
  716. * @param ...
  717. * zero or more context arguments generated from argument plugins. These
  718. * contexts must have an 'id' attached to them so that they can be
  719. * properly associated. The argument plugin system should set this, but
  720. * if the context is coming from elsewhere it will need to be set manually.
  721. *
  722. * @return
  723. * TRUE if access is granted, false if otherwise.
  724. */
  725. function ctools_access_menu($access) {
  726. // Short circuit everything if there are no access tests.
  727. if (empty($access['plugins'])) {
  728. return TRUE;
  729. }
  730. $contexts = array();
  731. foreach (func_get_args() as $arg) {
  732. if (is_object($arg) && get_class($arg) == 'ctools_context') {
  733. $contexts[$arg->id] = $arg;
  734. }
  735. }
  736. ctools_include('context');
  737. return ctools_access($access, $contexts);
  738. }
  739. /**
  740. * Determine if the current user has access via checks to multiple different
  741. * permissions.
  742. *
  743. * This function is a thin wrapper around user_access that allows multiple
  744. * permissions to be easily designated for use on, for example, a menu callback.
  745. *
  746. * @param ...
  747. * An indexed array of zero or more permission strings to be checked by
  748. * user_access().
  749. *
  750. * @return
  751. * Iff all checks pass will this function return TRUE. If an invalid argument
  752. * is passed (e.g., not a string), this function errs on the safe said and
  753. * returns FALSE.
  754. */
  755. function ctools_access_multiperm() {
  756. foreach (func_get_args() as $arg) {
  757. if (!is_string($arg) || !user_access($arg)) {
  758. return FALSE;
  759. }
  760. }
  761. return TRUE;
  762. }
  763. /**
  764. * Check to see if the incoming menu item is js capable or not.
  765. *
  766. * This can be used as %ctools_js as part of a path in hook menu. CTools
  767. * ajax functions will automatically change the phrase 'nojs' to 'ajax'
  768. * when it attaches ajax to a link. This can be used to autodetect if
  769. * that happened.
  770. */
  771. function ctools_js_load($js) {
  772. if ($js == 'ajax') {
  773. return TRUE;
  774. }
  775. return 0;
  776. }
  777. /**
  778. * Provides the default value for %ctools_js.
  779. *
  780. * This allows drupal_valid_path() to work with %ctools_js.
  781. */
  782. function ctools_js_to_arg($arg) {
  783. return empty($arg) || $arg == '%' ? 'nojs' : $arg;
  784. }
  785. /**
  786. * Menu _load hook.
  787. *
  788. * This function will be called to load an object as a replacement for
  789. * %ctools_export_ui in menu paths.
  790. */
  791. function ctools_export_ui_load($item_name, $plugin_name) {
  792. $return = &drupal_static(__FUNCTION__, FALSE);
  793. if (!$return) {
  794. ctools_include('export-ui');
  795. $plugin = ctools_get_export_ui($plugin_name);
  796. $handler = ctools_export_ui_get_handler($plugin);
  797. if ($handler) {
  798. return $handler->load_item($item_name);
  799. }
  800. }
  801. return $return;
  802. }
  803. // -----------------------------------------------------------------------
  804. // Caching callbacks on behalf of export-ui.
  805. /**
  806. * Menu access callback for various tasks of export-ui.
  807. */
  808. function ctools_export_ui_task_access($plugin_name, $op, $item = NULL) {
  809. ctools_include('export-ui');
  810. $plugin = ctools_get_export_ui($plugin_name);
  811. $handler = ctools_export_ui_get_handler($plugin);
  812. if ($handler) {
  813. return $handler->access($op, $item);
  814. }
  815. // Deny access if the handler cannot be found.
  816. return FALSE;
  817. }
  818. /**
  819. * Callback for access control ajax form on behalf of export ui.
  820. *
  821. * Returns the cached access config and contexts used.
  822. * Note that this is assuming that access will be in $item->access -- if it
  823. * is not, an export UI plugin will have to make its own callbacks.
  824. */
  825. function ctools_export_ui_ctools_access_get($argument) {
  826. ctools_include('export-ui');
  827. list($plugin_name, $key) = explode(':', $argument, 2);
  828. $plugin = ctools_get_export_ui($plugin_name);
  829. $handler = ctools_export_ui_get_handler($plugin);
  830. if ($handler) {
  831. ctools_include('context');
  832. $item = $handler->edit_cache_get($key);
  833. if (!$item) {
  834. $item = ctools_export_crud_load($handler->plugin['schema'], $key);
  835. }
  836. $contexts = ctools_context_load_contexts($item);
  837. return array($item->access, $contexts);
  838. }
  839. }
  840. /**
  841. * Callback for access control ajax form on behalf of export ui
  842. *
  843. * Returns the cached access config and contexts used.
  844. * Note that this is assuming that access will be in $item->access -- if it
  845. * is not, an export UI plugin will have to make its own callbacks.
  846. */
  847. function ctools_export_ui_ctools_access_set($argument, $access) {
  848. ctools_include('export-ui');
  849. list($plugin_name, $key) = explode(':', $argument, 2);
  850. $plugin = ctools_get_export_ui($plugin_name);
  851. $handler = ctools_export_ui_get_handler($plugin);
  852. if ($handler) {
  853. ctools_include('context');
  854. $item = $handler->edit_cache_get($key);
  855. if (!$item) {
  856. $item = ctools_export_crud_load($handler->plugin['schema'], $key);
  857. }
  858. $item->access = $access;
  859. return $handler->edit_cache_set_key($item, $key);
  860. }
  861. }
  862. /**
  863. * Implements hook_menu_local_tasks_alter().
  864. */
  865. function ctools_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  866. ctools_include('menu');
  867. _ctools_menu_add_dynamic_items($data, $router_item, $root_path);
  868. }
  869. /**
  870. * Implement hook_block_list_alter() to potentially remove blocks.
  871. *
  872. * This exists in order to replicate Drupal 6's "no blocks" functionality.
  873. */
  874. function ctools_block_list_alter(&$blocks) {
  875. $check = drupal_static('ctools_set_no_blocks', TRUE);
  876. if (!$check) {
  877. foreach ($blocks as $block_id => $block) {
  878. // @todo -- possibly we can set configuration for this so that users can
  879. // specify which blocks will not get rendered.
  880. if (strpos($block->region, 'sidebar') !== FALSE) {
  881. unset($blocks[$block_id]);
  882. }
  883. }
  884. }
  885. }
  886. /**
  887. * Implements hook_modules_enabled().
  888. *
  889. * Clear caches for detecting new plugins.
  890. */
  891. function ctools_modules_enabled($modules) {
  892. ctools_include('plugins');
  893. ctools_get_plugins_reset();
  894. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  895. }
  896. /**
  897. * Implements hook_modules_disabled().
  898. *
  899. * Clear caches for removing disabled plugins.
  900. */
  901. function ctools_modules_disabled($modules) {
  902. ctools_include('plugins');
  903. ctools_get_plugins_reset();
  904. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  905. }
  906. /**
  907. * Menu theme callback.
  908. *
  909. * This simply ensures that Panels ajax calls are rendered in the same
  910. * theme as the original page to prevent .css file confusion.
  911. *
  912. * To use this, set this as the theme callback on AJAX related menu
  913. * items. Since the ajax page state won't be sent during ajax requests,
  914. * it should be safe to use even if ajax isn't invoked.
  915. */
  916. function ctools_ajax_theme_callback() {
  917. if (!empty($_POST['ajax_page_state']['theme'])) {
  918. return $_POST['ajax_page_state']['theme'];
  919. }
  920. }
  921. /**
  922. * Implements hook_ctools_entity_context_alter().
  923. */
  924. function ctools_ctools_entity_context_alter(&$plugin, &$entity, $plugin_id) {
  925. ctools_include('context');
  926. switch ($plugin_id) {
  927. case 'entity_id:taxonomy_term':
  928. $plugin['no ui'] = TRUE;
  929. break;
  930. case 'entity:user':
  931. $plugin = ctools_get_context('user');
  932. unset($plugin['no ui']);
  933. unset($plugin['no required context ui']);
  934. break;
  935. }
  936. // Apply restrictions on taxonomy term reverse relationships whose
  937. // restrictions are in the settings on the field.
  938. if (!empty($plugin['parent']) &&
  939. $plugin['parent'] == 'entity_from_field' &&
  940. !empty($plugin['reverse']) &&
  941. $plugin['to entity'] == 'taxonomy_term') {
  942. $field = field_info_field($plugin['field name']);
  943. if (isset($field['settings']['allowed_values'][0]['vocabulary'])) {
  944. $plugin['required context']->restrictions = array('vocabulary' => array($field['settings']['allowed_values'][0]['vocabulary']));
  945. }
  946. }
  947. }
  948. /**
  949. * Implements hook_field_create_field().
  950. */
  951. function ctools_field_create_field($field) {
  952. ctools_flush_field_caches();
  953. }
  954. /**
  955. * Implements hook_field_create_instance().
  956. */
  957. function ctools_field_create_instance($instance) {
  958. ctools_flush_field_caches();
  959. }
  960. /**
  961. * Implements hook_field_delete_field().
  962. */
  963. function ctools_field_delete_field($field) {
  964. ctools_flush_field_caches();
  965. }
  966. /**
  967. * Implements hook_field_delete_instance().
  968. */
  969. function ctools_field_delete_instance($instance) {
  970. ctools_flush_field_caches();
  971. }
  972. /**
  973. * Implements hook_field_update_field().
  974. */
  975. function ctools_field_update_field($field, $prior_field, $has_data) {
  976. ctools_flush_field_caches();
  977. }
  978. /**
  979. * Implements hook_field_update_instance().
  980. */
  981. function ctools_field_update_instance($instance, $prior_instance) {
  982. ctools_flush_field_caches();
  983. }
  984. /**
  985. * Clear field related caches.
  986. */
  987. function ctools_flush_field_caches() {
  988. // Clear caches of 'Entity field' content type plugin.
  989. cache_clear_all('ctools_entity_field_content_type_content_types', 'cache');
  990. }