i18n_panels.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. *
  197. * @todo Check if a drupal_alter() in panels_display::get_title() is applicable.
  198. *
  199. * @see i18n_ctools_render_alter()
  200. *
  201. * @param panels_display $display
  202. * The display to deal with.
  203. * @param panels_renderer_standard $renderer
  204. * The renderer to deal with.
  205. */
  206. function i18n_panels_panels_pre_render(&$display, $renderer) {
  207. // Avoid double translations.
  208. if (!isset($display->i18n_panels_title_translated)) {
  209. $translation = i18n_string_object_translate('display_configuration', $display);
  210. if (is_array($translation->title)) {
  211. $display->title = $translation->title['string'];
  212. }
  213. else {
  214. $display->title = $translation->title;
  215. }
  216. $display->i18n_panels_title_translated = TRUE;
  217. }
  218. }
  219. /**
  220. * Implements hook_ctools_render_alter().
  221. *
  222. * Under some circumstances the title of the panel page is set before
  223. * hook_panels_pre_render() is fired. Such cases can be handled with this hook.
  224. *
  225. * @todo Check if a drupal_alter() in panels_display::get_title() is applicable.
  226. */
  227. function i18n_ctools_render_alter(&$info, $page, $context) {
  228. // @todo Find a better way to detect a panels page.
  229. if ($page === TRUE && !empty($info['content']['#display']) && $info['content']['#display'] instanceof panels_display) {
  230. i18n_panels_panels_pre_render($info['content']['#display'], NULL);
  231. // Set the info title. This is used to set the page title.
  232. $info['title'] = $info['content']['#display']->get_title();
  233. }
  234. }
  235. /**
  236. * Implements hook_ctools_plugin_post_alter().
  237. *
  238. * Register some translatable configuration settings for plugins.
  239. */
  240. function i18n_panels_ctools_plugin_post_alter(&$plugin, $plugin_type_info) {
  241. if ($plugin_type_info['type'] == 'content_types') {
  242. // Modify custom content.
  243. if ($plugin['name'] == 'custom') {
  244. // Register callback to get the translatable settings.
  245. $plugin['i18n_settings'] = 'ctools_custom_content_type_i18n_settings';
  246. }
  247. }
  248. }
  249. /**
  250. * Callback to provide the translatable settings appropriate to the config.
  251. *
  252. * @param array $conf
  253. * Content type configuration.
  254. *
  255. * @return array
  256. * i18n_settings configuration.
  257. */
  258. function ctools_custom_content_type_i18n_settings($conf) {
  259. return array(
  260. 'title',
  261. 'body' => array('format' => $conf['format']),
  262. );
  263. }
  264. /**
  265. * Implements hook_i18n_string_list_TEXTGROUP_alter().
  266. *
  267. * Necessary to support the dynamic translatable settings defined by ctools
  268. * content types.
  269. */
  270. function i18n_panels_i18n_string_list_panels_alter(&$strings, $type = NULL, $object = NULL) {
  271. if (isset($object->panels_i18n_settings)) {
  272. foreach ($object->panels_i18n_settings as $i18n_setting => $settings) {
  273. if (isset($object->{$i18n_setting})) {
  274. $strings['panels'][$type][$object->uuid][$i18n_setting] = $object->{$i18n_setting};
  275. }
  276. }
  277. }
  278. }
  279. /**
  280. * Implements hook_i18n_string_list().
  281. *
  282. * @todo Figure out a generic solution to fetch exported displays.
  283. */
  284. function i18n_panels_i18n_string_list($group) {
  285. $strings = array();
  286. if ($group == 'panels') {
  287. // Fetch all available displays.
  288. $displays = _18n_panels_fetch_all_panel_displays();
  289. foreach ($displays as $display) {
  290. if (empty($display->uuid)) {
  291. drupal_set_message(t('The display %display has no uuid, please resave or re-export it.', array('%display' => $display->did)), 'warning');
  292. continue;
  293. }
  294. // Avoid duplicated runs _18n_panels_fetch_all_panel_displays() probably
  295. // returns the same display twice, one for the db based and one for the
  296. // exported one.
  297. if (isset($strings['panels']['display_configuration'][$display->uuid])) {
  298. continue;
  299. }
  300. $strings['panels']['display_configuration'][$display->uuid]['title']['string'] = $display->title;
  301. foreach ($display->content as $pane) {
  302. if (empty($pane->uuid)) {
  303. // Fetch exported uuid and validate it.
  304. $uuid = str_replace('new-', '', $pane->pid);
  305. if (!ctools_uuid_is_valid($uuid)) {
  306. drupal_set_message(t('The pane %pane has no uuid, please resave or re-export it.', array('%pane' => $pane->pid)), 'warning');
  307. continue;
  308. }
  309. $pane->uuid = $uuid;
  310. }
  311. if ($translation_object = i18n_panels_get_i18n_translation_object($pane)) {
  312. // Split up all strings and add them to the list.
  313. $pane_strings = (array) $translation_object;
  314. unset($pane_strings['panels_i18n_settings']);
  315. foreach ($pane_strings as $key => $pane_string) {
  316. $strings['panels']['pane_configuration'][$pane->uuid][$key] = $pane_string;
  317. }
  318. }
  319. }
  320. }
  321. }
  322. return $strings;
  323. }
  324. /**
  325. * Checks if the give display is exported or only stored in the db.
  326. *
  327. * @return boolean
  328. * TRUE if the display is available from code.
  329. */
  330. function _18n_panels_is_exported_panels_display($display) {
  331. if (isset($display->uuid)) {
  332. $displays = _18n_panels_fetch_all_panel_displays();
  333. return isset($displays['exported-' . $display->uuid]);
  334. }
  335. return FALSE;
  336. }
  337. /**
  338. * Returns a list of really all available panel displays.
  339. *
  340. * The list is statically cached. Use the parameter $reset to refresh the list
  341. * during the same request.
  342. * Probably returns the same display twice - once with the db based and once
  343. * the exported one.
  344. *
  345. * @todo I bet there are better ways to solve this mess.
  346. *
  347. * @param bool $reset
  348. * Reset the static cache.
  349. *
  350. * @return array
  351. * List of all panel displays.
  352. */
  353. function _18n_panels_fetch_all_panel_displays($reset = FALSE) {
  354. $displays = &drupal_static(__FUNCTION__, array());
  355. if (!empty($displays) && !$reset) {
  356. return $displays;
  357. }
  358. // Fetch db based displays.
  359. $dids = db_select('panels_display')->fields('panels_display', array('did'))->execute()->fetchCol();
  360. $displays = panels_load_displays($dids);
  361. // Fetch exported displays.
  362. ctools_include('export');
  363. foreach (ctools_export_crud_load_all('panels_display') as $panels_display) {
  364. if (!empty($panels_display->uuid)) {
  365. $displays['exported-' . $panels_display->uuid] = $panels_display;
  366. }
  367. }
  368. // Fetch mini panels.
  369. $mini_panels = ctools_export_crud_load_all('panels_mini');
  370. foreach ($mini_panels as $pane) {
  371. if (!empty($pane->display->uuid)) {
  372. $displays['exported-' . $pane->display->uuid] = $pane->display;
  373. }
  374. }
  375. // Fetch in page manager embedded displays.
  376. if (module_exists('page_manager')) {
  377. module_load_include('inc', 'page_manager', 'page_manager.admin');
  378. $tasks = page_manager_get_tasks_by_type('page');
  379. $pages = array('operations' => array(), 'tasks' => array());
  380. page_manager_get_pages($tasks, $pages);
  381. foreach ($pages['tasks'] as $task) {
  382. $page = page_manager_cache_load($task);
  383. $task_info = page_manager_get_task_subtasks($page->task);
  384. foreach ($page->handler_info as $id => $info) {
  385. $page_manager_handler = $page->handlers[$id];
  386. if ($page_manager_handler->handler == 'panel_context') {
  387. // @todo Is there really no better way to check this?
  388. $is_exported = ($page_manager_handler->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE) || (isset($page->subtask['storage']) && $page->subtask['storage'] == t('Overridden')));
  389. if (!empty($page_manager_handler->conf['display'])) {
  390. $panels_display = $page_manager_handler->conf['display'];
  391. $displays['exported-' . $panels_display->uuid] = $panels_display;
  392. }
  393. elseif ($is_exported && isset($page_manager_handler->conf['did'])) {
  394. $panels_display = panels_load_display($page_manager_handler->conf['did']);
  395. if (isset($panels_display->uuid)) {
  396. $displays['exported-' . $panels_display->uuid] = $panels_display;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. }
  403. // Fetch panelizer displays.
  404. if (module_exists('panelizer')) {
  405. // Fetch all default handlers.
  406. $panelizer_defaults = ctools_export_crud_load_all('panelizer_defaults');
  407. foreach ($panelizer_defaults as $panelizer_default) {
  408. $displays['exported-' . $panelizer_default->display->uuid] = $panelizer_default->display;
  409. }
  410. }
  411. return $displays;
  412. }