i18n_panels.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /**
  3. * @file
  4. * Internationalization (i18n) submodule: Panels translation.
  5. */
  6. /**
  7. * Fetch the i18n_settings of the content type if there are any.
  8. *
  9. * @param stdClass $pane
  10. * The pane to deal with.
  11. *
  12. * @return array|false
  13. * Settings or FALSE if none are present.
  14. */
  15. function i18n_panels_get_i18n_settings($pane) {
  16. ctools_include('content');
  17. $content_type = ctools_get_content_type($pane->type);
  18. if (isset($content_type['i18n_settings'])) {
  19. if (is_string($content_type['i18n_settings']) && function_exists($content_type['i18n_settings'])) {
  20. $content_type['i18n_settings'] = $content_type['i18n_settings']($pane->configuration);
  21. }
  22. }
  23. // Provide the override title string as translation for all panes that have
  24. // this setting enabled.
  25. if (isset($pane->configuration['override_title']) && $pane->configuration['override_title']) {
  26. if (isset($content_type['i18n_settings']) && is_array($content_type['i18n_settings'])) {
  27. $content_type['i18n_settings'][] = 'override_title_text';
  28. }
  29. else {
  30. $content_type['i18n_settings'] = array('override_title_text');
  31. }
  32. }
  33. return isset($content_type['i18n_settings']) ? $content_type['i18n_settings'] : FALSE;
  34. }
  35. /**
  36. * Returns the translation object of the pane.
  37. *
  38. * @param stdClass $pane
  39. * The pane to deal with.
  40. *
  41. * @return stdClass|FALSE
  42. * Returns FALSE if no translation is necessary.
  43. */
  44. function i18n_panels_get_i18n_translation_object($pane) {
  45. $translation_object = array();
  46. // Handle content type specific i18n settings.
  47. if ($i18n_settings = i18n_panels_get_i18n_settings($pane)) {
  48. // Register translatable settings.
  49. foreach ($i18n_settings as $i18n_setting => $settings) {
  50. if (!is_array($settings)) {
  51. $i18n_setting = $settings;
  52. $settings = array('format' => 'plain_text');
  53. }
  54. $translation_object[$i18n_setting] = NULL;
  55. $key_exists = FALSE;
  56. // Ensure a nested setting is "unpacked".
  57. $config_value = drupal_array_get_nested_value($pane->configuration, explode('|', $i18n_setting), $key_exists);
  58. // If we reached the end of the nested setting use the value as source.
  59. if ($key_exists) {
  60. $translation_object[$i18n_setting] = array(
  61. 'string' => $config_value,
  62. 'format' => $settings['format'],
  63. );
  64. $translation_object['panels_i18n_settings'][$i18n_setting] = $settings;
  65. }
  66. }
  67. }
  68. // Check if this pane has a custom title enabled.
  69. if (!empty($pane->configuration['override_title'])) {
  70. $translation_object['title']['string'] = $pane->configuration['override_title_text'];
  71. }
  72. if (!empty($translation_object)) {
  73. return (object) $translation_object;
  74. }
  75. return FALSE;
  76. }
  77. /**
  78. * Implements hook_panels_pane_insert().
  79. *
  80. * @param stdClass $pane
  81. * The pane to deal with.
  82. */
  83. function i18n_panels_panels_pane_insert($pane) {
  84. i18n_panels_panels_pane_update($pane);
  85. }
  86. /**
  87. * Implements hook_panels_pane_update().
  88. *
  89. * @param stdClass $pane
  90. * The pane to deal with.
  91. */
  92. function i18n_panels_panels_pane_update($pane) {
  93. if ($translation_object = i18n_panels_get_i18n_translation_object($pane)) {
  94. $translation_object->uuid = $pane->uuid;
  95. $status = i18n_string_object_update('pane_configuration', $translation_object);
  96. }
  97. }
  98. /**
  99. * Implements hook_panels_pane_delete().
  100. *
  101. * @param array $pids
  102. * Array with the panel ids to delete.
  103. */
  104. function i18n_panels_panels_pane_delete($pids) {
  105. if (!empty($pids)) {
  106. // Fetch the uuids from the db.
  107. $uuids = db_select('panels_pane')
  108. ->fields('panels_pane', array('uuid'))
  109. ->condition('pid', $pids)
  110. ->execute()
  111. ->fetchCol();
  112. foreach ($uuids as $uuid) {
  113. // Create dummy pane with uuid as property.
  114. $pane = (object) array('uuid' => $uuid);
  115. i18n_string_object_remove('pane_configuration', $pane);
  116. }
  117. }
  118. }
  119. /**
  120. * Implements hook_panels_pane_prerender().
  121. *
  122. * @param stdClass $pane
  123. * The pane to deal with.
  124. */
  125. function i18n_panels_panels_pane_prerender($pane) {
  126. // Check if this pane has translations.
  127. if (isset($pane->uuid) && $translation_object = i18n_panels_get_i18n_translation_object($pane)) {
  128. $translation_object->uuid = $pane->uuid;
  129. // Send to translation.
  130. $translation_object = i18n_string_object_translate('pane_configuration', $translation_object);
  131. unset($translation_object->uuid, $translation_object->i18n_settings);
  132. foreach ($translation_object as $i18n_setting => $translated_setting) {
  133. if ($i18n_setting != 'panels_i18n_settings') {
  134. if (is_array($translated_setting)) {
  135. $translated_setting = $translated_setting['string'];
  136. }
  137. drupal_array_set_nested_value($pane->configuration, explode('|', $i18n_setting), $translated_setting);
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Implements hook_panels_display_save().
  144. *
  145. * @param panels_display $display
  146. * The display to deal with.
  147. */
  148. function i18n_panels_panels_display_save($display) {
  149. $status = i18n_string_object_update('display_configuration', $display);
  150. }
  151. /**
  152. * Implements hook_panels_display_delete().
  153. *
  154. * @param int $did
  155. * Id of the display to delete.
  156. */
  157. function i18n_panels_panels_delete_display($did) {
  158. // Fetch uuid to delete the translations.
  159. $uuid = db_select('panels_display')
  160. ->fields('panels_display', array('uuid'))
  161. ->condition('did', $did)
  162. ->execute()
  163. ->fetchColumn();
  164. // Build a dummy display.
  165. $display = (object) array('uuid' => $uuid);
  166. // Check if this display was just saved in the db.
  167. if (!_18n_panels_is_exported_panels_display($display)) {
  168. // If the display was just saved in the db remove all translations.
  169. i18n_string_object_remove('display_configuration', $display);
  170. // Remove related pane translations too.
  171. $pids = db_select('panels_pane')
  172. ->fields('panels_pane', array('pid'))
  173. ->condition('did', $did)
  174. ->execute()
  175. ->fetchCol();
  176. i18n_panels_panels_pane_delete($pids);
  177. }
  178. else {
  179. // If the display is exported leave the translated strings but give the user
  180. // a hint how to clean up.
  181. drupal_set_message(
  182. t(
  183. 'The reverted panels display(s) were exported, please run a <a href="!link">string refresh</a> to update the translatable strings.',
  184. array('!link' => url('admin/config/regional/translate/i18n_string'))
  185. ),
  186. 'warning',
  187. FALSE
  188. );
  189. }
  190. }
  191. /**
  192. * Implements hook_panels_pre_render().
  193. *
  194. * This function must not rely on the passed $renderer parameter. The parameter
  195. * could be empty because this function is reused in i18n_ctools_render_alter().
  196. * @todo Check if a drupal_alter() in panels_display::get_title() is applicable.
  197. *
  198. * @see i18n_ctools_render_alter()
  199. *
  200. * @param panels_display $display
  201. * The display to deal with.
  202. * @param panels_renderer_standard $renderer
  203. * The renderer to deal with.
  204. */
  205. function i18n_panels_panels_pre_render(&$display, $renderer) {
  206. // Avoid double translations.
  207. if (!isset($display->i18n_panels_title_translated)) {
  208. $translation = i18n_string_object_translate('display_configuration', $display);
  209. if (is_array($translation->title)) {
  210. $display->title = $translation->title['string'];
  211. }
  212. else {
  213. $display->title = $translation->title;
  214. }
  215. $display->i18n_panels_title_translated = TRUE;
  216. }
  217. }
  218. /**
  219. * Implements hook_ctools_render_alter().
  220. *
  221. * Under some circumstances the title of the panel page is set before
  222. * hook_panels_pre_render() is fired. Such cases can be handled with this hook.
  223. * @todo Check if a drupal_alter() in panels_display::get_title() is applicable.
  224. */
  225. function i18n_ctools_render_alter(&$info, $page, $context) {
  226. // @todo Find a better way to detect a panels page.
  227. if ($page === TRUE && !empty($info['content']['#display']) && $info['content']['#display'] instanceof panels_display) {
  228. i18n_panels_panels_pre_render($info['content']['#display'], NULL);
  229. // Set the info title. This is used to set the page title.
  230. $info['title'] = $info['content']['#display']->get_title();
  231. }
  232. }
  233. /**
  234. * Implements hook_ctools_plugin_post_alter().
  235. *
  236. * Register some translatable configuration settings for plugins.
  237. *
  238. */
  239. function i18n_panels_ctools_plugin_post_alter(&$plugin, $plugin_type_info) {
  240. if ($plugin_type_info['type'] == 'content_types') {
  241. // Modify custom content.
  242. if ($plugin['name'] == 'custom') {
  243. // Register callback to get the translatable settings.
  244. $plugin['i18n_settings'] = 'ctools_custom_content_type_i18n_settings';
  245. }
  246. }
  247. }
  248. /**
  249. * Callback to provide the translatable settings appropriate to the config.
  250. *
  251. * @param array $conf
  252. * Content type configuration.
  253. *
  254. * @return array
  255. * i18n_settings configuration.
  256. */
  257. function ctools_custom_content_type_i18n_settings($conf) {
  258. return array(
  259. 'title',
  260. 'body' => array('format' => $conf['format']),
  261. );
  262. }
  263. /**
  264. * Implements hook_i18n_string_list_TEXTGROUP_alter().
  265. *
  266. * Necessary to support the dynamic translatable settings defined by ctools
  267. * content types.
  268. */
  269. function i18n_panels_i18n_string_list_panels_alter(&$strings, $type = NULL, $object = NULL) {
  270. if (isset($object->panels_i18n_settings)) {
  271. foreach ($object->panels_i18n_settings as $i18n_setting => $settings) {
  272. if (isset($object->{$i18n_setting})) {
  273. $strings['panels'][$type][$object->uuid][$i18n_setting] = $object->{$i18n_setting};
  274. }
  275. }
  276. }
  277. }
  278. /**
  279. * Implements hook_i18n_string_list().
  280. *
  281. * @todo Figure out a generic solution to fetch exported displays.
  282. */
  283. function i18n_panels_i18n_string_list($group) {
  284. $strings = array();
  285. if ($group == 'panels') {
  286. // Fetch all available displays.
  287. $displays = _18n_panels_fetch_all_panel_displays();
  288. foreach ($displays as $display) {
  289. if (empty($display->uuid)) {
  290. drupal_set_message(t('The display %display has no uuid, please resave or re-export it.', array('%display' => $display->did)), 'warning');
  291. continue;
  292. }
  293. // Avoid duplicated runs _18n_panels_fetch_all_panel_displays() probably
  294. // returns the same display twice, one for the db based and one for the
  295. // exported one.
  296. if (isset($strings['panels']['display_configuration'][$display->uuid])) {
  297. continue;
  298. }
  299. $strings['panels']['display_configuration'][$display->uuid]['title']['string'] = $display->title;
  300. foreach ($display->content as $pane) {
  301. if (empty($pane->uuid)) {
  302. // Fetch exported uuid and validate it.
  303. $uuid = str_replace('new-', '', $pane->pid);
  304. if (!panels_uuid_is_valid($uuid)) {
  305. drupal_set_message(t('The pane %pane has no uuid, please resave or re-export it.', array('%pane' => $pane->pid)), 'warning');
  306. continue;
  307. }
  308. $pane->uuid = $uuid;
  309. }
  310. if ($translation_object = i18n_panels_get_i18n_translation_object($pane)) {
  311. // Split up all strings and add them to the list.
  312. $pane_strings = (array) $translation_object;
  313. unset($pane_strings['panels_i18n_settings']);
  314. foreach ($pane_strings as $key => $pane_string) {
  315. $strings['panels']['pane_configuration'][$pane->uuid][$key] = $pane_string;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. return $strings;
  322. }
  323. /**
  324. * Checks if the give display is exported or only stored in the db.
  325. *
  326. * @return boolean
  327. * TRUE if the display is available from code.
  328. */
  329. function _18n_panels_is_exported_panels_display($display) {
  330. if (isset($display->uuid)) {
  331. $displays = _18n_panels_fetch_all_panel_displays();
  332. return isset($displays['exported-' . $display->uuid]);
  333. }
  334. return FALSE;
  335. }
  336. /**
  337. * Returns a list of really all available panel displays.
  338. *
  339. * The list is statically cached. Use the parameter $reset to refresh the list
  340. * during the same request.
  341. * Probably returns the same display twice - once with the db based and once
  342. * the exported one.
  343. *
  344. * @todo I bet there are better ways to solve this mess.
  345. *
  346. * @param boolean $reset
  347. * Reset the static cache.
  348. *
  349. * @return array
  350. * List of all panel displays.
  351. */
  352. function _18n_panels_fetch_all_panel_displays($reset = FALSE) {
  353. $displays = &drupal_static(__FUNCTION__, array());
  354. if (!empty($displays) && !$reset) {
  355. return $displays;
  356. }
  357. // Fetch db based displays.
  358. $dids = db_select('panels_display')->fields('panels_display', array('did'))->execute()->fetchCol();
  359. $displays = panels_load_displays($dids);
  360. // Fetch exported displays.
  361. ctools_include('export');
  362. foreach (ctools_export_crud_load_all('panels_display') as $panels_display) {
  363. if (!empty($panels_display->uuid)) {
  364. $displays['exported-' . $panels_display->uuid] = $panels_display;
  365. }
  366. }
  367. // Fetch mini panels.
  368. $mini_panels = ctools_export_crud_load_all('panels_mini');
  369. foreach ($mini_panels as $pane) {
  370. if (!empty($pane->display->uuid)) {
  371. $displays['exported-' . $pane->display->uuid] = $pane->display;
  372. }
  373. }
  374. // Fetch in page manager embedded displays.
  375. if (module_exists('page_manager')) {
  376. module_load_include('inc', 'page_manager', 'page_manager.admin');
  377. $tasks = page_manager_get_tasks_by_type('page');
  378. $pages = array('operations' => array(), 'tasks' => array());
  379. page_manager_get_pages($tasks, $pages);
  380. foreach ($pages['tasks'] as $task) {
  381. $page = page_manager_cache_load($task);
  382. $task_info = page_manager_get_task_subtasks($page->task);
  383. foreach ($page->handler_info as $id => $info) {
  384. $page_manager_handler = $page->handlers[$id];
  385. if ($page_manager_handler->handler == 'panel_context') {
  386. // @todo Is there really no better way to check this?
  387. $is_exported = ($page_manager_handler->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE) || (isset($page->subtask['storage']) && $page->subtask['storage'] == t('Overridden')));
  388. if (!empty($page_manager_handler->conf['display'])) {
  389. $panels_display = $page_manager_handler->conf['display'];
  390. $displays['exported-' . $panels_display->uuid] = $panels_display;
  391. }
  392. elseif ($is_exported && isset($page_manager_handler->conf['did'])) {
  393. $panels_display = panels_load_display($page_manager_handler->conf['did']);
  394. if (isset($panels_display->uuid)) {
  395. $displays['exported-' . $panels_display->uuid] = $panels_display;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. }
  402. // Fetch panelizer displays.
  403. if (module_exists('panelizer')) {
  404. // Fetch all default handlers.
  405. $panelizer_defaults = ctools_export_crud_load_all('panelizer_defaults');
  406. foreach ($panelizer_defaults as $panelizer_default) {
  407. $displays['exported-' . $panelizer_default->display->uuid] = $panelizer_default->display;
  408. }
  409. }
  410. return $displays;
  411. }