webform_localization.i18n.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /**
  3. * @file
  4. * Webform Localization i18n_string integration.
  5. */
  6. /**
  7. * Provides interface with the i18n_string module.
  8. * Based in patch http://drupal.org/node/245424#comment-5244256
  9. * by Calin Marian. Further development sponsored by Riot Games.
  10. *
  11. * @author German Martin <gmartin.php@gmail.com>
  12. */
  13. /**
  14. * Translates the component properties that are translatable.
  15. *
  16. * These are found in under 'translated_strings' in the 'extra' array of the
  17. * component, which is build when the component is inserted / updated, or
  18. * when all webform strings are updated from
  19. * admin/config/regional/translate/i18n_string.
  20. *
  21. * @param array $element
  22. * The FAPI renderable array of the component instance.
  23. * @param array $component
  24. * The component.
  25. */
  26. function _webform_localization_translate_component(&$element, $component) {
  27. if (isset($component['extra']['translated_strings']) && is_array($component['extra']['translated_strings'])) {
  28. $node = !empty($component['nid']) ? node_load($component['nid']) : NULL;
  29. foreach ($component['extra']['translated_strings'] as $name) {
  30. $name_list = explode(':', $name);
  31. $current_element = &$element;
  32. $current_element_format;
  33. if (isset($current_element['#format'])) {
  34. $current_element_format = $current_element['#format'];
  35. } else {
  36. $current_element_format = I18N_STRING_FILTER_XSS_ADMIN;
  37. }
  38. if (strpos($name_list[3], '[') !== FALSE) {
  39. // The property is deeper in the renderable array, we must extract the
  40. // the place where it is.
  41. list ($children, $property) = explode(']#', $name_list[3]);
  42. // Remove the '[' from the begining of the string.
  43. $children = drupal_substr($children, 1);
  44. $children_array = explode('][', $children);
  45. foreach ($children_array as $child) {
  46. if (isset($current_element[$child])) {
  47. $current_element = &$current_element[$child];
  48. }
  49. else {
  50. continue;
  51. }
  52. }
  53. }
  54. else {
  55. // Remove the '#' from the begining of the property, for consistency.
  56. $property = drupal_substr($name_list[3], 1);
  57. }
  58. if (strpos($property, '-') !== FALSE) {
  59. // If property is array, we extract the key from the property.
  60. list ($property, $key) = explode('-', $property, 2);
  61. if (isset($current_element['#' . $property][$key])) {
  62. $text = i18n_string($name, $current_element['#' . $property][$key], array('format' => I18N_STRING_FILTER_XSS));
  63. if (module_exists('token')) {
  64. $current_element['#' . $property][$key] = webform_replace_tokens($text, $node);
  65. }
  66. else {
  67. $current_element['#' . $property][$key] = $text;
  68. }
  69. }
  70. }
  71. else {
  72. // If we are dealing with option groups.
  73. if (isset($name_list[4]) && strpos($name_list[4], '/-') !== FALSE) {
  74. $option_group = str_replace('/-', '', $name_list[4]);
  75. // If it's a element.
  76. if (isset($name_list[5])) {
  77. $text = i18n_string($name, $current_element['#' . $property][$option_group][$name_list[5]], array('format' => $current_element_format));
  78. if (module_exists('token')) {
  79. $current_element['#' . $property][$option_group][$name_list[5]] = webform_replace_tokens($text, $node);
  80. }
  81. else {
  82. $current_element['#' . $property][$option_group][$name_list[5]] = $text;
  83. }
  84. }
  85. else {
  86. // If it's a option group we translate the key.
  87. $text = i18n_string($name, $option_group, array('format' => $current_element_format));
  88. if (module_exists('token')) {
  89. $translated_option_group = webform_replace_tokens($text, $node);
  90. }
  91. else {
  92. $translated_option_group = $text;
  93. }
  94. if ($translated_option_group != $option_group) {
  95. _webform_localization_array_key_replace($current_element['#' . $property], $option_group, $translated_option_group);
  96. }
  97. }
  98. }
  99. else {
  100. // Else we can treat the property as string.
  101. if (isset($current_element['#' . $property])) {
  102. if ($property == 'markup' && $current_element['#type'] == 'markup') {
  103. $text = i18n_string($name, $current_element['#' . $property], array('format' => $current_element['#format']));
  104. }
  105. elseif ($property == 'description') {
  106. $text = i18n_string($name, $current_element['#' . $property], array('sanitize' => FALSE));
  107. }
  108. else {
  109. $text = i18n_string($name, $current_element['#' . $property], array('sanitize' => FALSE));
  110. }
  111. $current_element['#' . $property] = $text;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Update / create translation source for all the translatable properties.
  120. *
  121. * @param array $component
  122. * A webform component.
  123. */
  124. function webform_localization_component_update_translation_strings(&$component) {
  125. // Fill in the the default values for the missing properties.
  126. module_load_include('inc', 'webform', 'includes/webform.components');
  127. webform_component_defaults($component);
  128. // Render the 'render' FAPI array for the component.
  129. $element = webform_component_invoke($component['type'], 'render', $component, NULL, 'html');
  130. // Parse the renderable array to find the translatable properties and
  131. // update / create translation source for them.
  132. $component['extra']['translated_strings'] = _webform_localization_component_translation_parse($element, $component);
  133. // Render the 'display' FAPI array for the component.
  134. $element = webform_component_invoke($component['type'], 'display', $component, NULL, 'html');
  135. // Parse the renderable array to find the translatable properties and
  136. // update / create translation source for them.
  137. $component['extra']['translated_strings'] = array_merge($component['extra']['translated_strings'], array_diff(_webform_localization_component_translation_parse($element, $component), $component['extra']['translated_strings']));
  138. }
  139. /**
  140. * Parse a component renderable array to find the translatable properties.
  141. *
  142. * Create / update or remove translation source for translatable properties
  143. * of a webform component.
  144. *
  145. * @param array $element
  146. * The renderable array to be parsed.
  147. * @param array $component
  148. * The component which was rendered.
  149. *
  150. * @return array
  151. * An array of translatabled webform properties.
  152. */
  153. function _webform_localization_component_translation_parse($element, $component) {
  154. $translated_properies = array();
  155. if (!isset($element['#parents'])) {
  156. $element['#parents'] = array();
  157. }
  158. $element['#translatable'][] = 'placeholder';
  159. if (isset($element['#translatable']) && is_array($element['#translatable'])) {
  160. foreach ($element['#translatable'] as $key) {
  161. if (!empty($element['#' . $key]) || !empty($element['#attributes'][$key])) {
  162. if (isset($element['#parents']) && count($element['#parents'])) {
  163. $property = '[' . implode('][', $element['#parents']) . ']#' . $key;
  164. }
  165. else {
  166. $property = '#' . $key;
  167. }
  168. $element_key = '';
  169. if ($key == 'placeholder' && !empty($element['#attributes']['placeholder'])) {
  170. $element_key = $element['#attributes']['placeholder'];
  171. } elseif ($key != 'placeholder' && !empty($element['#' . $key])) {
  172. $element_key = $element['#' . $key];
  173. }
  174. if (is_array($element_key)) {
  175. // If the translatable property is an array, we translate the
  176. // children.
  177. foreach ($element_key as $elem_key => $elem_value) {
  178. // If the child if an array, we translate the elements.
  179. if (is_array($elem_value)) {
  180. foreach ($elem_value as $k => $v) {
  181. $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property, '/-' . $elem_key . '/-', $k);
  182. $translated_properies[] = $name;
  183. i18n_string($name, $v, array('update' => TRUE));
  184. }
  185. $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property, '/-' . $elem_key . '/-');
  186. $translated_properies[] = $name;
  187. i18n_string($name, $elem_key, array('update' => TRUE));
  188. }
  189. else {
  190. // If the child is not an array.
  191. $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property . '-' . $elem_key);
  192. $translated_properies[] = $name;
  193. i18n_string($name, $elem_value, array('update' => TRUE));
  194. }
  195. }
  196. }
  197. else {
  198. // If the translatable property is not an array,
  199. // it can be treated as a string.
  200. $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property);
  201. $translated_properies[] = $name;
  202. i18n_string($name, $element_key, array('update' => TRUE));
  203. }
  204. }
  205. }
  206. }
  207. // Recursevly call the function on the children, after adding the children
  208. // name to its #parents array.
  209. foreach (element_children($element) as $child) {
  210. $element[$child]['#parents'] = $element['#parents'];
  211. $element[$child]['#parents'][] = $child;
  212. // Add the translated propertied to the list.
  213. $translated_properies = array_merge(
  214. $translated_properies, _webform_localization_component_translation_parse($element[$child], $component)
  215. );
  216. }
  217. return $translated_properies;
  218. }
  219. /**
  220. * Utility function to create i18n string name.
  221. *
  222. * Additional arguments can be passed to add more depth to context
  223. *
  224. * @param int $node_identifier
  225. * webform nid.
  226. *
  227. * @return string
  228. * i18n string name grouped by nid or uuid if module is available.
  229. */
  230. function webform_localization_i18n_string_name($node_identifier) {
  231. if (module_exists('uuid') and !uuid_is_valid($node_identifier)) {
  232. $node_identifier = current(entity_get_uuid_by_id('node', array($node_identifier)));
  233. }
  234. $name = array('webform', $node_identifier);
  235. $args = func_get_args();
  236. // Remove $node_identifier from args.
  237. array_shift($args);
  238. foreach ($args as $arg) {
  239. $name[] = $arg;
  240. }
  241. return implode(':', $name);
  242. }
  243. /**
  244. * Delete translation source for all the translatable properties.
  245. *
  246. * Process components matching webforms configuration.
  247. */
  248. function webform_localization_delete_all_strings() {
  249. $query = db_select('webform_component', 'wc');
  250. $query->fields('wc');
  251. $query->condition('wl.expose_strings', 0, '=');
  252. $query->innerJoin('webform_localization', 'wl', 'wc.nid = wl.nid');
  253. $components = $query->execute()->fetchAllAssoc('cid');
  254. foreach ($components as $component) {
  255. $component = (array) $component;
  256. $component['extra'] = unserialize($component['extra']);
  257. webform_localization_component_delete_translation_strings($component);
  258. $component['extra'] = serialize($component['extra']);
  259. drupal_write_record('webform_component', $component, array('nid', 'cid'));
  260. }
  261. }
  262. /**
  263. * Remove translation source for all the translatable properties.
  264. *
  265. * @param array $component
  266. * A webform component array.
  267. */
  268. function webform_localization_component_delete_translation_strings($component) {
  269. if (isset($component['extra']['translated_strings'])) {
  270. foreach ($component['extra']['translated_strings'] as $name) {
  271. i18n_string_remove($name);
  272. }
  273. }
  274. }
  275. /**
  276. * Update / create translation source for general webform properties.
  277. *
  278. * @param array $properties
  279. * The form_state values that have been saved.
  280. */
  281. function webform_localization_update_translation_strings($properties) {
  282. $options = array('update' => TRUE, 'translate' => FALSE);
  283. if (!empty($properties['confirmation']['value'])) {
  284. $name = webform_localization_i18n_string_name($properties['nid'], 'confirmation');
  285. $confirmationOptions = $options + array('format' => I18N_STRING_FILTER_XSS);
  286. if (isset($properties['confirmation']['format'])) {
  287. if (i18n_string_allowed_format($properties['confirmation']['format'])) {
  288. $confirmationOptions['format'] = $properties['confirmation']['format'];
  289. }
  290. else {
  291. drupal_set_message(t('The string @name could not be refreshed with the text format @format because it is not allowed for translation.', array(
  292. '@name' => $name,
  293. '@format' => $properties['confirmation']['format']
  294. )), 'warning', FALSE);
  295. }
  296. }
  297. i18n_string($name, $properties['confirmation']['value'], $confirmationOptions);
  298. }
  299. if (!empty($properties['preview_message']['value'])) {
  300. $name = webform_localization_i18n_string_name($properties['nid'], 'preview_message');
  301. $confirmationOptions = $options + array('format' => I18N_STRING_FILTER_XSS);
  302. if (isset($properties['preview_message']['format'])) {
  303. if (i18n_string_allowed_format($properties['preview_message']['format'])) {
  304. $confirmationOptions['format'] = $properties['preview_message']['format'];
  305. }
  306. else {
  307. drupal_set_message(t('The string @name could not be refreshed with the text format @format because it is not allowed for translation.', array(
  308. '@name' => $name,
  309. '@format' => $properties['preview_message']['format']
  310. )), 'warning', FALSE);
  311. }
  312. }
  313. i18n_string($name, $properties['preview_message']['value'], $confirmationOptions);
  314. }
  315. if (!empty($properties['submit_text'])) {
  316. $name = webform_localization_i18n_string_name($properties['nid'], 'submit_text');
  317. i18n_string($name, $properties['submit_text'], $options);
  318. }
  319. // Allow to translate the redirect url if it's not set to none or the
  320. // default confirmation page.
  321. if (!in_array($properties['redirect_url'], array('<confirmation>', '<none>', '<front>'))) {
  322. $name = webform_localization_i18n_string_name($properties['nid'], 'redirect_url');
  323. i18n_string($name, $properties['redirect_url'], $options);
  324. }
  325. }
  326. /**
  327. * Translate general webform properties.
  328. *
  329. * @param object $node
  330. * A node object.
  331. */
  332. function webform_localization_translate_strings(&$node, $update = FALSE) {
  333. $options = array('update' => $update);
  334. if (!array_key_exists('nid', $node->webform)) {
  335. $node->webform['nid'] = $node->nid;
  336. }
  337. $name = webform_localization_i18n_string_name($node->webform['nid'], 'confirmation');
  338. $confirmationOptions = $options + array('format' => I18N_STRING_FILTER_XSS);
  339. if (in_array($node->webform['redirect_url'], array('<confirmation>', '<none>')) && isset($node->webform['confirmation_format'])) {
  340. if (i18n_string_allowed_format($node->webform['confirmation_format'])) {
  341. $confirmationOptions['format'] = $node->webform['confirmation_format'];
  342. }
  343. else {
  344. drupal_set_message(t('The string @name could not be refreshed with the text format @format because it is not allowed for translation.', array(
  345. '@name' => $name,
  346. '@format' => $node->webform['confirmation_format']
  347. )), 'warning', FALSE);
  348. }
  349. }
  350. $node->webform['confirmation'] = i18n_string(
  351. $name, $node->webform['confirmation'], $confirmationOptions);
  352. $name = webform_localization_i18n_string_name($node->webform['nid'], 'preview_message');
  353. if (isset($node->webform['preview_message_format'])) {
  354. if (i18n_string_allowed_format($node->webform['preview_message_format'])) {
  355. $confirmationOptions['format'] = $node->webform['preview_message_format'];
  356. }
  357. else {
  358. drupal_set_message(t('The string @name could not be refreshed with the text format @format because it is not allowed for translation.', array(
  359. '@name' => $name,
  360. '@format' => $node->webform['preview_message_format']
  361. )), 'warning', FALSE);
  362. }
  363. }
  364. $node->webform['preview_message'] = i18n_string(
  365. $name, $node->webform['preview_message'], $confirmationOptions);
  366. $name = webform_localization_i18n_string_name($node->webform['nid'], 'submit_text');
  367. $node->webform['submit_text'] = i18n_string(
  368. $name, $node->webform['submit_text'], $options);
  369. // Allow to translate the redirect url if it's not set to none or the
  370. // default confirmation page.
  371. if (!in_array($node->webform['redirect_url'], array('<confirmation>', '<none>', '<front>'))) {
  372. $name = webform_localization_i18n_string_name($node->webform['nid'], 'redirect_url');
  373. $node->webform['redirect_url'] = i18n_string($name, $node->webform['redirect_url'], $options);
  374. }
  375. }
  376. /**
  377. * Update / create translation source for webform email properties.
  378. *
  379. * @param array $properties
  380. * The form_state values that have been saved.
  381. */
  382. function webform_localization_emails_update_translation_string($properties) {
  383. $nid = $properties['node']->webform['nid'];
  384. $eid = $properties['eid'];
  385. if (!empty($properties['subject_custom'])) {
  386. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'subject_custom');
  387. i18n_string($name, $properties['subject_custom'], array('update' => TRUE));
  388. }
  389. // Allow to translate the mail recipients if not based on a component.
  390. if (!empty($properties['email']) && !is_numeric($properties['email'])) {
  391. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'email');
  392. i18n_string($name, $properties['email'], array('update' => TRUE));
  393. }
  394. if (!empty($properties['from_name_custom'])) {
  395. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'from_name_custom');
  396. i18n_string($name, $properties['from_name_custom'], array('update' => TRUE));
  397. }
  398. if (!empty($properties['template'])) {
  399. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'template');
  400. i18n_string($name, $properties['template'], array('update' => TRUE));
  401. }
  402. }
  403. /**
  404. * Update / create translation source for webform email properties.
  405. *
  406. * @param array $emails
  407. * An array of webform emails.
  408. * @param int $nid
  409. * The node Id of the webform.
  410. */
  411. function webform_localization_emails_translation_string_refresh($emails, $nid) {
  412. foreach ($emails as $email) {
  413. $eid = $email['eid'];
  414. if (!empty($email['subject']) && $email['subject'] != 'default') {
  415. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'subject_custom');
  416. i18n_string($name, $email['subject'], array('update' => TRUE));
  417. }
  418. // Allow to translate the mail recipients if not based on a component.
  419. if (!empty($email['email']) && !is_numeric($email['email'])) {
  420. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'email');
  421. i18n_string($name, $email['email'], array('update' => TRUE));
  422. }
  423. if (!empty($email['from_name']) && $email['from_name'] != 'default') {
  424. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'from_name_custom');
  425. i18n_string($name, $email['from_name'], array('update' => TRUE));
  426. }
  427. if (!empty($email['template']) && $email['template'] != 'default') {
  428. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'template');
  429. i18n_string($name, $email['template'], array('update' => TRUE));
  430. }
  431. }
  432. }
  433. /**
  434. * Translate webform email properties.
  435. *
  436. * @param object $node
  437. * A node object.
  438. */
  439. function webform_localization_email_translate_strings(&$node) {
  440. $nid = $node->webform['nid'];
  441. foreach ($node->webform['emails'] as $eid => &$email) {
  442. if (!empty($email['subject']) && $email['subject'] != 'default') {
  443. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'subject_custom');
  444. $email['subject'] = i18n_string($name, $email['subject']);
  445. }
  446. // Allow to translate the mail recipients if not based on a component.
  447. if (!empty($email['email']) && !is_numeric($email['email'])) {
  448. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'email');
  449. $email['email'] = i18n_string($name, $email['email']);
  450. }
  451. if (!empty($email['from_name']) && $email['from_name'] != 'default') {
  452. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'from_name_custom');
  453. $email['from_name'] = i18n_string($name, $email['from_name']);
  454. }
  455. if (!empty($email['template']) && $email['template'] != 'default') {
  456. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'template');
  457. $email['template'] = i18n_string($name, $email['template'], array('sanitize' => FALSE));
  458. }
  459. }
  460. }
  461. /**
  462. * Remove translation source for webform email properties.
  463. *
  464. * @param int $eid
  465. * A webform email Id.
  466. * @param int $nid
  467. * A node Id.
  468. */
  469. function webform_localization_emails_delete_translation_string($eid, $nid) {
  470. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'subject_custom');
  471. i18n_string_remove($name);
  472. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'from_name_custom');
  473. i18n_string_remove($name);
  474. $name = webform_localization_i18n_string_name($nid, 'email', $eid, 'template');
  475. i18n_string_remove($name);
  476. }
  477. /**
  478. * Translate general webform properties.
  479. *
  480. * @param object $node
  481. * A node object.
  482. */
  483. function webform_localization_delete_translate_strings($node) {
  484. $name = webform_localization_i18n_string_name($node->webform['nid'], 'confirmation');
  485. i18n_string_remove($name);
  486. $name = webform_localization_i18n_string_name($node->webform['nid'], 'submit_text');
  487. i18n_string_remove($name);
  488. foreach ($node->webform['emails'] as $eid => $value) {
  489. webform_localization_emails_delete_translation_string($eid, $node->nid);
  490. }
  491. }
  492. /**
  493. * Update i18n string contexts if uuid module is enabled/disabled.
  494. */
  495. function webform_localization_uuid_update_strings($disabling_uuid = FALSE) {
  496. module_load_install('i18n_string');
  497. $old_ids = db_query('SELECT distinct type FROM {i18n_string} WHERE textgroup = :webform', array(
  498. ':webform' => 'webform',
  499. ))->fetchCol();
  500. variable_set('webform_localization_using_uuid', !$disabling_uuid);
  501. if (empty($old_ids)) {
  502. return;
  503. }
  504. if (!$disabling_uuid) {
  505. $old_context_ids = entity_get_uuid_by_id('node', array($old_ids));
  506. }
  507. else {
  508. // entity_get_id_by_uuid() do not work properly on hook_disable.
  509. $old_context_ids = webform_localization_get_id_by_uuid('node', array($old_ids));
  510. }
  511. foreach ($old_context_ids as $old_id => $new_id) {
  512. $old_context = 'webform:' . $old_id . ':*';
  513. $new_context = 'webform:' . $new_id . ':*';
  514. i18n_string_install_update_context($old_context, $new_context);
  515. }
  516. }
  517. /**
  518. * Helper function that retrieves entity IDs by their UUIDs.
  519. *
  520. * @param string $entity_type
  521. * The entity type we should be dealing with.
  522. * @param array $uuids
  523. * An array of UUIDs for which we should find their entity IDs. If $revision
  524. * is TRUE this should be revision UUIDs instead.
  525. *
  526. * @return array
  527. * Array of entity IDs keyed by their UUIDs. If $revision is TRUE revision
  528. * IDs and UUIDs are returned instead.
  529. */
  530. function webform_localization_get_id_by_uuid($entity_type, $uuids) {
  531. if (empty($uuids)) {
  532. return array();
  533. }
  534. $info = entity_get_info($entity_type);
  535. $table = $info['base table'];
  536. $id_key = $info['entity keys']['id'];
  537. // The uuid key is not available at hook_disable.
  538. $core_info = uuid_get_core_entity_info();
  539. $uuid_key = $core_info['node']['entity keys']['uuid'];
  540. // Get all UUIDs in one query.
  541. return db_select($table, 't')
  542. ->fields('t', array($uuid_key, $id_key))
  543. ->condition($uuid_key, array_values($uuids), 'IN')
  544. ->execute()
  545. ->fetchAllKeyed();
  546. }
  547. /**
  548. * Helper function to replace an array key and its content.
  549. *
  550. * @param array $array
  551. * Array To process.
  552. * @param string $old_key
  553. * Array key to be replaced.
  554. * @param string $new_key
  555. * The new array key.
  556. */
  557. function _webform_localization_array_key_replace(&$array, $old_key, $new_key) {
  558. $keys = array_keys($array);
  559. $values = array_values($array);
  560. foreach ($keys as $k => $v) {
  561. if ($v == $old_key) {
  562. $keys[$k] = $new_key;
  563. }
  564. }
  565. $array = array_combine($keys, $values);
  566. }
  567. /**
  568. * Helper function to convert select / grid strings to array.
  569. *
  570. * @param string $string_array
  571. * Array To process.
  572. *
  573. * @return array
  574. * Processed array.
  575. */
  576. function _webform_localization_string_to_key($string_array) {
  577. $key_array = array();
  578. $items = explode("\n", trim($string_array));
  579. foreach ($items as $item) {
  580. $item_data = explode('|', $item);
  581. if (isset($item_data[1]) && isset($item_data[0])) {
  582. $key_array[$item_data[0]] = $item_data[1];
  583. }
  584. elseif (isset($item_data[1]) && !isset($item_data[0])) {
  585. $key_array[$item_data[0]] = '';
  586. }
  587. }
  588. return $key_array;
  589. }