entity_translation.admin.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. /**
  3. * @file
  4. * The entity translation user interface.
  5. */
  6. /**
  7. * Builder function for the entity translation settings form.
  8. */
  9. function entity_translation_admin_form($form, $form_state) {
  10. $options = array();
  11. $form['locale_field_language_fallback'] = array(
  12. '#type' => 'checkbox',
  13. '#title' => t('Enable language fallback'),
  14. '#description' => t('When language fallback is enabled, if a translation is not available for the requested language an existing one will be displayed.'),
  15. '#default_value' => variable_get('locale_field_language_fallback', TRUE),
  16. );
  17. $form['entity_translation_shared_labels'] = array(
  18. '#type' => 'checkbox',
  19. '#title' => t('Display shared labels'),
  20. '#description' => t('Append an "all languages" hint to entity form widgets shared across translations.'),
  21. '#default_value' => variable_get('entity_translation_shared_labels', TRUE),
  22. );
  23. $form['entity_translation_workflow_enabled'] = array(
  24. '#type' => 'checkbox',
  25. '#title' => t('Enable translation workflow permissions'),
  26. '#description' => t('By enabling the translation workflow permissions it will be possible to limit the access to the entity form elements. Once this is active every role previously allowed to access the entity form will need to be granted the <em>Edit original values</em> permission to edit the entity in the original language. Moreover, form elements dealing with values shared across the translations will be visible only to roles having been granted the <em>Edit shared values</em> permission.'),
  27. '#default_value' => entity_translation_workflow_enabled(),
  28. );
  29. $_null = NULL;
  30. list($items, ) = menu_router_build();
  31. _entity_translation_validate_path_schemes($_null, FALSE, $items);
  32. foreach (entity_get_info() as $entity_type => $info) {
  33. if ($info['fieldable']) {
  34. $et_info = &$info['translation']['entity_translation'];
  35. _entity_translation_process_path_schemes($entity_type, $et_info);
  36. _entity_translation_validate_path_schemes($et_info['path schemes'], $info['label']);
  37. // Translation can be enabled for the current entity only if it defines at
  38. // least one valid base path.
  39. foreach ($et_info['path schemes'] as $delta => $scheme) {
  40. if (!empty($scheme['base path'])) {
  41. $options[$entity_type] = !empty($info['label']) ? t($info['label']) : $entity_type;
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. // Avoid bloating memory with unused data.
  48. drupal_static_reset('_entity_translation_validate_path_schemes');
  49. $enabled_types = array_filter(variable_get('entity_translation_entity_types', array()));
  50. $form['enabled'] = array(
  51. '#type' => 'fieldset',
  52. '#title' => t('Translatable entity types'),
  53. '#description' => t('Select which entities can be translated.'),
  54. '#collapsible' => TRUE,
  55. '#collapsed' => !empty($enabled_types),
  56. );
  57. $form['enabled']['entity_translation_entity_types'] = array(
  58. '#type' => 'checkboxes',
  59. '#options' => $options,
  60. '#default_value' => $enabled_types,
  61. );
  62. $form['tabs'] = array(
  63. '#type' => 'vertical_tabs',
  64. );
  65. $languages = array(
  66. ENTITY_TRANSLATION_LANGUAGE_DEFAULT => t('Default language'),
  67. ENTITY_TRANSLATION_LANGUAGE_CURRENT => t('Current language'),
  68. ENTITY_TRANSLATION_LANGUAGE_AUTHOR => t('Author language'),
  69. LANGUAGE_NONE => t('Language neutral'),
  70. );
  71. foreach (entity_translation_languages($entity_type) as $langcode => $language) {
  72. $languages[$langcode] = t($language->name);
  73. }
  74. foreach ($enabled_types as $entity_type) {
  75. $label = $options[$entity_type];
  76. $entity_info = entity_get_info($entity_type);
  77. $bundles = !empty($entity_info['bundles']) ? $entity_info['bundles'] : array($entity_type => array('label' => $label));
  78. $enabled_bundles = 0;
  79. foreach ($bundles as $bundle => $info) {
  80. if (entity_translation_enabled_bundle($entity_type, $bundle)) {
  81. $enabled_bundles++;
  82. $settings = entity_translation_settings($entity_type, $bundle);
  83. $settings_key = 'entity_translation_settings_' . $entity_type . '__' . $bundle;
  84. $form['settings'][$entity_type][$settings_key] = array('#tree' => TRUE);
  85. // If the entity defines no bundle we do not need the fieldset.
  86. if (count($bundles) > 1 || $bundle != $entity_type) {
  87. $form['settings'][$entity_type][$settings_key] += array(
  88. '#type' => 'fieldset',
  89. '#title' => $info['label'],
  90. '#collapsible' => TRUE,
  91. '#collapsed' => TRUE,
  92. );
  93. }
  94. $form['settings'][$entity_type][$settings_key]['default_language'] = array(
  95. '#type' => 'select',
  96. '#title' => t('Default language'),
  97. '#options' => $languages,
  98. '#default_value' => $settings['default_language'],
  99. );
  100. $form['settings'][$entity_type][$settings_key]['hide_language_selector'] = array(
  101. '#type' => 'checkbox',
  102. '#title' => t('Hide language selector'),
  103. '#default_value' => $settings['hide_language_selector'],
  104. );
  105. $form['settings'][$entity_type][$settings_key]['exclude_language_none'] = array(
  106. '#type' => 'checkbox',
  107. '#title' => t('Exclude <em>Language neutral</em> from the available languages'),
  108. '#default_value' => $settings['exclude_language_none'],
  109. );
  110. $form['settings'][$entity_type][$settings_key]['lock_language'] = array(
  111. '#type' => 'checkbox',
  112. '#title' => t('Prevent language from being changed once the entity has been created'),
  113. '#default_value' => $settings['lock_language'],
  114. );
  115. $form['settings'][$entity_type][$settings_key]['shared_fields_original_only'] = array(
  116. '#type' => 'checkbox',
  117. '#title' => t('Hide shared elements on translation forms'),
  118. '#description' => t('Display form elements shared across translations only on entity forms for the original language.'),
  119. '#default_value' => $settings['shared_fields_original_only'],
  120. );
  121. }
  122. }
  123. if ($enabled_bundles > 0) {
  124. $form['settings'][$entity_type][$settings_key]['#collapsed'] = $enabled_bundles > 1;
  125. $form['settings'][$entity_type] += array(
  126. '#type' => 'fieldset',
  127. '#group' => 'tabs',
  128. '#title' => $label,
  129. );
  130. }
  131. }
  132. $form = system_settings_form($form);
  133. // Menu rebuilding needs to be performed after the system settings are saved.
  134. $form['#submit'][] = 'entity_translation_admin_form_submit';
  135. return $form;
  136. }
  137. /**
  138. * Submit handler for the entity translation settings form.
  139. */
  140. function entity_translation_admin_form_submit($form, $form_state) {
  141. // Clear the entity info cache for the new entity translation settings.
  142. entity_info_cache_clear();
  143. menu_rebuild();
  144. }
  145. /**
  146. * Applies the given settings to every defined bundle.
  147. *
  148. * @param $entity_type
  149. * The entity type the settings refer to.
  150. * @param $settings
  151. * (optional) The settings to be applied. Defaults to the entity default
  152. * settings.
  153. */
  154. function entity_translation_settings_init($entity_type, $settings = array()) {
  155. if (entity_translation_enabled($entity_type)) {
  156. $info = entity_get_info($entity_type);
  157. $bundles = !empty($info['bundles']) ? array_keys($info['bundles']) : array($entity_type);
  158. foreach ($bundles as $bundle) {
  159. if (entity_translation_enabled_bundle($entity_type, $bundle)) {
  160. $settings += entity_translation_settings($entity_type, $bundle);
  161. }
  162. }
  163. variable_set('entity_translation_settings_' . $entity_type . '__' . $bundle, $settings);
  164. }
  165. }
  166. /**
  167. * Translations overview page callback.
  168. */
  169. function entity_translation_overview($entity_type, $entity, $callback = NULL) {
  170. // Entity translation and node translation share the same system path.
  171. if ($callback && entity_translation_node($entity_type, $entity)) {
  172. return entity_translation_overview_callback($callback, $entity);
  173. }
  174. $handler = entity_translation_get_handler($entity_type, $entity);
  175. // Ensure $entity holds an entity object and not an id.
  176. $entity = $handler->getEntity();
  177. $handler->initPathScheme();
  178. // Initialize translations if they are empty.
  179. $translations = $handler->getTranslations();
  180. if (empty($translations->original)) {
  181. $handler->initTranslations();
  182. $handler->saveTranslations();
  183. }
  184. // Ensure that we have a coherent status between entity language and field
  185. // languages.
  186. if ($handler->initOriginalTranslation()) {
  187. // FIXME!
  188. field_attach_presave($entity_type, $entity);
  189. field_attach_update($entity_type, $entity);
  190. }
  191. $header = array(t('Language'), t('Source language'), t('Translation'), t('Status'), t('Operations'));
  192. $languages = entity_translation_languages();
  193. $source = $translations->original;
  194. $path = $handler->getViewPath();
  195. $rows = array();
  196. if (drupal_multilingual()) {
  197. // If we have a view path defined for the current entity get the switch
  198. // links based on it.
  199. if ($path) {
  200. $links = EntityTranslationDefaultHandler::languageSwitchLinks($path);
  201. }
  202. foreach ($languages as $language) {
  203. $classes = array();
  204. $options = array();
  205. $language_name = $language->name;
  206. $langcode = $language->language;
  207. $edit_path = $handler->getEditPath($langcode);
  208. $add_path = "{$handler->getEditPath()}/add/$source/$langcode";
  209. if ($edit_path) {
  210. $add_links = EntityTranslationDefaultHandler::languageSwitchLinks($add_path);
  211. $edit_links = EntityTranslationDefaultHandler::languageSwitchLinks($edit_path);
  212. }
  213. if (isset($translations->data[$langcode])) {
  214. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  215. // Existing translation in the translation set: display status.
  216. $is_original = $langcode == $translations->original;
  217. $translation = $translations->data[$langcode];
  218. $label = _entity_translation_label($entity_type, $entity, $langcode);
  219. $link = isset($links->links[$langcode]['href']) ? $links->links[$langcode] : array('href' => $path, 'language' => $language);
  220. $row_title = l($label, $link['href'], $link);
  221. if (empty($link['href'])) {
  222. $row_title = $is_original ? $label : t('n/a');
  223. }
  224. if ($edit_path && $handler->getAccess('update') && $handler->getTranslationAccess($langcode)) {
  225. $link = isset($edit_links->links[$langcode]['href']) ? $edit_links->links[$langcode] : array('href' => $edit_path, 'language' => $language);
  226. $link['query'] = isset($_GET['destination']) ? drupal_get_destination() : FALSE;
  227. $options[] = l(t('edit'), $link['href'], $link);
  228. }
  229. $status = $translation['status'] ? t('Published') : t('Not published');
  230. $classes[] = $translation['status'] ? 'published' : 'not-published';
  231. $status .= isset($translation['translate']) && $translation['translate'] ? theme('entity_translation_overview_outdated', array('message' => t('outdated'))) : '';
  232. $classes[] = isset($translation['translate']) && $translation['translate'] ? 'outdated' : '';
  233. if ($is_original) {
  234. $language_name = t('<strong>@language_name</strong>', array('@language_name' => $language_name));
  235. $source_name = t('(original content)');
  236. }
  237. else {
  238. $source_name = $languages[$translation['source']]->name;
  239. }
  240. }
  241. else {
  242. // No such translation in the set yet: help user to create it.
  243. $row_title = $source_name = t('n/a');
  244. if ($source != $langcode && $handler->getAccess('update') && $handler->getTranslationAccess($langcode)) {
  245. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  246. $translatable = FALSE;
  247. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  248. $field_name = $instance['field_name'];
  249. $field = field_info_field($field_name);
  250. if ($field['translatable']) {
  251. $translatable = TRUE;
  252. break;
  253. }
  254. }
  255. $link = isset($add_links->links[$langcode]['href']) ? $add_links->links[$langcode] : array('href' => $add_path, 'language' => $language);
  256. $link['query'] = isset($_GET['destination']) ? drupal_get_destination() : FALSE;
  257. $options[] = $translatable ? l(t('add'), $link['href'], $link) : t('No translatable fields');
  258. $classes[] = $translatable ? '' : 'non-traslatable';
  259. }
  260. $status = t('Not translated');
  261. }
  262. $rows[] = array(
  263. 'data' => array($language_name, $source_name, $row_title, $status, implode(" | ", $options)),
  264. 'class' => $classes,
  265. );
  266. }
  267. }
  268. drupal_set_title(t('Translations of %label', array('%label' => $handler->getLabel())), PASS_THROUGH);
  269. // Add metadata to the build render allow to let other modules know about
  270. // which entity this is.
  271. $build['#entity_type'] = $entity_type;
  272. $build['#entity'] = $entity;
  273. $build['entity_translation_overview'] = array(
  274. '#theme' => 'entity_translation_overview',
  275. '#header' => $header,
  276. '#rows' => $rows,
  277. );
  278. return $build;
  279. }
  280. /**
  281. * Calls the appropriate translation overview callback.
  282. */
  283. function entity_translation_overview_callback($callback, $entity) {
  284. if (module_exists($callback['module'])) {
  285. if (isset($callback['file'])) {
  286. $path = isset($callback['file path']) ? $callback['file path'] : drupal_get_path('module', $callback['module']);
  287. require_once DRUPAL_ROOT . '/' . $path . '/' . $callback['file'];
  288. }
  289. return $callback['page callback']($entity);
  290. }
  291. }
  292. /**
  293. * Returns the appropriate entity label for the given language.
  294. */
  295. function _entity_translation_label($entity_type, $entity, $langcode = NULL) {
  296. if (function_exists('title_entity_label')) {
  297. list (, , $bundle) = entity_extract_ids($entity_type, $entity);
  298. $entity_info = entity_get_info($entity_type);
  299. if (!empty($entity_info['entity keys']['label'])) {
  300. $legacy_field = $entity_info['entity keys']['label'];
  301. if (title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
  302. $title = title_entity_label($entity, $entity_type, $langcode);
  303. if (!empty($title)) {
  304. return $title;
  305. }
  306. }
  307. }
  308. }
  309. return t('view');
  310. }
  311. /**
  312. * Theme wrapper for the entity translation language page.
  313. */
  314. function theme_entity_translation_overview($variables){
  315. $rows = $variables['rows'];
  316. return theme('table', array(
  317. 'rows' => $rows,
  318. 'header' => $variables['header'],
  319. ));
  320. }
  321. /**
  322. * Theme wrapper for the entity translation language outdated translation.
  323. */
  324. function theme_entity_translation_overview_outdated($variables){
  325. $message = $variables['message'];
  326. return ' - <span class="marker">' . $message . '</span>';
  327. }
  328. /**
  329. * Translation deletion confirmation form.
  330. */
  331. function entity_translation_delete_confirm($form, $form_state, $entity_type, $entity, $langcode) {
  332. $handler = entity_translation_get_handler($entity_type, $entity);
  333. $handler->setFormLanguage($langcode);
  334. $languages = language_list();
  335. $form = array(
  336. '#handler' => $handler,
  337. '#entity_type' => $entity_type,
  338. // Ensure $entity holds an entity object and not an id.
  339. '#entity' => $handler->getEntity(),
  340. '#language' => $langcode,
  341. );
  342. return confirm_form(
  343. $form,
  344. t('Are you sure you want to delete the @language translation of %label?', array('@language' => $languages[$langcode]->name, '%label' => $handler->getLabel())),
  345. $handler->getEditPath($langcode),
  346. t('This action cannot be undone.'),
  347. t('Delete'),
  348. t('Cancel')
  349. );
  350. }
  351. /**
  352. * Submit handler for the translation deletion confirmation.
  353. */
  354. function entity_translation_delete_confirm_submit($form, &$form_state) {
  355. $handler = $form['#handler'];
  356. $entity_type = $form['#entity_type'];
  357. $entity = $form['#entity'];
  358. $langcode = $form['#language'];
  359. // Some modules expect the original values to be present when updating the
  360. // field values. Since we are deleting the translation no value has changed.
  361. $entity->original = $entity;
  362. // Remove the translation entry and the related fields.
  363. $handler->removeTranslation($langcode);
  364. field_attach_presave($entity_type, $entity);
  365. field_attach_update($entity_type, $entity);
  366. $form_state['redirect'] = $handler->getTranslatePath();
  367. }
  368. /*
  369. * Confirm form for changing field translatability.
  370. */
  371. function entity_translation_translatable_form($form, &$form_state, $field_name) {
  372. $field = field_info_field($field_name);
  373. if ($field['translatable']) {
  374. $title = t('Are you sure you want to disable translation for this field?');
  375. $text = t('All occurrences of this field will become <em>untranslatable</em>:');
  376. if (empty($form_state['input'])) {
  377. drupal_set_message(t('All the existing field translations will be deleted. This operation cannot be undone.'), 'warning');
  378. }
  379. }
  380. else {
  381. $title = t('Are you sure you want to enable translation for this field?');
  382. $text = t('All occurrences of this field will become <em>translatable</em>:');
  383. $form['options'] = array(
  384. '#type' => 'fieldset',
  385. '#title' => t('Migration settings'),
  386. '#weight' => -10,
  387. );
  388. $form['options']['copy_all_languages'] = array(
  389. '#title' => t('Copy translations'),
  390. '#description' => t('Copy field data into <em>all</em> existing translations, otherwise data will only be available in the original language.'),
  391. '#type' => 'checkbox',
  392. '#default_value' => TRUE,
  393. );
  394. }
  395. $text .= _entity_translation_field_desc($field);
  396. $text .= t('This operation may take a long time to complete.');
  397. // We need to keep some information for later processing.
  398. $form_state['field'] = $field;
  399. // Store the 'translatable' status on the client side to prevent outdated form
  400. // submits from toggling translatability.
  401. $form['translatable'] = array(
  402. '#type' => 'hidden',
  403. '#default_value' => $field['translatable'],
  404. );
  405. return confirm_form($form, $title, '', $text);
  406. }
  407. /**
  408. * Submit handler for the field settings form.
  409. *
  410. * This submit handler maintains consistency between the translatability of an
  411. * entity and the language under which the field data is stored. When a field is
  412. * marked as translatable, all the data in $entity->{field_name}[LANGUAGE_NONE]
  413. * is moved to $entity->{field_name}[$entity_language]. When a field is marked
  414. * as untranslatable the opposite process occurs. Note that marking a field as
  415. * untranslatable will cause all of its translations to be permanently removed,
  416. * with the exception of the one corresponding to the entity language.
  417. */
  418. function entity_translation_translatable_form_submit($form, $form_state) {
  419. // This is the current state that we want to reverse.
  420. $translatable = $form_state['values']['translatable'];
  421. $field_name = $form_state['field']['field_name'];
  422. $copy_all_languages = !empty($form_state['values']['copy_all_languages']);
  423. $field = field_info_field($field_name);
  424. if ($field['translatable'] !== $translatable) {
  425. // Field translatability has changed since form creation, abort.
  426. $t_args = array('%field_name' => $field_name, '!translatable' => $translatable ? t('untranslatable') : t('translatable'));
  427. drupal_set_message(t('The field %field_name is already !translatable. No change was performed.', $t_args), 'warning');
  428. return;
  429. }
  430. // If a field is untranslatable, it can have no data except under
  431. // LANGUAGE_NONE. Thus we need a field to be translatable before we convert
  432. // data to the entity language. Conversely we need to switch data back to
  433. // LANGUAGE_NONE before making a field untranslatable lest we lose
  434. // information.
  435. $operations = array(
  436. array('entity_translation_translatable_batch', array(!$translatable, $field_name, $copy_all_languages)),
  437. array('entity_translation_translatable_switch', array(!$translatable, $field_name)),
  438. );
  439. $operations = $translatable ? $operations : array_reverse($operations);
  440. $t_args = array('%field' => $field_name);
  441. $title = !$translatable ? t('Enabling translation for the %field field', $t_args) : t('Disabling translation for the %field field', $t_args);
  442. $batch = array(
  443. 'title' => $title,
  444. 'operations' => $operations,
  445. 'finished' => 'entity_translation_translatable_batch_done',
  446. 'file' => drupal_get_path('module', 'entity_translation') . '/entity_translation.admin.inc',
  447. );
  448. batch_set($batch);
  449. }
  450. /*
  451. * Toggle translatability of the given field.
  452. *
  453. * This is called from a batch operation, but should only run once per field.
  454. */
  455. function entity_translation_translatable_switch($translatable, $field_name) {
  456. $field = field_info_field($field_name);
  457. if ($field['translatable'] === $translatable) {
  458. return;
  459. }
  460. $field['translatable'] = $translatable;
  461. field_update_field($field);
  462. // This is needed for versions of Drupal core 7.10 and lower.
  463. // See http://drupal.org/node/1380660 for details.
  464. drupal_static_reset('field_available_languages');
  465. }
  466. /**
  467. * Batch operation. Convert field data to or from LANGUAGE_NONE.
  468. */
  469. function entity_translation_translatable_batch($translatable, $field_name, $copy_all_languages, &$context) {
  470. if (empty($context['sandbox'])) {
  471. $context['sandbox']['progress'] = 0;
  472. // How many entities will need processing?
  473. $query = new EntityFieldQuery();
  474. $count = $query
  475. ->fieldCondition($field_name)
  476. ->count()
  477. ->execute();
  478. if (intval($count) === 0) {
  479. // Nothing to do.
  480. $context['finished'] = 1;
  481. return;
  482. }
  483. $context['sandbox']['max'] = $count;
  484. }
  485. // Number of entities to be processed for each step.
  486. $limit = variable_get('entity_translation_translatable_batch_limit', 10);
  487. $offset = $context['sandbox']['progress'];
  488. $query = new EntityFieldQuery();
  489. $result = $query
  490. ->fieldCondition($field_name)
  491. ->entityOrderBy('entity_id')
  492. ->range($offset, $limit)
  493. ->execute();
  494. foreach ($result as $entity_type => $entities) {
  495. foreach (entity_load($entity_type, array_keys($entities)) as $entity) {
  496. $context['sandbox']['progress']++;
  497. $handler = entity_translation_get_handler($entity_type, $entity);
  498. $langcode = $handler->getLanguage();
  499. // Skip process for language neutral entities.
  500. if ($langcode == LANGUAGE_NONE) {
  501. continue;
  502. }
  503. // We need a two-steps approach while updating field translations: given
  504. // that field-specific update functions might rely on the stored values to
  505. // perform their processing, see for instance file_field_update(), first
  506. // we need to store the new translations and only after we can remove the
  507. // old ones. Otherwise we might have data loss, since the removal of the
  508. // old translations might occur before the new ones are stored.
  509. if ($translatable && isset($entity->{$field_name}[LANGUAGE_NONE])) {
  510. // If the field is being switched to translatable and has data for
  511. // LANGUAGE_NONE then we need to move the data to the right language.
  512. $translations = $handler->getTranslations();
  513. if ($copy_all_languages && !empty($translations->data)) {
  514. foreach ($translations->data as $translation) {
  515. $entity->{$field_name}[$translation['language']] = $entity->{$field_name}[LANGUAGE_NONE];
  516. }
  517. }
  518. else {
  519. $entity->{$field_name}[$langcode] = $entity->{$field_name}[LANGUAGE_NONE];
  520. }
  521. // Store the original value.
  522. _entity_translation_update_field($entity_type, $entity, $field_name);
  523. $entity->{$field_name}[LANGUAGE_NONE] = array();
  524. // Remove the language neutral value.
  525. _entity_translation_update_field($entity_type, $entity, $field_name);
  526. }
  527. elseif (!$translatable && isset($entity->{$field_name}[$langcode])) {
  528. // The field has been marked untranslatable and has data in the entity
  529. // language: we need to move it to LANGUAGE_NONE and drop the other
  530. // translations.
  531. $entity->{$field_name}[LANGUAGE_NONE] = $entity->{$field_name}[$langcode];
  532. // Store the original value.
  533. _entity_translation_update_field($entity_type, $entity, $field_name);
  534. // Remove translations.
  535. foreach ($entity->{$field_name} as $langcode => $items) {
  536. if ($langcode != LANGUAGE_NONE) {
  537. $entity->{$field_name}[$langcode] = array();
  538. }
  539. }
  540. _entity_translation_update_field($entity_type, $entity, $field_name);
  541. }
  542. else {
  543. // No need to save unchanged entities.
  544. continue;
  545. }
  546. }
  547. }
  548. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  549. }
  550. /**
  551. * Stores the given field translations.
  552. */
  553. function _entity_translation_update_field($entity_type, $entity, $field_name) {
  554. $empty = 0;
  555. $field = field_info_field($field_name);
  556. // Ensure that we are trying to store only valid data.
  557. foreach ($entity->{$field_name} as $langcode => $items) {
  558. $entity->{$field_name}[$langcode] = _field_filter_items($field, $entity->{$field_name}[$langcode]);
  559. $empty += empty($entity->{$field_name}[$langcode]);
  560. }
  561. // Save the field value only if there is at least one item available,
  562. // otherwise any stored empty field value would be deleted. If this happens
  563. // the range queries would be messed up.
  564. if ($empty < count($entity->{$field_name})) {
  565. field_attach_presave($entity_type, $entity);
  566. field_attach_update($entity_type, $entity);
  567. }
  568. }
  569. /**
  570. * Check the exit status of the batch operation.
  571. */
  572. function entity_translation_translatable_batch_done($success, $results, $operations) {
  573. if ($success) {
  574. drupal_set_message(t("Data successfully processed."));
  575. }
  576. else {
  577. // @todo: Do something about this case.
  578. drupal_set_message(t("Something went wrong while processing data. Some nodes may appear to have lost fields."));
  579. }
  580. }