entity_translation.module 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  1. <?php
  2. /**
  3. * @file
  4. * Allows entities to be translated into different languages.
  5. */
  6. module_load_include('inc', 'entity_translation', 'entity_translation.node');
  7. /**
  8. * Language code identifying the site default language.
  9. */
  10. define('ENTITY_TRANSLATION_LANGUAGE_DEFAULT', 'xx-et-default');
  11. /**
  12. * Language code identifying the current content language.
  13. */
  14. define('ENTITY_TRANSLATION_LANGUAGE_CURRENT', 'xx-et-current');
  15. /**
  16. * Language code identifying the author's preferred language.
  17. */
  18. define('ENTITY_TRANSLATION_LANGUAGE_AUTHOR', 'xx-et-author');
  19. /**
  20. * Implements hook_hook_info().
  21. */
  22. function entity_translation_hook_info() {
  23. $hooks['entity_translation_insert'] = array(
  24. 'group' => 'entity_translation',
  25. );
  26. $hooks['entity_translation_update'] = array(
  27. 'group' => 'entity_translation',
  28. );
  29. $hooks['entity_translation_delete'] = array(
  30. 'group' => 'entity_translation',
  31. );
  32. return $hooks;
  33. }
  34. /**
  35. * Implements hook_module_implements_alter().
  36. */
  37. function entity_translation_module_implements_alter(&$implementations, $hook) {
  38. switch ($hook) {
  39. case 'menu_alter':
  40. case 'entity_info_alter':
  41. // Move some of our hook implementations to the end of the list.
  42. $group = $implementations['entity_translation'];
  43. unset($implementations['entity_translation']);
  44. $implementations['entity_translation'] = $group;
  45. break;
  46. }
  47. }
  48. /**
  49. * Implements hook_language_type_info_alter().
  50. */
  51. function entity_translation_language_types_info_alter(array &$language_types) {
  52. unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
  53. }
  54. /**
  55. * Implements hook_entity_info().
  56. */
  57. function entity_translation_entity_info() {
  58. $info = array();
  59. $info['node'] = array(
  60. 'translation' => array(
  61. 'entity_translation' => array(
  62. 'class' => 'EntityTranslationNodeHandler',
  63. 'access callback' => 'entity_translation_node_tab_access',
  64. 'access arguments' => array(1),
  65. 'admin theme' => variable_get('node_admin_theme'),
  66. 'bundle callback' => 'entity_translation_node_supported_type',
  67. 'default settings' => array(
  68. 'default_language' => LANGUAGE_NONE,
  69. 'hide_language_selector' => FALSE,
  70. ),
  71. ),
  72. ),
  73. );
  74. if (module_exists('comment')) {
  75. $info['comment'] = array(
  76. 'translation' => array(
  77. 'entity_translation' => array(
  78. 'class' => 'EntityTranslationCommentHandler',
  79. 'admin theme' => FALSE,
  80. 'bundle callback' => 'entity_translation_comment_supported_type',
  81. 'default settings' => array(
  82. 'default_language' => ENTITY_TRANSLATION_LANGUAGE_CURRENT,
  83. 'hide_language_selector' => TRUE,
  84. ),
  85. ),
  86. ),
  87. );
  88. }
  89. if (module_exists('taxonomy')) {
  90. $info['taxonomy_term'] = array(
  91. 'translation' => array(
  92. 'entity_translation' => array(
  93. 'class' => 'EntityTranslationTaxonomyTermHandler',
  94. 'base path' => 'taxonomy/term/%taxonomy_term',
  95. 'edit form' => 'term',
  96. ),
  97. ),
  98. );
  99. }
  100. $info['user'] = array(
  101. 'translation' => array(
  102. 'entity_translation' => array(
  103. 'class' => 'EntityTranslationUserHandler',
  104. 'skip original values access' => TRUE,
  105. 'skip shared fields access' => TRUE,
  106. ),
  107. ),
  108. );
  109. return $info;
  110. }
  111. /**
  112. * Processes the given path schemes and fill-in default values.
  113. */
  114. function _entity_translation_process_path_schemes($entity_type, &$et_info) {
  115. $path_scheme_keys = array_flip(array('base path', 'view path', 'edit path', 'translate path', 'path wildcard', 'admin theme', 'edit tabs'));
  116. // Insert the default path scheme into the 'path schemes' array and remove
  117. // respective elements from the entity_translation info array.
  118. $default_scheme = array_intersect_key($et_info, $path_scheme_keys);
  119. if (!empty($default_scheme)) {
  120. $et_info['path schemes']['default'] = $default_scheme;
  121. $et_info = array_diff_key($et_info, $path_scheme_keys);
  122. }
  123. // If no base path is provided we default to the common "node/%node"
  124. // pattern.
  125. if (empty($et_info['path schemes']['default']['base path'])) {
  126. $et_info['path schemes']['default']['base path'] = "$entity_type/%$entity_type";
  127. }
  128. foreach ($et_info['path schemes'] as $delta => $scheme) {
  129. // If there is a base path, then we automatically create the other path
  130. // elements based on the base path.
  131. if (!empty($scheme['base path'])) {
  132. $view_path = $scheme['base path'];
  133. $edit_path = $scheme['base path'] . '/edit';
  134. $translate_path = $scheme['base path'] . '/translate';
  135. $et_info['path schemes'][$delta] += array(
  136. 'view path' => $view_path,
  137. 'edit path' => $edit_path,
  138. 'translate path' => $translate_path,
  139. );
  140. }
  141. // Merge in default values for other scheme elements.
  142. $et_info['path schemes'][$delta] += array(
  143. 'admin theme' => TRUE,
  144. 'path wildcard' => "%$entity_type",
  145. 'edit tabs' => TRUE,
  146. );
  147. }
  148. }
  149. /**
  150. * Implements hook_entity_info_alter().
  151. */
  152. function entity_translation_entity_info_alter(&$entity_info) {
  153. // Provide defaults for translation info.
  154. foreach ($entity_info as $entity_type => $info) {
  155. if (!isset($entity_info[$entity_type]['translation']['entity_translation'])) {
  156. $entity_info[$entity_type]['translation']['entity_translation'] = array();
  157. }
  158. $et_info = &$entity_info[$entity_type]['translation']['entity_translation'];
  159. // Every fieldable entity type must have a translation handler class and
  160. // translation keys defined, no matter if it is enabled for translation or
  161. // not. As a matter of fact we might need them to correctly switch field
  162. // translatability when a field is shared across different entity types.
  163. $et_info += array('class' => 'EntityTranslationDefaultHandler');
  164. if (!isset($entity_info[$entity_type]['entity keys'])) {
  165. $entity_info[$entity_type]['entity keys'] = array();
  166. }
  167. $entity_info[$entity_type]['entity keys'] += array('translations' => 'translations');
  168. if (entity_translation_enabled($entity_type, NULL, TRUE)) {
  169. $entity_info[$entity_type]['language callback'] = 'entity_translation_language';
  170. // Process path schemes and fill-in defaults.
  171. _entity_translation_process_path_schemes($entity_type, $et_info);
  172. // Merge in default values for remaining keys.
  173. $et_info += array(
  174. 'access callback' => 'entity_translation_tab_access',
  175. 'access arguments' => array($entity_type),
  176. );
  177. // Interpret a TRUE value for the 'edit form' key as the default value.
  178. if (!isset($et_info['edit form']) || $et_info['edit form'] === TRUE) {
  179. $et_info['edit form'] = $entity_type;
  180. }
  181. }
  182. }
  183. }
  184. /**
  185. * Implements hook_menu().
  186. */
  187. function entity_translation_menu() {
  188. $items = array();
  189. $items['admin/config/regional/entity_translation'] = array(
  190. 'title' => 'Entity translation',
  191. 'description' => 'Configure which entities can be translated and enable or disable language fallback.',
  192. 'page callback' => 'drupal_get_form',
  193. 'page arguments' => array('entity_translation_admin_form'),
  194. 'access arguments' => array('administer entity translation'),
  195. 'file' => 'entity_translation.admin.inc',
  196. 'module' => 'entity_translation',
  197. );
  198. $items['admin/config/regional/entity_translation/translatable/%'] = array(
  199. 'title' => 'Confirm change in translatability.',
  200. 'description' => 'Confirmation page for changing field translatability.',
  201. 'page callback' => 'drupal_get_form',
  202. 'page arguments' => array('entity_translation_translatable_form', 5),
  203. 'access arguments' => array('toggle field translatability'),
  204. 'file' => 'entity_translation.admin.inc',
  205. );
  206. return $items;
  207. }
  208. /**
  209. * Validate the given set of path schemes and remove invalid elements.
  210. *
  211. * Each path scheme needs to fulfill the following requirements:
  212. * - The 'path wildcard' key needs to be specified.
  213. * - Every path (base/view/edit/translate) needs to contain the path wildcard.
  214. * - The following path definitions (if specified) need to match existing menu
  215. * items: 'base path', 'view path', 'edit path'.
  216. * - The 'translate path' definition needs to have an existing parent menu item.
  217. *
  218. * This function needs to be called once with a list of menu items passed as the
  219. * last parameter, before it can be used for validation.
  220. *
  221. * @param $schemes
  222. * The array of path schemes.
  223. * @param $entity_type_label
  224. * The label of the current entity type. This is used in error messages.
  225. * @param $items
  226. * A list of menu items.
  227. * @param $warnings
  228. * (optional) Displays warnings when a path scheme does not validate.
  229. */
  230. function _entity_translation_validate_path_schemes(&$schemes, $entity_type_label, $items = FALSE, $warnings = FALSE) {
  231. $paths = &drupal_static(__FUNCTION__);
  232. static $regex = '|%[^/]+|';
  233. if (!empty($items)) {
  234. // Some menu loaders in the item paths might have been altered: we need to
  235. // replace any menu loader with a plain % to check if base paths are still
  236. // compatible.
  237. $paths = array();
  238. foreach ($items as $path => $item) {
  239. $stripped_path = preg_replace($regex, '%', $path);
  240. $paths[$stripped_path] = $path;
  241. }
  242. }
  243. if (empty($schemes)) {
  244. return;
  245. }
  246. // Make sure we have a set of paths to validate the scheme against.
  247. if (empty($paths)) {
  248. // This should never happen.
  249. throw new Exception('The Entity Translation path scheme validation function has not been initialized properly.');
  250. }
  251. foreach ($schemes as $delta => &$scheme) {
  252. // Every path scheme needs to declare a path wildcard for the entity id.
  253. if (empty($scheme['path wildcard'])) {
  254. if ($warnings) {
  255. $t_args = array('%scheme' => $delta, '%entity_type' => $entity_type_label);
  256. drupal_set_message(t('Entity Translation path scheme %scheme for entities of type %entity_type does not declare a path wildcard.', $t_args), 'warning');
  257. }
  258. unset($schemes[$delta]);
  259. continue;
  260. }
  261. $wildcard = $scheme['path wildcard'];
  262. $validate_keys = array('base path' => FALSE, 'view path' => FALSE, 'edit path' => FALSE, 'translate path' => TRUE);
  263. foreach ($validate_keys as $key => $check_parent) {
  264. if (isset($scheme[$key])) {
  265. $path = $scheme[$key];
  266. $parts = explode('/', $path);
  267. $scheme[$key . ' parts'] = $parts;
  268. // Check that the path contains the path wildcard. Required for
  269. // determining the position of the entity id in the path (see
  270. // entity_translation_menu_alter()).
  271. if (!in_array($wildcard, $parts)) {
  272. if ($warnings) {
  273. $t_args = array('%path_key' => $key, '%entity_type' => $entity_type_label, '%wildcard' => $wildcard, '%path' => $path);
  274. drupal_set_message(t('Invalid %path_key defined for entities of type %entity_type: entity wildcard %wildcard not found in %path.', $t_args), 'warning');
  275. }
  276. unset($scheme[$key]);
  277. continue;
  278. }
  279. // Remove the trailing path element for paths requiring an existing
  280. // parent menu item (i.e. the "translate path").
  281. $trailing_path_element = FALSE;
  282. if ($check_parent) {
  283. $trailing_path_element = array_pop($parts);
  284. $path = implode('/', $parts);
  285. }
  286. $stripped_path = preg_replace($regex, '%', $path);
  287. if (!isset($paths[$stripped_path])) {
  288. if ($warnings) {
  289. $t_args = array('%path_key' => $key, '%entity_type' => $entity_type_label, '%path' => $path);
  290. $msg = $check_parent ?
  291. t('Invalid %path_key defined for entities of type %entity_type: parent menu item not found for %path', $t_args) :
  292. t('Invalid %path_key defined for entities of type %entity_type: matching menu item not found for %path', $t_args);
  293. drupal_set_message($msg, 'warning');
  294. }
  295. unset($scheme[$key]);
  296. }
  297. // If there is a matching menu item for the current scheme key, save
  298. // the real path, i.e. the path of the matching menu item.
  299. else {
  300. $real_path = $paths[$stripped_path];
  301. $real_parts = explode('/', $real_path);
  302. // Restore previously removed trailing path element.
  303. if ($trailing_path_element) {
  304. $real_path .= '/' . $trailing_path_element;
  305. $real_parts[] = $trailing_path_element;
  306. }
  307. $scheme['real ' . $key] = $real_path;
  308. $scheme['real ' . $key . ' parts'] = $real_parts;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. /**
  315. * Implements hook_menu_alter().
  316. */
  317. function entity_translation_menu_alter(&$items) {
  318. $backup = array();
  319. // Initialize path schemes validation function with set of current menu items.
  320. $_null = NULL;
  321. _entity_translation_validate_path_schemes($_null, FALSE, $items);
  322. // Create tabs for all possible entity types.
  323. foreach (entity_get_info() as $entity_type => $info) {
  324. // Menu is rebuilt while determining entity translation base paths and
  325. // callbacks so we might not have them available yet.
  326. if (entity_translation_enabled($entity_type)) {
  327. $et_info = $info['translation']['entity_translation'];
  328. // Flag for tracking whether we have managed to attach the translate UI
  329. // successfully at least once.
  330. $translate_ui_attached = FALSE;
  331. // Validate path schemes for current entity type. Also removes invalid
  332. // ones and adds '... path parts' elements.
  333. _entity_translation_validate_path_schemes($et_info['path schemes'], $info['label'], FALSE, TRUE);
  334. foreach ($et_info['path schemes'] as $scheme) {
  335. $translate_item = NULL;
  336. $edit_item = NULL;
  337. // If we have a translate path then attach the translation UI, and
  338. // register the callback for deleting a translation.
  339. if (isset($scheme['translate path'])) {
  340. $translate_path = $scheme['translate path'];
  341. $keys = array('theme callback', 'theme arguments', 'access callback', 'access arguments', 'load arguments');
  342. $item = array_intersect_key($info['translation']['entity_translation'], drupal_map_assoc($keys));
  343. $item += array(
  344. 'file' => 'entity_translation.admin.inc',
  345. 'module' => 'entity_translation',
  346. );
  347. $entity_position = array_search($scheme['path wildcard'], $scheme['translate path parts']);
  348. if ($item['access callback'] == 'entity_translation_tab_access') {
  349. $item['access arguments'][] = $entity_position;
  350. }
  351. // Backup existing values for the translate overview page.
  352. if (isset($items[$translate_path])) {
  353. $backup[$entity_type] = $items[$translate_path];
  354. }
  355. $items[$translate_path] = array(
  356. 'title' => 'Translate',
  357. 'page callback' => 'entity_translation_overview',
  358. 'page arguments' => array($entity_type, $entity_position),
  359. 'type' => MENU_LOCAL_TASK,
  360. 'weight' => 2,
  361. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  362. ) + $item;
  363. // Delete translation callback.
  364. $language_position = count($scheme['translate path parts']) + 1;
  365. $items["$translate_path/delete/%entity_translation_language"] = array(
  366. 'title' => 'Delete',
  367. 'page callback' => 'drupal_get_form',
  368. 'page arguments' => array('entity_translation_delete_confirm', $entity_type, $entity_position, $language_position),
  369. ) + $item;
  370. $translate_item = &$items[$translate_path];
  371. }
  372. // If we have an edit path, then replace the menu edit form with our
  373. // proxy implementation, and register new callbacks for adding and
  374. // editing a translation.
  375. if (isset($scheme['edit path'])) {
  376. // Find the edit item. If the edit path is a default local task we
  377. // need to find the parent item.
  378. $real_edit_path_parts = $scheme['real edit path parts'];
  379. do {
  380. $edit_item = &$items[implode('/', $real_edit_path_parts)];
  381. array_pop($real_edit_path_parts);
  382. }
  383. while (!empty($edit_item['type']) && $edit_item['type'] == MENU_DEFAULT_LOCAL_TASK);
  384. $edit_path = $scheme['edit path'];
  385. $edit_path_parts = $scheme['edit path parts'];
  386. // Replace the main edit callback with our proxy implementation to set
  387. // form language to the current language and check access.
  388. $entity_position = array_search($scheme['path wildcard'], $edit_path_parts);
  389. $original_item = $edit_item;
  390. $args = array($entity_type, $entity_position, FALSE, $original_item);
  391. $edit_item['page callback'] = 'entity_translation_edit_page';
  392. $edit_item['page arguments'] = array_merge($args, $original_item['page arguments']);
  393. $edit_item['access callback'] = 'entity_translation_edit_access';
  394. $edit_item['access arguments'] = array_merge($args, $original_item['access arguments']);
  395. // Edit translation callback.
  396. if ($scheme['edit tabs'] !== FALSE) {
  397. $translation_position = count($edit_path_parts);
  398. $args = array($entity_type, $entity_position, $translation_position, $original_item);
  399. $items["$edit_path/%entity_translation_language"] = array(
  400. 'type' => MENU_DEFAULT_LOCAL_TASK,
  401. 'title callback' => 'entity_translation_edit_title',
  402. 'title arguments' => array($translation_position),
  403. 'page callback' => 'entity_translation_edit_page',
  404. 'page arguments' => array_merge($args, $original_item['page arguments']),
  405. 'access callback' => 'entity_translation_edit_access',
  406. 'access arguments' => array_merge($args, $original_item['access arguments']),
  407. )
  408. // We need to inherit the remaining menu item keys, mostly 'module'
  409. // and 'file' to keep ajax callbacks working (see form_get_cache() and
  410. // drupal_retrieve_form()).
  411. + $original_item;
  412. }
  413. // Add translation callback.
  414. $add_path = "$edit_path/add/%entity_translation_language/%entity_translation_language";
  415. $source_position = count($edit_path_parts) + 1;
  416. $target_position = count($edit_path_parts) + 2;
  417. $args = array($entity_type, $entity_position, $source_position, $target_position, $original_item);
  418. $items[$add_path] = array(
  419. 'title callback' => 'Add translation',
  420. 'page callback' => 'entity_translation_add_page',
  421. 'page arguments' => array_merge($args, $original_item['page arguments']),
  422. 'type' => MENU_LOCAL_TASK,
  423. 'access callback' => 'entity_translation_add_access',
  424. 'access arguments' => array_merge($args, $original_item['access arguments']),
  425. ) + $original_item;
  426. }
  427. // Make the "Translate" tab follow the "Edit" tab if possible.
  428. if ($translate_item && $edit_item && isset($edit_item['weight'])) {
  429. $translate_item['weight'] = $edit_item['weight'] + 1;
  430. }
  431. // If we have both an edit item and a translate item, then we know that
  432. // the translate UI has been attached properly (at least once).
  433. $translate_ui_attached = $translate_ui_attached || ($translate_item && $edit_item);
  434. // Cleanup reference variables, so we don't accidentially overwrite
  435. // something in a later iteration.
  436. unset($translate_item, $edit_item);
  437. }
  438. if ($translate_ui_attached == FALSE) {
  439. drupal_set_message(t('The entities of type %entity_type do not define a valid path scheme: it will not be possible to translate them.', array('%entity_type' => $info['label'])), 'warning');
  440. }
  441. // Node-specific menu alterations.
  442. if ($entity_type == 'node') {
  443. entity_translation_node_menu_alter($items, $backup);
  444. }
  445. }
  446. }
  447. // Avoid bloating memory with unused data.
  448. drupal_static_reset('_entity_translation_validate_path_schemes');
  449. }
  450. /**
  451. * Title callback.
  452. */
  453. function entity_translation_edit_title($langcode) {
  454. $languages = entity_translation_languages();
  455. return isset($languages[$langcode]) ? t($languages[$langcode]->name) : '';
  456. }
  457. /**
  458. * Page callback.
  459. */
  460. function entity_translation_edit_page() {
  461. $args = func_get_args();
  462. $entity_type = array_shift($args);
  463. $entity = array_shift($args);
  464. $langcode = array_shift($args);
  465. $edit_form_item = array_shift($args);
  466. // Set the current form language.
  467. $handler = entity_translation_get_handler($entity_type, $entity);
  468. $handler->initPathScheme();
  469. $langcode = entity_translation_get_existing_language($entity_type, $entity, $langcode);
  470. $handler->setFormLanguage($langcode);
  471. // Display the entity edit form.
  472. return _entity_translation_callback($edit_form_item['page callback'], $args, $edit_form_item);
  473. }
  474. /**
  475. * Access callback.
  476. */
  477. function entity_translation_edit_access() {
  478. $args = func_get_args();
  479. $entity_type = array_shift($args);
  480. $entity = array_shift($args);
  481. $langcode = array_shift($args);
  482. $edit_form_item = array_shift($args);
  483. $access_callback = isset($edit_form_item['access callback']) ? $edit_form_item['access callback'] : 'user_access';
  484. $handler = entity_translation_get_handler($entity_type, $entity);
  485. // First, check a handler has been loaded. This could be empty if a
  486. // non-existent entity edit path has been requested, for example. Delegate
  487. // directly to the edit form item access callback in this case.
  488. if (empty($handler)) {
  489. return _entity_translation_callback($access_callback, $args, $edit_form_item);
  490. }
  491. $translations = $handler->getTranslations();
  492. $langcode = entity_translation_get_existing_language($entity_type, $entity, $langcode);
  493. // The user must be explicitly allowed to access the original values if
  494. // workflow permissions are enabled.
  495. if (!$handler->getTranslationAccess($langcode)) {
  496. return FALSE;
  497. }
  498. // If the translation exists or no translation was specified, we can show the
  499. // corresponding local task. If translations have not been initialized yet, we
  500. // need to grant access to the user.
  501. if (empty($translations->data) || isset($translations->data[$langcode])) {
  502. // Check that the requested language is actually accessible. If the entity
  503. // is language neutral we need to let editors access it.
  504. $enabled_languages = entity_translation_languages($entity_type, $entity);
  505. if (isset($enabled_languages[$langcode]) || $langcode == LANGUAGE_NONE) {
  506. return _entity_translation_callback($access_callback, $args, $edit_form_item);
  507. }
  508. }
  509. return FALSE;
  510. }
  511. /**
  512. * Determines the current form language.
  513. *
  514. * @param $langcode
  515. * The requested language code.
  516. * @param EntityTranslationHandlerInterface $handler
  517. * A translation handler instance.
  518. *
  519. * @return
  520. * A valid language code.
  521. *
  522. * @deprecated This is no longer used and will be removed in the first stable
  523. * release.
  524. */
  525. function entity_translation_form_language($langcode, $handler) {
  526. return entity_translation_get_existing_language($handler->getEntity(), $handler->getEntityType(), $langcode);
  527. }
  528. /**
  529. * Determines an existing translation language.
  530. *
  531. * Based on the requested language and the translations available for the given
  532. * entity, determines an existing translation language. This takes into account
  533. * language fallback rules.
  534. *
  535. * @param $entity_type
  536. * The type of the entity.
  537. * @param $entity
  538. * The entity whose existing translation language has to be returned.
  539. * @param $langcode
  540. * (optional) The requested language code. Defaults to the current content
  541. * language.
  542. *
  543. * @return
  544. * A valid language code.
  545. */
  546. function entity_translation_get_existing_language($entity_type, $entity, $langcode = NULL) {
  547. $handler = entity_translation_get_handler($entity_type, $entity);
  548. if (empty($langcode)) {
  549. $langcode = $GLOBALS['language_content']->language;
  550. }
  551. $translations = $handler->getTranslations();
  552. $fallback = drupal_multilingual() ? language_fallback_get_candidates() : array(LANGUAGE_NONE);
  553. while (!empty($langcode) && !isset($translations->data[$langcode])) {
  554. $langcode = array_shift($fallback);
  555. }
  556. // If no translation is available fall back to the entity language.
  557. return !empty($langcode) ? $langcode : $handler->getLanguage();
  558. }
  559. /**
  560. * Access callback.
  561. */
  562. function entity_translation_add_access() {
  563. $args = func_get_args();
  564. $entity_type = array_shift($args);
  565. $entity = array_shift($args);
  566. $source = array_shift($args);
  567. $langcode = array_shift($args);
  568. $handler = entity_translation_get_handler($entity_type, $entity);
  569. $translations = $handler->getTranslations();
  570. // If the translation does not exist we can show the tab.
  571. if (!isset($translations->data[$langcode]) && $langcode != $source) {
  572. // Check that the requested language is actually accessible.
  573. $enabled_languages = entity_translation_languages($entity_type, $entity);
  574. if (isset($enabled_languages[$langcode])) {
  575. $edit_form_item = array_shift($args);
  576. $access_callback = isset($edit_form_item['access callback']) ? $edit_form_item['access callback'] : 'user_access';
  577. return _entity_translation_callback($access_callback, $args, $edit_form_item);
  578. }
  579. }
  580. return FALSE;
  581. }
  582. /**
  583. * Page callback.
  584. */
  585. function entity_translation_add_page() {
  586. $args = func_get_args();
  587. $entity_type = array_shift($args);
  588. $entity = array_shift($args);
  589. $source = array_shift($args);
  590. $langcode = array_shift($args);
  591. $edit_form_item = array_shift($args);
  592. $handler = entity_translation_get_handler($entity_type, $entity);
  593. $handler->initPathScheme();
  594. $handler->setFormLanguage($langcode);
  595. $handler->setSourceLanguage($source);
  596. // Display the entity edit form.
  597. return _entity_translation_callback($edit_form_item['page callback'], $args, $edit_form_item);
  598. }
  599. /**
  600. * Helper function. Proxies a callback call including any needed file.
  601. */
  602. function _entity_translation_callback($callback, $args, $info = array()) {
  603. if (isset($info['file'])) {
  604. $path = isset($info['file path']) ? $info['file path'] : drupal_get_path('module', $info['module']);
  605. include_once DRUPAL_ROOT . '/' . $path . '/' . $info['file'];
  606. }
  607. return call_user_func_array($callback, $args);
  608. }
  609. /**
  610. * Implements hook_admin_paths().
  611. */
  612. function entity_translation_admin_paths() {
  613. $paths = array();
  614. foreach (entity_get_info() as $entity_type => $info) {
  615. if (isset($info['translation']['entity_translation']['path schemes']) && entity_translation_enabled($entity_type, NULL, TRUE)) {
  616. foreach ($info['translation']['entity_translation']['path schemes'] as $scheme) {
  617. if (!empty($scheme['admin theme'])) {
  618. if (isset($scheme['translate path'])) {
  619. $translate_path = preg_replace('|%[^/]*|', '*', $scheme['translate path']);
  620. $paths[$translate_path] = TRUE;
  621. $paths["$translate_path/*"] = TRUE;
  622. }
  623. if (isset($scheme['edit path'])) {
  624. $edit_path = preg_replace('|%[^/]*|', '*', $scheme['edit path']);
  625. $paths["$edit_path/*"] = TRUE;
  626. }
  627. }
  628. }
  629. }
  630. }
  631. return $paths;
  632. }
  633. /**
  634. * Access callback.
  635. */
  636. function entity_translation_tab_access($entity_type, $entity) {
  637. if (drupal_multilingual() && (user_access('translate any entity') || user_access("translate $entity_type entities"))) {
  638. $handler = entity_translation_get_handler($entity_type, $entity);
  639. // Ensure $entity holds an entity object and not an id.
  640. $entity = $handler->getEntity();
  641. $enabled = entity_translation_enabled($entity_type, $entity);
  642. return $enabled && $handler->getLanguage() != LANGUAGE_NONE;
  643. }
  644. return FALSE;
  645. }
  646. /**
  647. * Menu loader callback.
  648. */
  649. function entity_translation_language_load($langcode, $entity_type = NULL, $entity = NULL) {
  650. $enabled_languages = entity_translation_languages($entity_type, $entity);
  651. return isset($enabled_languages[$langcode]) ? $langcode : FALSE;
  652. }
  653. /**
  654. * Menu loader callback.
  655. */
  656. function entity_translation_menu_entity_load($entity_id, $entity_type) {
  657. $entities = entity_load($entity_type, array($entity_id));
  658. return $entities[$entity_id];
  659. }
  660. /**
  661. * Implements hook_permission().
  662. */
  663. function entity_translation_permission() {
  664. $permission = array(
  665. 'administer entity translation' => array(
  666. 'title' => t('Administer entity translation'),
  667. 'description' => t('Select which entities can be translated.'),
  668. ),
  669. 'toggle field translatability' => array(
  670. 'title' => t('Toggle field translatability'),
  671. 'description' => t('Toggle translatability of fields performing a bulk update.'),
  672. ),
  673. 'translate any entity' => array(
  674. 'title' => t('Translate any entity'),
  675. 'description' => t('Translate field content for any fieldable entity.'),
  676. ),
  677. );
  678. $workflow = entity_translation_workflow_enabled();
  679. if ($workflow) {
  680. $permission += array(
  681. 'edit translation shared fields' => array(
  682. 'title' => t('Edit shared values'),
  683. 'description' => t('Edit values shared between translations on the entity form.'),
  684. ),
  685. 'edit original values' => array(
  686. 'title' => t('Edit original values'),
  687. 'description' => t('Access any entity form in the original language.'),
  688. ),
  689. );
  690. }
  691. foreach (entity_get_info() as $entity_type => $info) {
  692. if ($info['fieldable'] && entity_translation_enabled($entity_type)) {
  693. $label = !empty($info['label']) ? t($info['label']) : $entity_type;
  694. $permission["translate $entity_type entities"] = array(
  695. 'title' => t('Translate entities of type @type', array('@type' => $label)),
  696. 'description' => t('Translate field content for entities of type @type.', array('@type' => $label)),
  697. );
  698. if ($workflow) {
  699. // Avoid access control for original values on the current entity.
  700. if (empty($info['translation']['entity_translation']['skip original values access'])) {
  701. $permission["edit $entity_type original values"] = array(
  702. 'title' => t('Edit original values on entities of type @type', array('@type' => $label)),
  703. 'description' => t('Access the entity form in the original language for entities of type @type.', array('@type' => $label)),
  704. );
  705. }
  706. // Avoid access control for shared fields on the current entity.
  707. if (empty($info['translation']['entity_translation']['skip shared fields access'])) {
  708. $permission["edit $entity_type translation shared fields"] = array(
  709. 'title' => t('Edit @type shared values.', array('@type' => $label)),
  710. 'description' => t('Edit values shared between translations on @type forms.', array('@type' => $label)),
  711. );
  712. }
  713. }
  714. }
  715. }
  716. return $permission;
  717. }
  718. /**
  719. * Returns TRUE if the translation workflow is enabled.
  720. */
  721. function entity_translation_workflow_enabled() {
  722. return variable_get('entity_translation_workflow_enabled', FALSE);
  723. }
  724. /**
  725. * Implements hook_theme().
  726. */
  727. function entity_translation_theme() {
  728. return array(
  729. 'entity_translation_unavailable' => array(
  730. 'variables' => array('element' => NULL),
  731. ),
  732. 'entity_translation_language_tabs' => array(
  733. 'render element' => 'element',
  734. ),
  735. 'entity_translation_overview' => array(
  736. 'variables' => array('rows' => NULL, 'header' => NULL),
  737. 'file' => 'entity_translation.admin.inc'
  738. ),
  739. 'entity_translation_overview_outdated' => array(
  740. 'variables' => array('message' => NULL),
  741. 'file' => 'entity_translation.admin.inc'
  742. ),
  743. );
  744. }
  745. /**
  746. * Implements hook_entity_load().
  747. */
  748. function entity_translation_entity_load($entities, $entity_type) {
  749. if (entity_translation_enabled($entity_type)) {
  750. EntityTranslationDefaultHandler::loadMultiple($entity_type, $entities);
  751. }
  752. }
  753. /**
  754. * Implements hook_field_extra_fields().
  755. */
  756. function entity_translation_field_extra_fields() {
  757. $extra = array();
  758. $enabled = variable_get('entity_translation_entity_types', array());
  759. $info = entity_get_info();
  760. foreach ($enabled as $entity_type) {
  761. if (entity_translation_enabled($entity_type)) {
  762. $bundles = !empty($info[$entity_type]['bundles']) ? array_keys($info[$entity_type]['bundles']) : array($entity_type);
  763. foreach ($bundles as $bundle) {
  764. // @todo Clean this up in https://www.drupal.org/node/1661348.
  765. if ($entity_type == 'taxonomy_term') {
  766. $vocabulary = taxonomy_vocabulary_machine_name_load($bundle);
  767. if ($vocabulary && module_invoke('i18n_taxonomy', 'vocabulary_mode', $vocabulary, 4)) {
  768. continue;
  769. }
  770. }
  771. $settings = entity_translation_settings($entity_type, $bundle);
  772. if (empty($settings['hide_language_selector']) && entity_translation_enabled_bundle($entity_type, $bundle) && ($handler = entity_translation_get_handler($entity_type, $bundle))) {
  773. $language_key = $handler->getLanguageKey();
  774. $extra[$entity_type][$bundle] = array(
  775. 'form' => array(
  776. $language_key => array(
  777. 'label' => t('Language'),
  778. 'description' => t('Language selection'),
  779. 'weight' => 5,
  780. ),
  781. ),
  782. );
  783. }
  784. }
  785. }
  786. }
  787. return $extra;
  788. }
  789. /**
  790. * Implements hook_field_language_alter().
  791. *
  792. * Performs language fallback for unaccessible translations.
  793. */
  794. function entity_translation_field_language_alter(&$display_language, $context) {
  795. if (variable_get('locale_field_language_fallback', TRUE) && entity_translation_enabled($context['entity_type'])) {
  796. $entity = $context['entity'];
  797. $entity_type = $context['entity_type'];
  798. $handler = entity_translation_get_handler($entity_type, $entity);
  799. $translations = $handler->getTranslations();
  800. // Apply fallback only on unpublished translations as missing translations
  801. // are already handled in locale_field_language_alter().
  802. if (isset($translations->data[$context['language']]) && !entity_translation_access($entity_type, $translations->data[$context['language']])) {
  803. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  804. $instances = field_info_instances($entity_type, $bundle);
  805. $entity = clone($entity);
  806. foreach ($translations->data as $langcode => $translation) {
  807. if ($langcode == $context['language'] || !entity_translation_access($entity_type, $translations->data[$langcode])) {
  808. // Unset unaccessible field translations: if the field is
  809. // untranslatable unsetting a language different from LANGUAGE_NONE
  810. // has no effect.
  811. foreach ($instances as $instance) {
  812. unset($entity->{$instance['field_name']}[$langcode]);
  813. }
  814. }
  815. }
  816. // Find the new fallback values.
  817. locale_field_language_fallback($display_language, $entity, $context['language']);
  818. }
  819. elseif (!field_has_translation_handler($entity_type, 'locale')) {
  820. // If not handled by the Locale translation handler trigger fallback too.
  821. locale_field_language_fallback($display_language, $entity, $context['language']);
  822. }
  823. }
  824. }
  825. /**
  826. * Implements hook_field_attach_view_alter().
  827. *
  828. * Hide the entity if no translation is available for the current language and
  829. * language fallback is disabled.
  830. */
  831. function entity_translation_field_attach_view_alter(&$output, $context) {
  832. if (!variable_get('locale_field_language_fallback', TRUE) && entity_translation_enabled($context['entity_type'])) {
  833. $handler = entity_translation_get_handler($context['entity_type'], $context['entity']);
  834. $translations = $handler->getTranslations();
  835. $langcode = !empty($context['language']) ? $context['language'] : $GLOBALS['language_content']->language;
  836. // If fallback is disabled we need to notify the user that the translation
  837. // is unavailable (missing or unpublished).
  838. if (!empty($translations->data) && ((!isset($translations->data[$langcode]) && !isset($translations->data[LANGUAGE_NONE])) || ((isset($translations->data[$langcode]) && !entity_translation_access($context['entity_type'], $translations->data[$langcode]))))) {
  839. // Provide context for rendering.
  840. $output['#entity'] = $context['entity'];
  841. $output['#entity_type'] = $context['entity_type'];
  842. $output['#view_mode'] = $context['view_mode'];
  843. // We perform theming here because the theming function might need to set
  844. // system messages. It would be too late in the #post_render callback.
  845. $output['#entity_translation_unavailable'] = theme('entity_translation_unavailable', array('element' => $output));
  846. // As we used a string key, other modules implementing
  847. // hook_field_attach_view_alter() may unset/override this.
  848. $output['#post_render']['entity_translation'] = 'entity_translation_unavailable';
  849. }
  850. }
  851. }
  852. /**
  853. * Override the entity output with the unavailable translation one.
  854. */
  855. function entity_translation_unavailable($children, $element) {
  856. return $element['#entity_translation_unavailable'];
  857. }
  858. /**
  859. * Theme an unvailable translation.
  860. */
  861. function theme_entity_translation_unavailable($variables) {
  862. $element = $variables['element'];
  863. $handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']);
  864. $args = array('%language' => t($GLOBALS['language_content']->name), '%label' => $handler->getLabel());
  865. $message = t('%language translation unavailable for %label.', $args);
  866. $classes = $element['#entity_type'] . ' ' . $element['#entity_type'] . '-' . $element['#view_mode'];
  867. return "<div class=\"$classes\"><div class=\"messages warning\">$message</div></div>";
  868. }
  869. /**
  870. * Implements hook_field_info_alter().
  871. */
  872. function entity_translation_field_info_alter(&$info) {
  873. $columns = array('fid');
  874. $supported_types = array('file' => $columns, 'image' => $columns);
  875. foreach ($info as $field_type => &$field_type_info) {
  876. // Store columns to be synchronized.
  877. if (!isset($field_type_info['settings'])) {
  878. $field_type_info['settings'] = array();
  879. }
  880. $field_type_info['settings'] += array(
  881. 'entity_translation_sync' => isset($supported_types[$field_type]) ? $supported_types[$field_type] : FALSE,
  882. );
  883. // Synchronization can be enabled per instance.
  884. if (!isset($field_type_info['instance_settings'])) {
  885. $field_type_info['instance_settings'] = array();
  886. }
  887. $field_type_info['instance_settings'] += array(
  888. 'entity_translation_sync' => FALSE,
  889. );
  890. }
  891. }
  892. /**
  893. * Implements hook_field_attach_presave().
  894. */
  895. function entity_translation_field_attach_presave($entity_type, $entity) {
  896. if (entity_translation_enabled($entity_type, $entity)) {
  897. entity_translation_sync($entity_type, $entity);
  898. }
  899. }
  900. /**
  901. * Performs field column synchronization.
  902. */
  903. function entity_translation_sync($entity_type, $entity) {
  904. // If we are creating a new entity or if we have no translations for the
  905. // current entity, there is nothing to synchronize.
  906. $handler = entity_translation_get_handler($entity_type, $entity, TRUE);
  907. $translations = $handler->getTranslations();
  908. $original_langcode = $handler->getSourceLanguage();
  909. if ($handler->isNewEntity() || (count($translations->data) < 2 && !$original_langcode)) {
  910. return;
  911. }
  912. list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  913. $instances = field_info_instances($entity_type, $bundle);
  914. $entity_unchanged = isset($entity->original) ? $entity->original : entity_load_unchanged($entity_type, $id);
  915. // If the entity language is being changed there is nothing to synchronize.
  916. $langcode = $handler->getLanguage();
  917. $handler->setEntity($entity_unchanged);
  918. if ($langcode != $handler->getLanguage()) {
  919. return;
  920. }
  921. foreach ($instances as $field_name => $instance) {
  922. $field = field_info_field($field_name);
  923. // If the field is empty there is nothing to synchronize. Synchronization
  924. // makes sense only for translatable fields.
  925. if (!empty($entity->{$field_name}) && !empty($instance['settings']['entity_translation_sync']) && field_is_translatable($entity_type, $field)) {
  926. $columns = $field['settings']['entity_translation_sync'];
  927. $change_map = array();
  928. $source_langcode = entity_language($entity_type, $entity);
  929. $source_items = $entity->{$field_name}[$source_langcode];
  930. // If a translation is being created, the original values should be used
  931. // as the unchanged items. In fact there are no unchanged items to check
  932. // against.
  933. $langcode = $original_langcode ? $original_langcode : $source_langcode;
  934. $unchanged_items = !empty($entity_unchanged->{$field_name}[$langcode]) ? $entity_unchanged->{$field_name}[$langcode] : array();
  935. // By picking the maximum size between updated and unchanged items, we
  936. // make sure to process also removed items.
  937. $total = max(array(count($source_items), count($unchanged_items)));
  938. // Make sure we can detect any change in the source items.
  939. for ($delta = 0; $delta < $total; $delta++) {
  940. foreach ($columns as $column) {
  941. // Store the delta for the unchanged column value.
  942. if (isset($unchanged_items[$delta][$column])) {
  943. $value = $unchanged_items[$delta][$column];
  944. $change_map[$column][$value]['old'] = $delta;
  945. }
  946. // Store the delta for the new column value.
  947. if (isset($source_items[$delta][$column])) {
  948. $value = $source_items[$delta][$column];
  949. $change_map[$column][$value]['new'] = $delta;
  950. }
  951. }
  952. }
  953. // Backup field values.
  954. $field_values = $entity->{$field_name};
  955. // Reset field values so that no spurious translation value is stored.
  956. // Source values and anything else must be preserved in any case.
  957. $entity->{$field_name} = array($source_langcode => $source_items) + array_diff_key($entity->{$field_name}, $translations->data);
  958. // Update translations.
  959. foreach ($translations->data as $langcode => $translation) {
  960. // We need to synchronize only values different from the source ones.
  961. if ($langcode != $source_langcode) {
  962. // Process even removed items.
  963. for ($delta = 0; $delta < $total; $delta++) {
  964. $created = TRUE;
  965. $removed = TRUE;
  966. foreach ($columns as $column) {
  967. if (isset($source_items[$delta][$column])) {
  968. $value = $source_items[$delta][$column];
  969. $created = $created && !isset($change_map[$column][$value]['old']);
  970. $removed = $removed && !isset($change_map[$column][$value]['new']);
  971. }
  972. }
  973. // If an item has been removed we do not store its translations.
  974. if ($removed) {
  975. // Ensure items are actually removed.
  976. if (!isset($entity->{$field_name}[$langcode])) {
  977. $entity->{$field_name}[$langcode] = array();
  978. }
  979. continue;
  980. }
  981. // If a synchronized column has changed we need to override the full
  982. // items array for all languages.
  983. elseif ($created) {
  984. $entity->{$field_name}[$langcode][$delta] = $source_items[$delta];
  985. }
  986. // The current item might have been reordered.
  987. elseif (!empty($change_map[$column][$value])) {
  988. $old_delta = $change_map[$column][$value]['old'];
  989. $new_delta = $change_map[$column][$value]['new'];
  990. // If for nay reason the old value is not defined for the current
  991. // we language we fall back to the new source value.
  992. $items = isset($field_values[$langcode][$old_delta]) ? $field_values[$langcode][$old_delta] : $source_items[$new_delta];
  993. $entity->{$field_name}[$langcode][$new_delta] = $items;
  994. }
  995. }
  996. }
  997. }
  998. }
  999. }
  1000. }
  1001. /**
  1002. * Implements hook_field_attach_insert().
  1003. */
  1004. function entity_translation_field_attach_insert($entity_type, $entity) {
  1005. // Store entity translation metadata only if the entity bundle is
  1006. // translatable.
  1007. if (entity_translation_enabled($entity_type, $entity)) {
  1008. $handler = entity_translation_get_handler($entity_type, $entity);
  1009. $handler->initTranslations();
  1010. $handler->saveTranslations();
  1011. }
  1012. }
  1013. /**
  1014. * Implements hook_field_attach_update().
  1015. */
  1016. function entity_translation_field_attach_update($entity_type, $entity) {
  1017. // Store entity translation metadata only if the entity bundle is
  1018. // translatable.
  1019. if (entity_translation_enabled($entity_type, $entity)) {
  1020. $handler = entity_translation_get_handler($entity_type, $entity, TRUE);
  1021. $handler->updateTranslations();
  1022. $handler->saveTranslations();
  1023. }
  1024. }
  1025. /**
  1026. * Implements hook_field_attach_delete().
  1027. */
  1028. function entity_translation_field_attach_delete($entity_type, $entity) {
  1029. if (entity_translation_enabled($entity_type, $entity)) {
  1030. $handler = entity_translation_get_handler($entity_type, $entity);
  1031. $handler->removeTranslations();
  1032. $handler->saveTranslations();
  1033. }
  1034. }
  1035. /**
  1036. * Implements hook_field_attach_delete_revision().
  1037. */
  1038. function entity_translation_field_attach_delete_revision($entity_type, $entity) {
  1039. if (entity_translation_enabled($entity_type, $entity)) {
  1040. $handler = entity_translation_get_handler($entity_type, $entity);
  1041. $handler->removeRevisionTranslations();
  1042. $handler->saveTranslations();
  1043. }
  1044. }
  1045. /**
  1046. * Implementation of hook_field_attach_form().
  1047. */
  1048. function entity_translation_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  1049. // Avoid recursing into the source form.
  1050. list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  1051. if (empty($form['#entity_translation_source_form']) && entity_translation_enabled($entity_type, $bundle)) {
  1052. $handler = entity_translation_get_handler($entity_type, $entity);
  1053. $langcode = !empty($langcode) ? $langcode : $handler->getLanguage();
  1054. $form_langcode = $handler->getFormLanguage();
  1055. $translations = $handler->getTranslations();
  1056. $update_langcode = $form_langcode && ($form_langcode != $langcode);
  1057. $source = $handler->getSourceLanguage();
  1058. $new_translation = !isset($translations->data[$form_langcode]);
  1059. // If we are creating a new translation we need to retrieve form elements
  1060. // populated with the source language values, but only if form is not being
  1061. // rebuilt. In this case source values have already been populated, so we
  1062. // need to preserve possible changes. There might be situations, e.g. ajax
  1063. // calls, where the form language has not been properly initialized before
  1064. // calling field_attach_form(). In this case we need to rebuild the form
  1065. // with the correct form language and replace the field elements with the
  1066. // correct ones.
  1067. if ($update_langcode || ($source && !isset($translations->data[$form_langcode]) && isset($translations->data[$source]) && empty($form_state['rebuild']))) {
  1068. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  1069. $field_name = $instance['field_name'];
  1070. $field = field_info_field($field_name);
  1071. // If we are creating a new translation we have to change the form item
  1072. // language information from source to target language, this way the
  1073. // user can find the form items already populated with the source values
  1074. // while the field form element holds the correct language information.
  1075. if ($field['translatable']) {
  1076. $element = &$form[$field_name];
  1077. $element['#entity_type'] = $entity_type;
  1078. $element['#entity'] = $entity;
  1079. $element['#entity_id'] = $id;
  1080. $element['#field_name'] = $field_name;
  1081. $element['#source'] = $update_langcode ? $form_langcode : $source;
  1082. $element['#previous'] = NULL;
  1083. $element['#form_parents'] = $form['#parents'];
  1084. // If we are updating the form language we need to make sure that the
  1085. // wrong language is unset and the right one is stored in the field
  1086. // element (see entity_translation_prepare_element()).
  1087. if ($update_langcode) {
  1088. $element['#previous'] = $element['#language'];
  1089. $element['#language'] = $form_langcode;
  1090. }
  1091. // Swap default values during form processing to avoid recursion. We
  1092. // try to act before any other callback so that the correct values are
  1093. // already in place for them.
  1094. if (!isset($element['#process'])) {
  1095. $element['#process'] = array();
  1096. }
  1097. array_unshift($element['#process'], 'entity_translation_prepare_element');
  1098. }
  1099. }
  1100. }
  1101. // Handle fields shared between translations when there is at least one
  1102. // translation available or a new one is being created.
  1103. if (!$handler->isNewEntity() && ($new_translation || count($translations->data) > 1)) {
  1104. $shared_access = $handler->getSharedFieldsAccess();
  1105. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  1106. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  1107. $field_name = $instance['field_name'];
  1108. $field = field_info_field($field_name);
  1109. // If access is not set or is granted we check whether the user has
  1110. // access to shared fields.
  1111. $form[$field_name]['#access'] = (!isset($form[$field_name]['#access']) || $form[$field_name]['#access']) && ($field['translatable'] || $shared_access);
  1112. $form[$field_name]['#multilingual'] = (boolean) $field['translatable'];
  1113. }
  1114. }
  1115. // If a translation is being created and no path alias exists for its
  1116. // language, by default an alias needs to be generated. The standard
  1117. // behavior is defaulting to FALSE when an entity already exists, hence we
  1118. // need to override it here.
  1119. if (module_exists('pathauto') && $handler->getSourceLanguage()) {
  1120. $entity->path['pathauto'] = TRUE;
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. * Form element process callback.
  1126. */
  1127. function entity_translation_prepare_element($element, &$form_state) {
  1128. static $drupal_static_fast;
  1129. if (!isset($drupal_static_fast)) {
  1130. $drupal_static_fast = &drupal_static(__FUNCTION__, array(
  1131. 'source_forms' => array(),
  1132. 'source_form_states' => array(),
  1133. ));
  1134. }
  1135. $source_forms = &$drupal_static_fast['source_forms'];
  1136. $source_form_states = &$drupal_static_fast['source_form_states'];
  1137. $form = $form_state['complete form'];
  1138. $build_id = $form['#build_id'];
  1139. $source = $element['#source'];
  1140. $entity_type = $element['#entity_type'];
  1141. $id = $element['#entity_id'];
  1142. // Key the source form cache per entity type and entity id to allow for
  1143. // multiple entities on the same entity form.
  1144. if (!isset($source_forms[$build_id][$source][$entity_type][$id])) {
  1145. $source_form = array(
  1146. '#entity_translation_source_form' => TRUE,
  1147. '#parents' => $element['#form_parents'],
  1148. );
  1149. $source_form_state = $form_state;
  1150. field_attach_form($entity_type, $element['#entity'], $source_form, $source_form_state, $source);
  1151. $source_forms[$build_id][$source][$entity_type][$id] = &$source_form;
  1152. $source_form_states[$build_id][$source][$entity_type][$id] = &$source_form_state;
  1153. }
  1154. $source_form = &$source_forms[$build_id][$source][$entity_type][$id];
  1155. $source_form_state = $source_form_states[$build_id][$source][$entity_type][$id];
  1156. $langcode = $element['#language'];
  1157. $field_name = $element['#field_name'];
  1158. // If we are creating a new translation we have to change the form item
  1159. // language information from source to target language, this way the user can
  1160. // find the form items already populated with the source values while the
  1161. // field form element holds the correct language information.
  1162. if (isset($source_form[$field_name][$source])) {
  1163. $element[$langcode] = $source_form[$field_name][$source];
  1164. entity_translation_form_element_language_replace($element, $source, $langcode);
  1165. entity_translation_form_element_state_replace($element, $source_form[$field_name], $field_name, $source_form_state, $form_state);
  1166. unset($element[$element['#previous']]);
  1167. }
  1168. return $element;
  1169. }
  1170. /**
  1171. * Helper function. Sets the right values in $form_state['field'] when using
  1172. * source language values as defaults.
  1173. */
  1174. function entity_translation_form_element_state_replace($element, $source_element, $field_name, $source_form_state, &$form_state) {
  1175. if (isset($source_element['#language'])) {
  1176. $source = $source_element['#language'];
  1177. // Iterate through the form structure recursively.
  1178. foreach (element_children($element) as $key) {
  1179. if (isset($source_element[$key])) {
  1180. entity_translation_form_element_state_replace($element[$key], $source_element[$key], $key, $source_form_state, $form_state);
  1181. }
  1182. elseif (isset($source_element[$source])) {
  1183. entity_translation_form_element_state_replace($element[$key], $source_element[$source], $key, $source_form_state, $form_state);
  1184. }
  1185. }
  1186. if (isset($source_element[$source]['#field_parents'])) {
  1187. $source_parents = $source_element[$source]['#field_parents'];
  1188. $langcode = $element['#language'];
  1189. $parents = $element[$langcode]['#field_parents'];
  1190. $source_state = field_form_get_state($source_parents, $field_name, $source, $source_form_state);
  1191. drupal_alter('entity_translation_source_field_state', $source_state);
  1192. field_form_set_state($parents, $field_name, $langcode, $form_state, $source_state);
  1193. }
  1194. }
  1195. }
  1196. /**
  1197. * Helper function. Recursively replaces the source language with the given one.
  1198. */
  1199. function entity_translation_form_element_language_replace(&$element, $source, $langcode) {
  1200. // Iterate through the form structure recursively.
  1201. foreach (element_children($element) as $key) {
  1202. entity_translation_form_element_language_replace($element[$key], $source, $langcode);
  1203. }
  1204. // Replace specific occurrences of the source language with the target
  1205. // language.
  1206. foreach (element_properties($element) as $key) {
  1207. if ($key === '#language' && $element[$key] != LANGUAGE_NONE) {
  1208. $element[$key] = $langcode;
  1209. }
  1210. elseif ($key === '#parents' || $key === '#field_parents') {
  1211. foreach ($element[$key] as $delta => $value) {
  1212. if ($value === $source) {
  1213. $element[$key][$delta] = $langcode;
  1214. }
  1215. }
  1216. }
  1217. elseif ($key === '#limit_validation_errors') {
  1218. foreach ($element[$key] as $section => $section_value) {
  1219. foreach ($element[$key][$section] as $delta => $value) {
  1220. if ($value === $source) {
  1221. $element[$key][$section][$delta] = $langcode;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. }
  1227. }
  1228. /**
  1229. * Adds visual clues about the translatability of a field to the given element.
  1230. *
  1231. * Field titles are appended with the string "Shared" for fields which are
  1232. * shared between different translations. Moreover fields receive a CSS class to
  1233. * distinguish between translatable and shared fields.
  1234. */
  1235. function entity_translation_element_translatability_clue($element) {
  1236. // Append language to element title.
  1237. if (empty($element['#multilingual'])) {
  1238. _entity_translation_element_title_append($element, ' (' . t('all languages') . ')');
  1239. }
  1240. // Add CSS class names.
  1241. if (!isset($element['#attributes'])) {
  1242. $element['#attributes'] = array();
  1243. }
  1244. if (!isset($element['#attributes']['class'])) {
  1245. $element['#attributes']['class'] = array();
  1246. }
  1247. $element['#attributes']['class'][] = 'entity-translation-' . (!empty($element['#multilingual']) ? 'field-translatable' : 'field-shared');
  1248. return $element;
  1249. }
  1250. /**
  1251. * Adds a callback function to the given FAPI element.
  1252. *
  1253. * Drupal core only adds default element callbacks if the respective handler
  1254. * type is not defined yet. This function ensures that our callback is only
  1255. * prepended/appended to the default set of callbacks instead of replacing it.
  1256. *
  1257. * @param $element
  1258. * The FAPI element.
  1259. * @param $type
  1260. * The callback type, e.g. '#pre_render' or '#process'.
  1261. * @param $function
  1262. * The name of the callback to add.
  1263. * @param $prepend
  1264. * Set to TRUE to add the new callback to the beginning of the existing set of
  1265. * callbacks, and set it to FALSE to append it at the end.
  1266. */
  1267. function _entity_translation_element_add_callback(&$element, $type, $function, $prepend = TRUE) {
  1268. // If handler type has not been set, add defaults from element_info().
  1269. if (!isset($element[$type])) {
  1270. $element_type = isset($element['#type']) ? $element['#type'] : 'markup';
  1271. $element_info = element_info($element_type);
  1272. $element[$type] = isset($element_info[$type]) ? $element_info[$type] : array();
  1273. }
  1274. if ($prepend) {
  1275. array_unshift($element[$type], $function);
  1276. }
  1277. else {
  1278. $element[$type][] = $function;
  1279. }
  1280. }
  1281. /**
  1282. * Appends the given $suffix string to the title of the given form element.
  1283. *
  1284. * If the given element does not have a #title attribute, the function is
  1285. * recursively applied to child elements.
  1286. */
  1287. function _entity_translation_element_title_append(&$element, $suffix) {
  1288. static $fapi_title_elements;
  1289. // Elements which can have a #title attribute according to FAPI Reference.
  1290. if (!isset($fapi_title_elements)) {
  1291. $fapi_title_elements = array_flip(array('checkbox', 'checkboxes', 'date', 'fieldset', 'file', 'item', 'password', 'password_confirm', 'radio', 'radios', 'select', 'text_format', 'textarea', 'textfield', 'weight'));
  1292. }
  1293. // Update #title attribute for all elements that are allowed to have a #title
  1294. // attribute according to the Form API Reference. The reason for this check
  1295. // is because some elements have a #title attribute even though it is not
  1296. // rendered, e.g. field containers.
  1297. if (isset($element['#type']) && isset($fapi_title_elements[$element['#type']]) && isset($element['#title'])) {
  1298. $element['#title'] .= $suffix;
  1299. }
  1300. // If the current element does not have a (valid) title, try child elements.
  1301. elseif ($children = element_children($element)) {
  1302. foreach ($children as $delta) {
  1303. _entity_translation_element_title_append($element[$delta], $suffix);
  1304. }
  1305. }
  1306. // If there are no children, fall back to the current #title attribute if it
  1307. // exists.
  1308. elseif (isset($element['#title'])) {
  1309. $element['#title'] .= $suffix;
  1310. }
  1311. }
  1312. /**
  1313. * Implements hook_form_alter().
  1314. */
  1315. function entity_translation_form_alter(&$form, &$form_state) {
  1316. if ($info = entity_translation_edit_form_info($form, $form_state)) {
  1317. $handler = entity_translation_get_handler($info['entity type'], $info['entity']);
  1318. if (entity_translation_enabled($info['entity type'], $info['entity'])) {
  1319. if (!$handler->isNewEntity()) {
  1320. $handler->entityForm($form, $form_state);
  1321. $translations = $handler->getTranslations();
  1322. $form_langcode = $handler->getFormLanguage();
  1323. if (!isset($translations->data[$form_langcode]) || count($translations->data) > 1) {
  1324. // Hide shared form elements if the user is not allowed to edit them.
  1325. $handler->entityFormSharedElements($form);
  1326. }
  1327. }
  1328. else {
  1329. $handler->entityFormLanguageWidget($form, $form_state);
  1330. }
  1331. // We need to process the posted form as early as possible to update the
  1332. // form language value.
  1333. array_unshift($form['#validate'], 'entity_translation_entity_form_validate');
  1334. }
  1335. // We might have an entity form for an entity or a bundle not enabled for
  1336. // translation. In this case we might need to deal with entity and field
  1337. // languages anyway, since fields may be shared among different bundles and
  1338. // entity types.
  1339. else {
  1340. $handler->entityFormLanguageWidget($form, $form_state);
  1341. }
  1342. }
  1343. }
  1344. /**
  1345. * Submit handler for the source language selector.
  1346. */
  1347. function entity_translation_entity_form_source_language_submit($form, &$form_state) {
  1348. $handler = entity_translation_entity_form_get_handler($form, $form_state);
  1349. $langcode = $form_state['values']['source_language']['language'];
  1350. $path = "{$handler->getEditPath()}/add/$langcode/{$handler->getFormLanguage()}";
  1351. $options = array();
  1352. if (isset($_GET['destination'])) {
  1353. $options['query'] = drupal_get_destination();
  1354. unset($_GET['destination']);
  1355. }
  1356. $form_state['redirect'] = array($path, $options);
  1357. $languages = language_list();
  1358. drupal_set_message(t('Source translation set to: %language', array('%language' => t($languages[$langcode]->name))));
  1359. }
  1360. /**
  1361. * Submit handler for the translation deletion.
  1362. */
  1363. function entity_translation_entity_form_delete_translation_submit($form, &$form_state) {
  1364. $handler = entity_translation_entity_form_get_handler($form, $form_state);
  1365. $path = "{$handler->getTranslatePath()}/delete/{$handler->getFormLanguage()}";
  1366. $options = array();
  1367. if (isset($_GET['destination'])) {
  1368. $options['query'] = drupal_get_destination();
  1369. unset($_GET['destination']);
  1370. }
  1371. $form_state['redirect'] = array($path, $options);
  1372. }
  1373. /**
  1374. * Validation handler for the entity edit form.
  1375. */
  1376. function entity_translation_entity_form_validate($form, &$form_state) {
  1377. $handler = entity_translation_entity_form_get_handler($form, $form_state);
  1378. if (!empty($handler)) {
  1379. $handler->entityFormValidate($form, $form_state);
  1380. }
  1381. }
  1382. /**
  1383. * Validation handler for the entity language widget.
  1384. */
  1385. function entity_translation_entity_form_language_update($element, &$form_state, $form) {
  1386. $handler = entity_translation_entity_form_get_handler($form, $form_state);
  1387. // Ensure the handler form language match the actual one. This is mainly
  1388. // needed when responding to an AJAX request where the languages cannot be set
  1389. // from the usual page callback.
  1390. if (!empty($form_state['entity_translation']['form_langcode'])) {
  1391. $handler->setFormLanguage($form_state['entity_translation']['form_langcode']);
  1392. }
  1393. // When responding to an AJAX request we should ignore any change in the
  1394. // language widget as it may alter the field language expected by the AJAX
  1395. // callback.
  1396. if (empty($form_state['triggering_element']['#ajax'])) {
  1397. $handler->entityFormLanguageWidgetSubmit($form, $form_state);
  1398. }
  1399. }
  1400. /**
  1401. * Submit handler for the entity deletion.
  1402. */
  1403. function entity_translation_entity_form_submit($form, &$form_state) {
  1404. if ($form_state['clicked_button']['#value'] == t('Delete')) {
  1405. $handler = entity_translation_entity_form_get_handler($form, $form_state);
  1406. if (count($handler->getTranslations()->data) > 1) {
  1407. $info = entity_get_info($form['#entity_type']);
  1408. drupal_set_message(t('This will delete all the @entity_type translations.', array('@entity_type' => drupal_strtolower($info['label']))), 'warning');
  1409. }
  1410. }
  1411. }
  1412. /**
  1413. * Implementation of hook_field_attach_submit().
  1414. *
  1415. * Mark translations as outdated if the submitted value is true.
  1416. */
  1417. function entity_translation_field_attach_submit($entity_type, $entity, $form, &$form_state) {
  1418. if (($handler = entity_translation_entity_form_get_handler($form, $form_state)) && entity_translation_enabled($entity_type, $entity)) {
  1419. // Update the wrapped entity with the submitted values.
  1420. $handler->setEntity($entity);
  1421. $handler->entityFormSubmit($form, $form_state);
  1422. }
  1423. }
  1424. /**
  1425. * Implements hook_menu_local_tasks_alter().
  1426. */
  1427. function entity_translation_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  1428. // When displaying the main edit form, we need to craft an additional level of
  1429. // local tasks for each available translation.
  1430. $handler = entity_translation_get_handler();
  1431. if (!empty($handler) && $handler->isEntityForm() && $handler->getLanguage() != LANGUAGE_NONE && drupal_multilingual()) {
  1432. $handler->localTasksAlter($data, $router_item, $root_path);
  1433. }
  1434. }
  1435. /**
  1436. * Preprocess variables for 'page.tpl.php'.
  1437. */
  1438. function entity_translation_preprocess_page(&$variables) {
  1439. if (!empty($variables['tabs']['#secondary'])) {
  1440. $language_tabs = array();
  1441. foreach ($variables['tabs']['#secondary'] as $index => $tab) {
  1442. if (!empty($tab['#language_tab'])) {
  1443. $language_tabs[] = $tab;
  1444. unset($variables['tabs']['#secondary'][$index]);
  1445. }
  1446. }
  1447. if (!empty($language_tabs)) {
  1448. if (count($variables['tabs']['#secondary']) <= 1) {
  1449. $variables['tabs']['#secondary'] = $language_tabs;
  1450. }
  1451. else {
  1452. // If secondary tabs are already defined we need to add another level
  1453. // and wrap it so that it will be positioned on its own row.
  1454. $variables['tabs']['#secondary']['#language_tabs'] = $language_tabs;
  1455. $variables['tabs']['#secondary']['#pre_render']['entity_translation'] = 'entity_translation_language_tabs_render';
  1456. }
  1457. }
  1458. }
  1459. }
  1460. /**
  1461. * Pre render callback.
  1462. *
  1463. * Appends the language tabs to the current local tasks area.
  1464. */
  1465. function entity_translation_language_tabs_render($element) {
  1466. $build = array(
  1467. '#theme' => 'menu_local_tasks',
  1468. '#theme_wrappers' => array('entity_translation_language_tabs'),
  1469. '#secondary' => $element['#language_tabs'],
  1470. '#attached' => array(
  1471. 'css' => array(drupal_get_path('module', 'entity_translation') . '/entity-translation.css'),
  1472. ),
  1473. );
  1474. $element['#suffix'] .= drupal_render($build);
  1475. return $element;
  1476. }
  1477. /**
  1478. * Theme wrapper for the entity translation language tabs.
  1479. */
  1480. function theme_entity_translation_language_tabs($variables) {
  1481. return '<div class="entity-translation-language-tabs">' . $variables['element']['#children'] . '</div>';
  1482. }
  1483. /**
  1484. * Implements hook_form_FORM_ID_alter().
  1485. *
  1486. * Adds an option to enable field synchronization.
  1487. * Enable a selector to choose whether a field is translatable.
  1488. */
  1489. function entity_translation_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  1490. $instance = $form['#instance'];
  1491. $entity_type = $instance['entity_type'];
  1492. $field_name = $instance['field_name'];
  1493. $field = field_info_field($field_name);
  1494. if (!empty($field['settings']['entity_translation_sync']) && field_is_translatable($entity_type, $field)) {
  1495. $form['instance']['settings']['entity_translation_sync'] = array(
  1496. '#prefix' => '<label>' . t('Field synchronization') . '</label>',
  1497. '#type' => 'checkbox',
  1498. '#title' => t('Enable field synchronization'),
  1499. '#description' => t('Check this option if you wish to synchronize the value of this field across its translations.'),
  1500. '#default_value' => !empty($instance['settings']['entity_translation_sync']),
  1501. );
  1502. }
  1503. $translatable = $field['translatable'];
  1504. $label = t('Field translation');
  1505. $title = t('Users may translate all occurrences of this field:') . _entity_translation_field_desc($field);
  1506. if (field_has_data($field)) {
  1507. $path = "admin/config/regional/entity_translation/translatable/$field_name";
  1508. $status = $translatable ? $title : (t('All occurrences of this field are untranslatable:') . _entity_translation_field_desc($field));
  1509. $link_title = !$translatable ? t('Enable translation') : t('Disable translation');
  1510. $form['field']['translatable'] = array(
  1511. '#prefix' => '<div class="translatable"><label>' . $label . '</label>',
  1512. '#suffix' => '</div>',
  1513. 'message' => array(
  1514. '#markup' => $status . ' ',
  1515. ),
  1516. 'link' => array(
  1517. '#type' => 'link',
  1518. '#title' => $link_title,
  1519. '#href' => $path,
  1520. '#options' => array('query' => drupal_get_destination()),
  1521. '#access' => user_access('toggle field translatability'),
  1522. ),
  1523. );
  1524. }
  1525. else {
  1526. $form['field']['translatable'] = array(
  1527. '#prefix' => '<label>' . $label . '</label>',
  1528. '#type' => 'checkbox',
  1529. '#title' => $title,
  1530. '#default_value' => $translatable,
  1531. );
  1532. }
  1533. }
  1534. /**
  1535. * Returns a human-readable, localized, bullet list of instances of a field.
  1536. *
  1537. * @param field
  1538. * A field data structure.
  1539. *
  1540. * @return
  1541. * A themed list of field instances with the bundle they are attached to.
  1542. */
  1543. function _entity_translation_field_desc($field) {
  1544. $instances = array();
  1545. foreach ($field['bundles'] as $entity_type => $bundle_names) {
  1546. $entity_type_info = entity_get_info($entity_type);
  1547. foreach ($bundle_names as $bundle_name) {
  1548. $instance_info = field_info_instance($entity_type, $field['field_name'], $bundle_name);
  1549. $instances[] = t('@instance_label in %entity_label', array('@instance_label' => $instance_info['label'], '%entity_label' => $entity_type_info['bundles'][$bundle_name]['label']));
  1550. }
  1551. }
  1552. return theme('item_list', array('items' => $instances));
  1553. }
  1554. /**
  1555. * Determines whether the given entity type is translatable.
  1556. *
  1557. * @param $entity_type
  1558. * The entity type enabled for translation.
  1559. * @param $entity
  1560. * (optional) The entity belonging to the bundle enabled for translation. A
  1561. * bundle name can alternatively be passed. If an empty value is passed the
  1562. * bundle-level check is skipped. Defaults to NULL.
  1563. * @param $skip_handler
  1564. * (optional) A boolean indicating whether skip checking if the entity type is
  1565. * registered as a field translation handler. Defaults to FALSE.
  1566. */
  1567. function entity_translation_enabled($entity_type, $entity = NULL, $skip_handler = FALSE) {
  1568. $enabled_types = variable_get('entity_translation_entity_types', array());
  1569. $enabled = !empty($enabled_types[$entity_type]) && ($skip_handler || field_has_translation_handler($entity_type, 'entity_translation'));
  1570. // If the entity type is not enabled or we are not checking bundle status, we
  1571. // have a result.
  1572. if (!$enabled || !isset($entity)) {
  1573. return $enabled;
  1574. }
  1575. // Determine the bundle to check for translatability.
  1576. $bundle = FALSE;
  1577. if (is_object($entity)) {
  1578. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  1579. }
  1580. elseif (is_string($entity)) {
  1581. $bundle = $entity;
  1582. }
  1583. return $bundle && entity_translation_enabled_bundle($entity_type, $bundle);
  1584. }
  1585. /**
  1586. * Determines whether the given entity bundle is translatable.
  1587. *
  1588. * NOTE: Does not check for whether the entity type is translatable.
  1589. * Consider using entity_translation_enabled() instead.
  1590. *
  1591. * @param $entity_type
  1592. * The entity type the bundle to be checked belongs to.
  1593. * @param $bundle
  1594. * The name of the bundle to be checked.
  1595. */
  1596. function entity_translation_enabled_bundle($entity_type, $bundle) {
  1597. $info = entity_get_info($entity_type);
  1598. $bundle_callback = isset($info['translation']['entity_translation']['bundle callback']) ? $info['translation']['entity_translation']['bundle callback'] : FALSE;
  1599. return empty($bundle_callback) || call_user_func($bundle_callback, $bundle);
  1600. }
  1601. /**
  1602. * Return the entity translation settings for the given entity type and bundle.
  1603. */
  1604. function entity_translation_settings($entity_type, $bundle) {
  1605. $settings = variable_get('entity_translation_settings_' . $entity_type . '__' . $bundle, array());
  1606. if (empty($settings)) {
  1607. $info = entity_get_info($entity_type);
  1608. if (!empty($info['translation']['entity_translation']['default settings'])) {
  1609. $settings = $info['translation']['entity_translation']['default settings'];
  1610. }
  1611. }
  1612. $settings += array(
  1613. 'default_language' => ENTITY_TRANSLATION_LANGUAGE_DEFAULT,
  1614. 'hide_language_selector' => TRUE,
  1615. 'exclude_language_none' => FALSE,
  1616. 'lock_language' => FALSE,
  1617. 'shared_fields_original_only' => FALSE,
  1618. );
  1619. return $settings;
  1620. }
  1621. /**
  1622. * Entity language callback.
  1623. *
  1624. * This callback changes the entity language from the actual one to the active
  1625. * form language. This overriding allows to obtain language dependent form
  1626. * widgets where multilingual values are supported (e.g. field or path alias
  1627. * widgets) even if the code was not originally written with supporting multiple
  1628. * values per language in mind.
  1629. *
  1630. * The main drawback of this approach is that code needing to access the actual
  1631. * language in the entity form build/validation/submit workflow cannot rely on
  1632. * the entity_language() function. On the other hand in these scenarios assuming
  1633. * the presence of Entity translation should be safe, thus being able to rely on
  1634. * the EntityTranslationHandlerInterface::getLanguage() method.
  1635. *
  1636. * @param $entity_type
  1637. * The the type of the entity.
  1638. * @param $entity
  1639. * The entity whose language has to be returned.
  1640. *
  1641. * @return
  1642. * A valid language code.
  1643. */
  1644. function entity_translation_language($entity_type, $entity) {
  1645. $handler = entity_translation_get_handler($entity_type, $entity);
  1646. if (empty($handler)) {
  1647. return LANGUAGE_NONE;
  1648. }
  1649. $langcode = $handler->getFormLanguage();
  1650. return !empty($langcode) ? $langcode : $handler->getLanguage();
  1651. }
  1652. /**
  1653. * Translation handler factory.
  1654. *
  1655. * @param $entity_type
  1656. * (optional) The type of $entity; e.g. 'node' or 'user'.
  1657. * @param $entity
  1658. * (optional) The entity to be translated. A bundle name may be passed to
  1659. * instantiate an empty entity.
  1660. *
  1661. * @return EntityTranslationHandlerInterface
  1662. * A class implementing EntityTranslationHandlerInterface.
  1663. */
  1664. function entity_translation_get_handler($entity_type = NULL, $entity = NULL) {
  1665. if (class_exists('EntityTranslationHandlerFactory')) {
  1666. $factory = EntityTranslationHandlerFactory::getInstance();
  1667. return empty($entity) ? $factory->getLastHandler($entity_type) : $factory->getHandler($entity_type, $entity);
  1668. }
  1669. // @todo BC layer. Remove before the first stable release.
  1670. elseif (!empty($entity_type) && is_object($entity)) {
  1671. $entity_info = entity_get_info($entity_type);
  1672. return new EntityTranslationDefaultHandler($entity_type, $entity_info, $entity);
  1673. }
  1674. }
  1675. /**
  1676. * Returns the translation handler wrapping the entity being edited.
  1677. *
  1678. * @param $form
  1679. * The entity form.
  1680. * @param $form_state
  1681. * A keyed array containing the current state of the form.
  1682. *
  1683. * @return EntityTranslationHandlerInterface
  1684. * A class implementing EntityTranslationHandlerInterface.
  1685. */
  1686. function entity_translation_entity_form_get_handler($form, $form_state) {
  1687. $handler = FALSE;
  1688. if ($info = entity_translation_edit_form_info($form, $form_state)) {
  1689. $handler = entity_translation_get_handler($info['entity type'], $info['entity']);
  1690. }
  1691. return $handler;
  1692. }
  1693. /**
  1694. * Returns the translation handler associated to the currently submitted form.
  1695. *
  1696. * @return EntityTranslationHandlerInterface
  1697. * A translation handler instance if available, FALSE oterwise.
  1698. *
  1699. * @deprecated This is no longer used and will be removed in the first stable
  1700. * release.
  1701. */
  1702. function entity_translation_current_form_get_handler() {
  1703. $handler = FALSE;
  1704. if (!empty($_POST['form_build_id'])) {
  1705. $form_state = form_state_defaults();
  1706. if ($form = form_get_cache($_POST['form_build_id'], $form_state)) {
  1707. $handler = entity_translation_entity_form_get_handler($form, $form_state);
  1708. }
  1709. }
  1710. return $handler;
  1711. }
  1712. /**
  1713. * Returns an array of edit form info as defined in hook_translation_info().
  1714. *
  1715. * @param $form
  1716. * The entity edit form.
  1717. * @param $form_state
  1718. * The entity edit form state.
  1719. *
  1720. * @return
  1721. * An edit form info array containing the entity to be translated in the
  1722. * 'entity' key.
  1723. */
  1724. function entity_translation_edit_form_info($form, $form_state) {
  1725. $info = FALSE;
  1726. $entity_type = isset($form['#entity_type']) && is_string($form['#entity_type']) ? $form['#entity_type'] : FALSE;
  1727. if ($entity_type) {
  1728. $entity_info = entity_get_info($form['#entity_type']);
  1729. if (!empty($entity_info['translation']['entity_translation']['edit form'])) {
  1730. $entity_keys = explode('][', $entity_info['translation']['entity_translation']['edit form']);
  1731. $key_exists = FALSE;
  1732. $entity = drupal_array_get_nested_value($form_state, $entity_keys, $key_exists);
  1733. if ($key_exists) {
  1734. $info = array(
  1735. 'entity type' => $form['#entity_type'],
  1736. 'entity' => (object) $entity,
  1737. );
  1738. }
  1739. }
  1740. }
  1741. return $info;
  1742. }
  1743. /**
  1744. * Checks whether an entity translation is accessible.
  1745. *
  1746. * @param $translation
  1747. * An array representing an entity translation.
  1748. *
  1749. * @return
  1750. * TRUE if the current user is allowed to view the translation.
  1751. */
  1752. function entity_translation_access($entity_type, $translation) {
  1753. return $translation['status'] || user_access('translate any entity') || user_access("translate $entity_type entities");
  1754. }
  1755. /**
  1756. * Returns the set of languages available for translations.
  1757. */
  1758. function entity_translation_languages($entity_type = NULL, $entity = NULL) {
  1759. if (isset($entity) && $entity_type == 'node' && module_exists('i18n_node')) {
  1760. // @todo Inherit i18n language settings.
  1761. }
  1762. elseif (variable_get('entity_translation_languages_enabled', FALSE)) {
  1763. $languages = language_list('enabled');
  1764. return $languages[1];
  1765. }
  1766. return language_list();
  1767. }
  1768. /**
  1769. * Implements hook_views_api().
  1770. */
  1771. function entity_translation_views_api() {
  1772. return array(
  1773. 'api' => 3,
  1774. 'path' => drupal_get_path('module', 'entity_translation') . '/views',
  1775. );
  1776. }
  1777. /**
  1778. * Implements hook_uuid_entities_features_export_entity_alter().
  1779. */
  1780. function entity_translation_uuid_entities_features_export_entity_alter($entity, $entity_type) {
  1781. // We do not need to export most of the keys:
  1782. // - The entity type is determined from the entity the translations are
  1783. // attached to.
  1784. // - The entity id will change from one site to another.
  1785. // - The user id needs to be removed because it will change as well.
  1786. // - Created and changed could be left but the UUID module removes created and
  1787. // changed values from the entities it exports, hence we do the same for
  1788. // consistency.
  1789. if (entity_translation_enabled($entity_type, $entity)) {
  1790. $fields = array('entity_type', 'entity_id', 'uid', 'created', 'changed');
  1791. $handler = entity_translation_get_handler($entity_type, $entity);
  1792. $translations = $handler->getTranslations();
  1793. if ($translations && isset($translations->data)) {
  1794. foreach ($translations->data as &$translation) {
  1795. foreach ($fields as $field) {
  1796. unset($translation[$field]);
  1797. }
  1798. }
  1799. }
  1800. }
  1801. }
  1802. /**
  1803. * Implements hook_entity_uuid_presave().
  1804. */
  1805. function entity_translation_entity_uuid_presave(&$entity, $entity_type) {
  1806. // UUID exports entities as arrays, therefore we need to cast the translations
  1807. // array back into an object.
  1808. $entity_info = entity_get_info($entity_type);
  1809. if (isset($entity_info['entity keys']) && isset($entity_info['entity keys']['translations'])) {
  1810. $key = $entity_info['entity keys']['translations'];
  1811. if (isset($entity->{$key})) {
  1812. $entity->{$key} = (object) $entity->{$key};
  1813. }
  1814. }
  1815. }
  1816. /**
  1817. * Implement hook_pathauto_alias_alter().
  1818. *
  1819. * When bulk-updating aliases for nodes automatically create a path for every
  1820. * translation.
  1821. */
  1822. function entity_translation_pathauto_alias_alter(&$alias, array &$context) {
  1823. $info = entity_get_info();
  1824. $entity_type = $context['module'];
  1825. // Ensure that we are dealing with a bundle having entity translation enabled.
  1826. if ($context['op'] == 'bulkupdate' && !empty($info[$entity_type]['token type']) && !empty($context['data'][$info[$entity_type]['token type']])) {
  1827. $entity = $context['data'][$info[$entity_type]['token type']];
  1828. if (entity_translation_enabled($entity_type, $entity)) {
  1829. $translations = entity_translation_get_handler($entity_type, $entity)->getTranslations();
  1830. // Only create extra aliases if we are working on the original language to
  1831. // avoid infinite recursion.
  1832. if ($context['language'] == $translations->original) {
  1833. foreach ($translations->data as $language => $translation) {
  1834. // We already have an alias for the original language, so let's not
  1835. // create another one.
  1836. if ($language == $translations->original) {
  1837. continue;
  1838. }
  1839. pathauto_create_alias($entity_type, $context['op'], $context['source'], $context['data'], $context['type'], $language);
  1840. }
  1841. }
  1842. }
  1843. }
  1844. }
  1845. /**
  1846. * Implements hook_entity_translation_delete().
  1847. */
  1848. function path_entity_translation_delete($entity_type, $entity, $langcode) {
  1849. // Remove any existing path alias for the removed translation.
  1850. $handler = entity_translation_get_handler($entity_type, $entity);
  1851. path_delete(array('source' => $handler->getViewPath(), 'language' => $langcode));
  1852. }
  1853. /**
  1854. * Wrapper for entity_save().
  1855. *
  1856. * @param $entity_type
  1857. * The entity type.
  1858. * @param $entity
  1859. * The entity object.
  1860. */
  1861. function entity_translation_entity_save($entity_type, $entity) {
  1862. // Entity module isn't required, but use it if it's available.
  1863. if (module_exists('entity')) {
  1864. entity_save($entity_type, $entity);
  1865. }
  1866. // Fall back to field_attach_* functions otherwise.
  1867. else {
  1868. field_attach_presave($entity_type, $entity);
  1869. field_attach_update($entity_type, $entity);
  1870. }
  1871. }