entity_translation.module 79 KB

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