translation.handler.inc 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. <?php
  2. /**
  3. * @file
  4. * Default translation handler for the translation module.
  5. */
  6. /**
  7. * Interface for enabling entity translation.
  8. *
  9. * Defines a set of methods to allow any entity to be processed by the entity
  10. * translation UI.
  11. */
  12. interface EntityTranslationHandlerInterface {
  13. /**
  14. * Injects the translation handler factory.
  15. */
  16. public function setFactory(EntityTranslationHandlerFactory $factory);
  17. /**
  18. * Registers a child translation handler for the given entity.
  19. */
  20. public function addChild($entity_type, $entity);
  21. /**
  22. * Removes a previously registered child translation handler.
  23. */
  24. public function removeChild($entity_type, $entity);
  25. /**
  26. * Loads the translation data into the wrapped entity.
  27. */
  28. public function loadTranslations();
  29. /**
  30. * Writes the translation status to the storage.
  31. */
  32. public function saveTranslations();
  33. /**
  34. * Returns the translation data for the current (wrapped) entity.
  35. */
  36. public function getTranslations();
  37. /**
  38. * Adds/updates an entity translation.
  39. *
  40. * @param $translation
  41. * A translation array as defined by the translation table's schema.
  42. * @param $values
  43. * (optional) the values that should be assigned to the field translations.
  44. */
  45. public function setTranslation($translation, $values = NULL);
  46. /**
  47. * Removes a translation from the translation set.
  48. *
  49. * @param $langcode
  50. * The language code of the translation to be removed.
  51. */
  52. public function removeTranslation($langcode);
  53. /**
  54. * Initializes the translation set by creating the original translation.
  55. */
  56. public function initTranslations();
  57. /**
  58. * Updates the translation set from the current entity status.
  59. */
  60. public function updateTranslations();
  61. /**
  62. * Removes all translations from the translation set.
  63. */
  64. public function removeTranslations();
  65. /**
  66. * Removes all translations from the current revision.
  67. */
  68. public function removeRevisionTranslations();
  69. /**
  70. * Initialize the language of the original field values.
  71. *
  72. * Ensure that the original translation language matches the language assigned
  73. * to the original field values.
  74. *
  75. * @return
  76. * TRUE if any initialization was necessary, FALSE otherwise.
  77. */
  78. public function initOriginalTranslation();
  79. /**
  80. * Returns the entity language.
  81. */
  82. public function getLanguage();
  83. /**
  84. * Returns the translation object key for the wrapped entity type.
  85. */
  86. public function getLanguageKey();
  87. /**
  88. * Returns the default language for the wrapped entity type and bundle.
  89. */
  90. public function getDefaultLanguage();
  91. /**
  92. * Sets the language of the orginal translation.
  93. *
  94. * @param $langcode
  95. * The language code of the original content values.
  96. */
  97. public function setOriginalLanguage($langcode);
  98. /**
  99. * Returns TRUE if the entity is currently being translated.
  100. *
  101. * @deprecated This is no longer used and will be removed before the first
  102. * stable release.
  103. */
  104. public function isTranslating();
  105. /**
  106. * Notifies the translation handler that its entity is being translated.
  107. *
  108. * @param $translating
  109. * A boolean value.
  110. *
  111. * @deprecated This is no longer used and will be removed before the first
  112. * stable release.
  113. */
  114. public function setTranslating($translating);
  115. /**
  116. * Return TRUE if a new revision of the entity has just been created.
  117. */
  118. public function isRevision();
  119. /**
  120. * Return TRUE if the entity type supports revisions.
  121. */
  122. public function isRevisionable();
  123. /**
  124. * Replaces the wrapped entity.
  125. *
  126. * @param $entity
  127. * The entity to be translated.
  128. */
  129. public function setEntity($entity);
  130. /**
  131. * Returns the wrapped entity.
  132. *
  133. * @param return
  134. * The wrapped entity.
  135. */
  136. public function getEntity();
  137. /**
  138. * Returns the wrapped entity type.
  139. *
  140. * @param return
  141. * The wrapped entity type.
  142. */
  143. public function getEntityType();
  144. /**
  145. * Checks that the wrapped entity matches the give entity
  146. *
  147. * @param $entity_type
  148. * The type of the entity to be matched.
  149. * @param $entity
  150. * The entity to be matched.
  151. */
  152. public function isWrappedEntity($entity_type, $entity);
  153. /**
  154. * Sets the translation update status.
  155. *
  156. * @param $outdated
  157. * A boolean value.
  158. */
  159. public function setOutdated($outdated);
  160. /**
  161. * Returns the base path for the current entity.
  162. *
  163. * This path will be prepended to the URL of any administration page.
  164. *
  165. * @return
  166. * A string to be used as a URL path prefix.
  167. */
  168. public function getBasePath();
  169. /**
  170. * Returns the path of the entity edit form.
  171. *
  172. * @param $langcode
  173. * (optional) The language the edit form should be presented in.
  174. */
  175. public function getEditPath($langcode = NULL);
  176. /**
  177. * Returns the path of the translation overview page.
  178. */
  179. public function getTranslatePath();
  180. /**
  181. * Returns the path of the entity view page.
  182. */
  183. public function getViewPath();
  184. /**
  185. * Returns the active path scheme.
  186. */
  187. public function getPathScheme();
  188. /**
  189. * Changes the active path scheme.
  190. *
  191. * @param $scheme
  192. * The new path scheme.
  193. */
  194. public function setPathScheme($scheme);
  195. /**
  196. * Initializes the most suited path scheme based on the given path.
  197. *
  198. * @param $path
  199. * (optional) The path to match the defined path schemes against. Defaults
  200. * to the current path.
  201. *
  202. * @return
  203. * The matched path scheme key.
  204. */
  205. public function initPathScheme($path = NULL);
  206. /**
  207. * A string allowing the user to identify the entity.
  208. */
  209. public function getLabel();
  210. /**
  211. * Checks if the user can perform the given operation on the wrapped entity.
  212. *
  213. * @param $op
  214. * The operation to be performed.
  215. *
  216. * @return
  217. * TRUE if the user is allowed to perform the given operation, FALSE
  218. * otherwise.
  219. */
  220. public function getAccess($op);
  221. /**
  222. * Checks if a user is allowed to edit the given translation.
  223. */
  224. public function getTranslationAccess($langcode);
  225. /**
  226. * Checks if a user is allowed to edit shared fields on the active form.
  227. */
  228. public function getSharedFieldsAccess();
  229. /**
  230. * Return TRUE if the entity supports URL aliasing.
  231. */
  232. public function isAliasEnabled();
  233. /**
  234. * Sets the active form language.
  235. */
  236. public function setFormLanguage($langcode);
  237. /**
  238. * Retrieves the active form language.
  239. */
  240. public function getFormLanguage();
  241. /**
  242. * Sets the source language for the translation being created.
  243. */
  244. public function setSourceLanguage($langcode);
  245. /**
  246. * Retrieves the source language for the translation being created.
  247. */
  248. public function getSourceLanguage();
  249. /**
  250. * Returns TRUE if a new entity is currently wrapped.
  251. */
  252. public function isNewEntity();
  253. /**
  254. * Returns TRUE whether we are displying an entity form.
  255. */
  256. public function isEntityForm();
  257. /**
  258. * Performs the needed alterations to the entity form.
  259. */
  260. public function entityForm(&$form, &$form_state);
  261. /**
  262. * Adds an language selection widget to the entity form.
  263. */
  264. public function entityFormLanguageWidget(&$form, &$form_state);
  265. /**
  266. * Performs submission tasks on the submitted entity language.
  267. */
  268. public function entityFormLanguageWidgetSubmit($form, &$form_state);
  269. /**
  270. * Handle shared form elements.
  271. */
  272. public function entityFormSharedElements(&$element);
  273. /**
  274. * Performs validation tasks on the submitted entity forms.
  275. */
  276. public function entityFormValidate($form, &$form_state);
  277. /**
  278. * Performs submission tasks on the submitted entity forms.
  279. */
  280. public function entityFormSubmit($form, &$form_state);
  281. /**
  282. * Alters the local tasks render array to populate the language tabs.
  283. */
  284. public function localTasksAlter(&$data, $router_item, $root_path);
  285. }
  286. /**
  287. * Class implementing the default entity translation behaviours.
  288. */
  289. class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterface {
  290. protected $entityType;
  291. protected $entity;
  292. protected $entityInfo;
  293. protected $entityId;
  294. protected $bundle;
  295. protected $revisionable;
  296. /**
  297. * The translation handler factory.
  298. *
  299. * @var EntityTranslationHandlerFactory
  300. */
  301. protected $factory;
  302. /**
  303. * The translation handler hierarchy storage.
  304. *
  305. * @var array
  306. */
  307. protected $children = array();
  308. private $entityForm;
  309. private $translating;
  310. private $outdated;
  311. private $formLanguage;
  312. private $sourceLanguage;
  313. private $pathScheme;
  314. private $pathWildcard;
  315. private $basePath;
  316. private $editPath;
  317. private $translatePath;
  318. private $viewPath;
  319. private $routerMap;
  320. /**
  321. * Initializes an instance of the translation handler.
  322. *
  323. * @param $entity_type
  324. * The type of the entity being wrapped.
  325. * @param $entity_info
  326. * The entity information for the entity being wrapped.
  327. * @param $entity
  328. * The entity being wrapped.
  329. */
  330. public function __construct($entity_type, $entity_info, $entity) {
  331. $this->entityType = $entity_type;
  332. $this->entityInfo = $entity_info;
  333. $this->setEntity($entity);
  334. $this->entityForm = FALSE;
  335. $this->translating = FALSE;
  336. $this->outdated = FALSE;
  337. $this->formLanguage = FALSE;
  338. $this->sourceLanguage = FALSE;
  339. $this->pathScheme = 'default';
  340. $this->routerMap = array();
  341. if (entity_translation_enabled($entity_type)) {
  342. $this->initPathVariables();
  343. }
  344. }
  345. /**
  346. * Read the translation data from the storage.
  347. */
  348. public static function loadMultiple($entity_type, $entities) {
  349. $entity_info = entity_get_info($entity_type);
  350. if (isset($entity_info['entity keys']['translations'])){
  351. $translations_key = $entity_info['entity keys']['translations'];
  352. }
  353. else {
  354. // If no translations key is defined we cannot proceed.
  355. return;
  356. }
  357. $revisionable = self::isEntityTypeRevisionable($entity_type);
  358. $revisions_ids = array();
  359. foreach ($entities as $id => $entity) {
  360. $entities[$id]->{$translations_key} = self::emptyTranslations();
  361. if ($revisionable) {
  362. list(, $revisions_id,) = entity_extract_ids($entity_type, $entity);
  363. $revisions_ids[$id] = $revisions_id;
  364. }
  365. }
  366. $table = $revisionable ? 'entity_translation_revision' : 'entity_translation';
  367. $query = db_select($table, 'et')
  368. ->fields('et')
  369. ->condition('entity_type', $entity_type);
  370. if (!$revisionable) {
  371. $query->condition('entity_id', array_keys($entities), 'IN');
  372. }
  373. else {
  374. $query->condition('revision_id', $revisions_ids, 'IN');
  375. }
  376. $results = $query->execute();
  377. foreach ($results as $row) {
  378. $id = $row->entity_id;
  379. $entities[$id]->{$translations_key}->data[$row->language] = (array) $row;
  380. // Only the original translation has an empty source.
  381. if (empty($row->source)) {
  382. $entities[$id]->{$translations_key}->original = $row->language;
  383. }
  384. }
  385. }
  386. /**
  387. * Returns the localized links for the given path.
  388. */
  389. public static function languageSwitchLinks($path) {
  390. $links = language_negotiation_get_switch_links(LANGUAGE_TYPE_CONTENT, $path);
  391. if (empty($links)) {
  392. // If content language is set up to fall back to the interface language,
  393. // then there will be no switch links for LANGUAGE_TYPE_CONTENT, ergo we
  394. // also need to use interface switch links.
  395. $links = language_negotiation_get_switch_links(LANGUAGE_TYPE_INTERFACE, $path);
  396. }
  397. return $links;
  398. }
  399. /**
  400. * @see EntityTranslationHandlerInterface::setFactory()
  401. */
  402. public function setFactory(EntityTranslationHandlerFactory $factory) {
  403. $this->factory = $factory;
  404. }
  405. /**
  406. * @see EntityTranslationHandlerInterface::addChild()
  407. */
  408. public function addChild($entity_type, $entity) {
  409. if (!empty($this->factory)) {
  410. $handler = $this->factory->getHandler($entity_type, $entity);
  411. $handler->setFormLanguage($this->getFormLanguage());
  412. $handler->setSourceLanguage($this->getSourceLanguage());
  413. // Avoid registering more than one child handler for each entity.
  414. $hid = $this->factory->getHandlerId($entity_type, $entity);
  415. $this->children[$hid] = $handler;
  416. }
  417. }
  418. /**
  419. * @see EntityTranslationHandlerInterface::removeChild()
  420. */
  421. public function removeChild($entity_type, $entity) {
  422. if (!empty($this->factory)) {
  423. $hid = $this->factory->getHandlerId($entity_type, $entity);
  424. unset($this->children[$hid]);
  425. }
  426. }
  427. /**
  428. * Proxies the specified method invocation to a child translation handler.
  429. */
  430. protected function notifyChildren($method, $args) {
  431. foreach ($this->children as $handler) {
  432. call_user_func_array(array($handler, $method), $args);
  433. }
  434. }
  435. /**
  436. * @see EntityTranslationHandlerInterface::loadTranslations()
  437. */
  438. public function loadTranslations() {
  439. if (isset($this->entityId)) {
  440. $this->loadMultiple($this->entityType, array($this->entityId => $this->entity));
  441. }
  442. elseif ($translations_key = $this->getTranslationsKey()) {
  443. $this->entity->{$translations_key} = $this->emptyTranslations();
  444. }
  445. }
  446. /**
  447. * @see EntityTranslationHandlerInterface::saveTranslations()
  448. */
  449. public function saveTranslations() {
  450. $translations = $this->getTranslations();
  451. // Save current values.
  452. $this->doSaveTranslations($translations, 'entity_translation');
  453. // Save revision values.
  454. if ($this->isRevisionable()) {
  455. $this->doSaveTranslations($translations, 'entity_translation_revision', TRUE);
  456. }
  457. // The translation handler interface decouples operations on translations at
  458. // data structure level from CRUD operations. Hence hooks must be fired
  459. // after changes are actually persisted.
  460. if (!empty($translations->hook)) {
  461. // Hook info is keyed by language code so that subsequent operations at
  462. // data structure level do not cause multiple hooks for the same data to
  463. // be fired. For instance if a translation is first updated and then
  464. // deleted, only the 'delete' hook should be fired, because it is the only
  465. // change that has actually been persisted.
  466. foreach ($translations->hook as $langcode => $info) {
  467. $translation = isset($translations->data[$langcode]) ? $translations->data[$langcode] : $langcode;
  468. $data = isset($info['data']) ? $info['data'] : NULL;
  469. module_invoke_all('entity_translation_' . $info['hook'], $this->entityType, $this->entity, $translation, $data);
  470. // Provide Rules events integration if available.
  471. if (module_exists('rules')) {
  472. // Pass the entity as a wrapped one since rules can't do it for us
  473. // when using the variable type 'entity'.
  474. rules_invoke_event('entity_translation_' . $info['hook'], $this->entityType, entity_metadata_wrapper($this->entityType, $this->entity), $translation, $data);
  475. }
  476. }
  477. // Avoid firing hooks more than once for the same changes.
  478. $translations->hook = array();
  479. }
  480. }
  481. /**
  482. * Saves entity translation records to the storage.
  483. */
  484. protected function doSaveTranslations($translations, $table, $revision = FALSE) {
  485. // Delete and insert, rather than update, in case a value was added.
  486. $query = db_delete($table)
  487. ->condition('entity_type', $this->entityType)
  488. ->condition('entity_id', $this->entityId);
  489. // If we are storing translations for the current revision or we are
  490. // deleting the entity we should remove all translation data.
  491. $langcode = $translations->original;
  492. $hook = isset($translations->hook) ? $translations->hook : array();
  493. if ($revision && $this->isRevisionable() && (empty($hook[$langcode]['hook']) || $hook[$langcode]['hook'] != 'delete')) {
  494. $query->condition('revision_id', $this->revisionId);
  495. }
  496. $query->execute();
  497. if (count($translations->data)) {
  498. $columns = array('entity_type', 'entity_id', 'revision_id', 'language', 'source', 'uid', 'status', 'translate', 'created', 'changed');
  499. $query = db_insert($table)->fields($columns);
  500. // These values should override the translation ones as they are not
  501. // supposed to change.
  502. $overrides = array(
  503. 'entity_type' => $this->entityType,
  504. 'entity_id' => $this->entityId,
  505. 'revision_id' => $this->isRevisionable() ? $this->revisionId : $this->entityId,
  506. );
  507. // These instead are just defaults.
  508. $defaults = array(
  509. 'source' => '',
  510. 'uid' => $GLOBALS['user']->uid,
  511. 'translate' => 0,
  512. 'status' => 0,
  513. 'created' => REQUEST_TIME,
  514. 'changed' => REQUEST_TIME,
  515. );
  516. foreach ($translations->data as $translation) {
  517. $translation = $overrides + $translation + $defaults;
  518. $query->values($translation);
  519. }
  520. $query->execute();
  521. }
  522. }
  523. /**
  524. * @see EntityTranslationHandlerInterface::getTranslations()
  525. */
  526. public function getTranslations() {
  527. if ($translations_key = $this->getTranslationsKey()) {
  528. // Lazy load translations if for some reason the wrapped entity did not go
  529. // through hook_entity_load().
  530. if (!isset($this->entity->{$translations_key})) {
  531. $this->loadTranslations();
  532. }
  533. return $this->entity->{$translations_key};
  534. }
  535. return self::emptyTranslations();
  536. }
  537. /**
  538. * @see EntityTranslationHandlerInterface::setTranslation()
  539. */
  540. public function setTranslation($translation, $values = NULL) {
  541. if (isset($translation['source']) && $translation['language'] == $translation['source']) {
  542. throw new Exception('Invalid translation language');
  543. }
  544. $translations = $this->getTranslations();
  545. $langcode = $translation['language'];
  546. $this->setTranslating(TRUE);
  547. if (isset($translations->data[$langcode])) {
  548. $translation = array_merge($translations->data[$langcode], $translation);
  549. $translation['changed'] = REQUEST_TIME;
  550. // If a newly inserted translation has not been stored yet do not fire an
  551. // update hook.
  552. $hook = empty($translations->hook[$langcode]['hook']) ? 'update' : $translations->hook[$langcode]['hook'];
  553. }
  554. else {
  555. $hook = 'insert';
  556. }
  557. // Store the translation data.
  558. $translations->data[$langcode] = $translation;
  559. // Keep track that the translation has been inserted or updated.
  560. $translations->hook[$langcode] = array('hook' => $hook, 'data' => $values);
  561. if (is_array($values)) {
  562. // Update field translations.
  563. foreach (field_info_instances($this->entityType, $this->bundle) as $instance) {
  564. $field_name = $instance['field_name'];
  565. $field = field_info_field($field_name);
  566. if ($field['translatable'] && isset($values[$field_name])) {
  567. $this->entity->{$field_name}[$langcode] = $values[$field_name][$langcode];
  568. }
  569. }
  570. }
  571. $args = func_get_args();
  572. $this->notifyChildren(__FUNCTION__, $args);
  573. }
  574. /**
  575. * @see EntityTranslationHandlerInterface::removeTranslation()
  576. */
  577. public function removeTranslation($langcode) {
  578. $translations_key = $this->getTranslationsKey();
  579. if (empty($translations_key)) {
  580. return;
  581. }
  582. $hook_info = array('hook' => 'delete');
  583. if (!empty($langcode)) {
  584. unset($this->entity->{$translations_key}->data[$langcode]);
  585. // Keep track that the current translation has been removed.
  586. $this->entity->{$translations_key}->hook[$langcode] = $hook_info;
  587. }
  588. elseif (!empty($this->entity->{$translations_key}->data)) {
  589. $keys = array_keys($this->entity->{$translations_key}->data);
  590. $values = array_fill(0, count($keys), $hook_info);
  591. // Keep track that the all translations have been removed.
  592. $this->entity->{$translations_key}->hook = array_combine($keys, $values);
  593. // Actually remove translations.
  594. $this->entity->{$translations_key}->data = array();
  595. }
  596. // Remove field translations.
  597. foreach (field_info_instances($this->entityType, $this->bundle) as $instance) {
  598. $field_name = $instance['field_name'];
  599. $field = field_info_field($field_name);
  600. if ($field['translatable']) {
  601. if (!empty($langcode)) {
  602. $this->entity->{$field_name}[$langcode] = array();
  603. }
  604. else {
  605. $this->entity->{$field_name} = array();
  606. }
  607. }
  608. }
  609. // Clear the cache for this entity.
  610. entity_get_controller($this->entityType)->resetCache(array($this->getEntityId()));
  611. }
  612. /**
  613. * @see EntityTranslationHandlerInterface::initTranslations()
  614. */
  615. public function initTranslations() {
  616. $langcode = $this->getLanguage();
  617. if (!empty($langcode)) {
  618. $translation = array('language' => $langcode, 'status' => 1);
  619. $this->setTranslation($translation);
  620. $this->setOriginalLanguage($langcode);
  621. }
  622. }
  623. /**
  624. * @see EntityTranslationHandlerInterface::updateTranslations()
  625. */
  626. public function updateTranslations() {
  627. $langcode = $this->getLanguage();
  628. // Only create a translation on edit if the translation set is empty:
  629. // the entity might have been created with language set to "language
  630. // neutral".
  631. if (empty($this->getTranslations()->data)) {
  632. $this->initTranslations();
  633. }
  634. elseif (!empty($langcode)) {
  635. $this->setOriginalLanguage($langcode);
  636. }
  637. }
  638. /**
  639. * @see EntityTranslationHandlerInterface::removeTranslations()
  640. */
  641. public function removeTranslations() {
  642. $this->removeTranslation(NULL);
  643. }
  644. /**
  645. * @see EntityTranslationHandlerInterface::removeRevisionTranslations()
  646. */
  647. public function removeRevisionTranslations() {
  648. $translations_key = $this->getTranslationsKey();
  649. $keys = array_keys($this->entity->{$translations_key}->data);
  650. $values = array_fill(0, count($keys), array('hook' => 'delete_revision'));
  651. $this->removeTranslation(NULL);
  652. $this->entity->{$translations_key}->hook = array_combine($keys, $values);
  653. }
  654. /**
  655. * @see EntityTranslationHandlerInterface::initOriginalTranslation()
  656. */
  657. public function initOriginalTranslation() {
  658. $fixed = FALSE;
  659. $translations = $this->getTranslations();
  660. foreach (field_info_instances($this->entityType, $this->bundle) as $instance) {
  661. $field_name = $instance['field_name'];
  662. $field = field_info_field($field_name);
  663. $langcode = count($this->entity->{$field_name}) == 1 ? key($this->entity->{$field_name}) : $translations->original;
  664. if ($langcode == LANGUAGE_NONE && $field['translatable']) {
  665. $this->entity->{$field_name}[$translations->original] = $this->entity->{$field_name}[$langcode];
  666. $this->entity->{$field_name}[$langcode] = array();
  667. $fixed = TRUE;
  668. }
  669. }
  670. return $fixed;
  671. }
  672. /**
  673. * @see EntityTranslationHandlerInterface::getLanguage()
  674. */
  675. public function getLanguage() {
  676. if (!empty($this->entityInfo['entity keys']['language'])) {
  677. $language_key = $this->entityInfo['entity keys']['language'];
  678. if (!empty($this->entity->{$language_key})) {
  679. return $this->entity->{$language_key};
  680. }
  681. }
  682. $translations = $this->getTranslations();
  683. if (!empty($translations->original)) {
  684. return $translations->original;
  685. }
  686. else {
  687. // When we are creating an entity and no language is specified fall back
  688. // to the default language for the current entity and bundle.
  689. return $this->getDefaultLanguage();
  690. }
  691. }
  692. /**
  693. * @see EntityTranslationHandlerInterface::getLanguageKey()
  694. */
  695. public function getLanguageKey() {
  696. return !empty($this->entityInfo['entity keys']['language']) ? $this->entityInfo['entity keys']['language'] : 'language';
  697. }
  698. /**
  699. * @see EntityTranslationHandlerInterface::getDefaultLanguage()
  700. */
  701. public function getDefaultLanguage() {
  702. $settings = variable_get('entity_translation_settings_' . $this->entityType . '__' . $this->bundle, array());
  703. if (!empty($settings['default_language'])) {
  704. switch ($settings['default_language']) {
  705. case ENTITY_TRANSLATION_LANGUAGE_DEFAULT:
  706. $langcode = language_default()->language;
  707. break;
  708. case ENTITY_TRANSLATION_LANGUAGE_CURRENT:
  709. $langcode = $GLOBALS[LANGUAGE_TYPE_CONTENT]->language;
  710. break;
  711. case ENTITY_TRANSLATION_LANGUAGE_AUTHOR:
  712. $langcode = $GLOBALS['user']->language;
  713. break;
  714. default:
  715. // An actual language code has been explicitly configured.
  716. $langcode = $settings['default_language'];
  717. }
  718. }
  719. else {
  720. // Fall back to the default language to keep backward compatibility.
  721. $langcode = language_default()->language;
  722. }
  723. return $langcode;
  724. }
  725. /**
  726. * @see EntityTranslationHandlerInterface::setOriginalLanguage()
  727. */
  728. public function setOriginalLanguage($langcode) {
  729. $translations = $this->getTranslations();
  730. if (!isset($translations->original) || $translations->original != $langcode) {
  731. if (isset($translations->original)) {
  732. $translations->data[$langcode] = $translations->data[$translations->original];
  733. $translations->data[$langcode]['language'] = $langcode;
  734. unset($translations->data[$translations->original]);
  735. }
  736. $translations->original = $langcode;
  737. $args = func_get_args();
  738. $this->notifyChildren(__FUNCTION__, $args);
  739. }
  740. }
  741. /**
  742. * @see EntityTranslationHandlerInterface::isTranslating()
  743. */
  744. public function isTranslating() {
  745. return $this->translating;
  746. }
  747. /**
  748. * @see EntityTranslationHandlerInterface::setTranslating()
  749. */
  750. public function setTranslating($translating) {
  751. $this->translating = $translating;
  752. }
  753. /**
  754. * @see EntityTranslationHandlerInterface::isRevision()
  755. */
  756. public function isRevision() {
  757. return FALSE;
  758. }
  759. /**
  760. * @see EntityTranslationHandlerInterface::isRevisionable()
  761. */
  762. public function isRevisionable() {
  763. $result = FALSE;
  764. if (!isset($this->revisionable)) {
  765. $result = self::isEntityTypeRevisionable($this->entityType);
  766. }
  767. return $result;
  768. }
  769. /**
  770. * Returns whether the entity type is revisionable.
  771. */
  772. public static function isEntityTypeRevisionable($entity_type) {
  773. $entity_info = entity_get_info($entity_type);
  774. return variable_get('entity_translation_revision_enabled', FALSE) && !empty($entity_info['entity keys']['revision']);
  775. }
  776. /**
  777. * @see EntityTranslationHandlerInterface::setEntity()
  778. */
  779. public function setEntity($entity) {
  780. $this->entity = $entity;
  781. // Ensure translations data is populated.
  782. $translations_key = $this->getTranslationsKey();
  783. if ($translations_key && !isset($this->entity->{$translations_key})) {
  784. $this->entity->{$translations_key} = self::emptyTranslations();
  785. }
  786. // Update entity properties.
  787. list($this->entityId, $this->revisionId, $this->bundle) = entity_extract_ids($this->entityType, $this->entity);
  788. // Initialize the handler id if needed.
  789. if (!empty($this->factory)) {
  790. $this->factory->getHandlerId($this->entityType, $entity);
  791. }
  792. }
  793. /**
  794. * @see EntityTranslationHandlerInterface::getEntity()
  795. */
  796. public function getEntity() {
  797. return $this->entity;
  798. }
  799. /**
  800. * @see EntityTranslationHandlerInterface::getEntityType()
  801. */
  802. public function getEntityType() {
  803. return $this->entityType;
  804. }
  805. /**
  806. * @see EntityTranslationHandlerInterface::isWrappedEntity()
  807. */
  808. public function isWrappedEntity($entity_type, $entity) {
  809. list($id,,) = entity_extract_ids($entity_type, $entity);
  810. return $entity_type == $this->entityType && $id == $this->entityId;
  811. }
  812. /**
  813. * @see EntityTranslationHandlerInterface::setOutdated()
  814. */
  815. public function setOutdated($outdated) {
  816. if ($outdated) {
  817. $translations = $this->getTranslations();
  818. foreach ($translations->data as $langcode => &$translation) {
  819. if ($langcode != $this->getFormLanguage()) {
  820. $translation['translate'] = 1;
  821. }
  822. }
  823. $args = func_get_args();
  824. $this->notifyChildren(__FUNCTION__, $args);
  825. }
  826. }
  827. /**
  828. * @see EntityTranslationHandlerInterface::getBasePath()
  829. */
  830. public function getBasePath() {
  831. return $this->basePath;
  832. }
  833. /**
  834. * @see EntityTranslationHandlerInterface::getEditPath()
  835. */
  836. public function getEditPath($langcode = NULL) {
  837. return empty($this->editPath) ? FALSE : (empty($langcode) ? $this->editPath : $this->editPath . '/' . $langcode);
  838. }
  839. /**
  840. * @see EntityTranslationHandlerInterface::getTranslatePath()
  841. */
  842. public function getTranslatePath() {
  843. return $this->translatePath;
  844. }
  845. /**
  846. * @see EntityTranslationHandlerInterface::getViewPath()
  847. */
  848. public function getViewPath() {
  849. return $this->viewPath;
  850. }
  851. /**
  852. * @see EntityTranslationHandlerInterface::getPathScheme()
  853. */
  854. public function getPathScheme() {
  855. return $this->pathScheme;
  856. }
  857. /**
  858. * @see EntityTranslationHandlerInterface::setPathScheme()
  859. */
  860. public function setPathScheme($scheme) {
  861. if ($scheme != $this->pathScheme) {
  862. $this->pathScheme = $scheme;
  863. $this->initPathVariables();
  864. }
  865. }
  866. /**
  867. * @see EntityTranslationHandlerInterface::initPathScheme()
  868. */
  869. public function initPathScheme($path = NULL) {
  870. $scheme = 'default';
  871. // If only one path scheme is defined no need to find one.
  872. if (count($this->entityInfo['translation']['entity_translation']['path schemes']) > 1) {
  873. $item = menu_get_item($path);
  874. if (!empty($item['path'])) {
  875. $current_path_scheme = $this->findMatchingPathScheme($item['path']);
  876. if ($current_path_scheme) {
  877. $scheme = $current_path_scheme;
  878. $this->routerMap = $item['original_map'];
  879. }
  880. }
  881. }
  882. $this->setPathScheme($scheme);
  883. return $scheme;
  884. }
  885. /**
  886. * Find a path scheme matching the given path.
  887. *
  888. * @param $router_path
  889. * The path to match against.
  890. *
  891. * @return
  892. * The key of the path scheme if found, FALSE otherwise.
  893. */
  894. protected function findMatchingPathScheme($router_path) {
  895. $path_keys = array_flip(array('base path', 'view path', 'edit path', 'translate path'));
  896. foreach ($this->entityInfo['translation']['entity_translation']['path schemes'] as $delta => $scheme) {
  897. // Construct regular expression pattern for determining whether any path
  898. // in the current scheme matches the current request path.
  899. $path_elements = array_intersect_key($scheme, $path_keys);
  900. // Add additional path elements which were added during
  901. // entity_translation_menu_alter().
  902. if (isset($path_elements['edit path'])) {
  903. $path_elements[] = $path_elements['edit path'] . '/%entity_translation_language';
  904. $path_elements[] = $path_elements['edit path'] . '/add/%entity_translation_language/%entity_translation_language';
  905. }
  906. if (isset($path_elements['translate path'])) {
  907. $path_elements[] = $path_elements['translate path'] . '/delete/%entity_translation_language';
  908. }
  909. // Replace wildcards with % for matching parameters.
  910. $path_elements = array_flip(preg_replace('|%[^/]+|', '%', $path_elements));
  911. if (isset($path_elements[$router_path])) {
  912. return $delta;
  913. }
  914. }
  915. return FALSE;
  916. }
  917. /**
  918. * @see EntityTranslationHandlerInterface::getLabel()
  919. */
  920. public function getLabel() {
  921. if (($label = entity_label($this->entityType, $this->entity)) !== FALSE) {
  922. return $label;
  923. }
  924. else {
  925. return "{$this->entityType}:{$this->getEntityId()}" ;
  926. }
  927. }
  928. /**
  929. * @see EntityTranslationHandlerInterface::getAccess()
  930. */
  931. public function getAccess($op) {
  932. return TRUE;
  933. }
  934. /**
  935. * @see EntityTranslationHandlerInterface::getTranslationAccess()
  936. */
  937. public function getTranslationAccess($langcode) {
  938. return !empty($this->entityInfo['translation']['entity_translation']['skip original values access']) || !entity_translation_workflow_enabled() || $langcode != $this->getLanguage() || user_access('edit original values') || user_access("edit {$this->entityType} original values");
  939. }
  940. /**
  941. * @see EntityTranslationHandlerInterface::getSharedFieldsAccess()
  942. */
  943. public function getSharedFieldsAccess() {
  944. $settings = entity_translation_settings($this->entityType, $this->bundle);
  945. return ($settings['shared_fields_original_only'] == FALSE || $this->getLanguage() == $this->getFormLanguage()) &&
  946. (!entity_translation_workflow_enabled() || user_access('edit translation shared fields') || user_access("edit {$this->entityType} translation shared fields"));
  947. }
  948. /**
  949. * @see EntityTranslationHandlerInterface::isAliasEnabled()
  950. */
  951. public function isAliasEnabled() {
  952. return !empty($this->entityInfo['translation']['entity_translation']['alias']);
  953. }
  954. /**
  955. * @see EntityTranslationHandlerInterface::setFormLanguage()
  956. */
  957. public function setFormLanguage($langcode) {
  958. $this->formLanguage = $langcode;
  959. $args = func_get_args();
  960. $this->notifyChildren(__FUNCTION__, $args);
  961. }
  962. /**
  963. * @see EntityTranslationHandlerInterface::getFormLanguage()
  964. */
  965. public function getFormLanguage() {
  966. return !empty($this->formLanguage) ? $this->formLanguage : $this->getLanguage();
  967. }
  968. /**
  969. * @see EntityTranslationHandlerInterface::setSourceLanguage()
  970. */
  971. public function setSourceLanguage($langcode) {
  972. $this->sourceLanguage = $langcode;
  973. $args = func_get_args();
  974. $this->notifyChildren(__FUNCTION__, $args);
  975. }
  976. /**
  977. * @see EntityTranslationHandlerInterface::getSourceLanguage()
  978. */
  979. public function getSourceLanguage() {
  980. return $this->sourceLanguage;
  981. }
  982. /**
  983. * @see EntityTranslationHandlerInterface::isNewEntity()
  984. */
  985. public function isNewEntity() {
  986. $id = $this->getEntityId();
  987. return empty($id);
  988. }
  989. /**
  990. * @see EntityTranslationHandlerInterface::isEntityForm()
  991. */
  992. public function isEntityForm() {
  993. return $this->entityForm;
  994. }
  995. /**
  996. * @see EntityTranslationHandlerInterface::entityForm()
  997. */
  998. public function entityForm(&$form, &$form_state) {
  999. $this->entityForm = TRUE;
  1000. $translations = $this->getTranslations();
  1001. $form_langcode = $this->getFormLanguage();
  1002. $langcode = $this->getLanguage();
  1003. $is_translation = $this->isTranslationForm();
  1004. $new_translation = !isset($translations->data[$form_langcode]);
  1005. $no_translations = count($translations->data) < 2;
  1006. $languages = language_list();
  1007. $access = user_access('translate any entity') || user_access("translate $this->entityType entities");
  1008. // Store contextual information in the form state.
  1009. $form_state['entity_translation']['form_langcode'] = $form_langcode;
  1010. $form_state['entity_translation']['source_langcode'] = $this->getSourceLanguage();
  1011. // The only way to determine whether we are editing the original values is
  1012. // comparing form language and entity language. Since a language change
  1013. // might render impossible to make this check after form submission, we
  1014. // store the related information here.
  1015. $form_state['entity_translation']['is_translation'] = $is_translation;
  1016. // Adjust page title to specify the current language being edited, if we
  1017. // have at least one translation.
  1018. if ($form_langcode != LANGUAGE_NONE && (!$no_translations || $new_translation)) {
  1019. drupal_set_title($this->entityFormTitle() . ' [' . t($languages[$form_langcode]->name) . ']', PASS_THROUGH);
  1020. }
  1021. // Display source language selector only if we are creating a new
  1022. // translation and there are at least two translations available.
  1023. if (!$no_translations && $new_translation) {
  1024. $form['source_language'] = array(
  1025. '#type' => 'fieldset',
  1026. '#title' => t('Source language'),
  1027. '#collapsible' => TRUE,
  1028. '#collapsed' => TRUE,
  1029. '#tree' => TRUE,
  1030. '#weight' => -100,
  1031. '#access' => $access,
  1032. '#multilingual' => TRUE,
  1033. 'language' => array(
  1034. '#type' => 'select',
  1035. '#default_value' => $this->getSourceLanguage(),
  1036. '#options' => array(),
  1037. ),
  1038. 'submit' => array(
  1039. '#type' => 'submit',
  1040. '#value' => t('Change'),
  1041. '#submit' => array('entity_translation_entity_form_source_language_submit'),
  1042. ),
  1043. );
  1044. foreach (language_list() as $language) {
  1045. if (isset($translations->data[$language->language])) {
  1046. $form['source_language']['language']['#options'][$language->language] = t($language->name);
  1047. }
  1048. }
  1049. }
  1050. // Add the entity language switcher.
  1051. $this->entityFormLanguageWidget($form, $form_state);
  1052. if ($is_translation && isset($form['actions']['delete'])) {
  1053. // Replace the delete button with the delete translation one.
  1054. if (!$new_translation) {
  1055. $weight = 100;
  1056. foreach (array('delete', 'submit') as $key) {
  1057. if (isset($form['actions'][$key]['weight'])) {
  1058. $weight = $form['actions'][$key]['weight'];
  1059. break;
  1060. }
  1061. }
  1062. $form['actions']['delete_translation'] = array(
  1063. '#type' => 'submit',
  1064. '#value' => t('Delete translation'),
  1065. '#weight' => $weight,
  1066. '#submit' => array('entity_translation_entity_form_delete_translation_submit'),
  1067. );
  1068. }
  1069. // Always remove the delete button on translation forms.
  1070. unset($form['actions']['delete']);
  1071. }
  1072. // We need to display the translation tab only when there is at least one
  1073. // translation available or a new one is about to be created.
  1074. if ($new_translation || count($translations->data) > 1) {
  1075. $form['translation'] = array(
  1076. '#type' => 'fieldset',
  1077. '#title' => t('Translation'),
  1078. '#collapsible' => TRUE,
  1079. '#collapsed' => TRUE,
  1080. '#tree' => TRUE,
  1081. '#weight' => 10,
  1082. '#access' => $access,
  1083. '#multilingual' => TRUE,
  1084. );
  1085. // A new translation is enabled by default.
  1086. $status = $new_translation || $translations->data[$form_langcode]['status'];
  1087. // If there is only one published translation we cannot unpublish it,
  1088. // since there would be no content left to display. The whole entity
  1089. // should be unpublished instead, where possible.
  1090. $enabled = !$status;
  1091. if (!empty($status)) {
  1092. // A new translation is not available in the translation data hence it
  1093. // should count as one more.
  1094. $published = $new_translation;
  1095. foreach ($translations->data as $langcode => $translation) {
  1096. $published += $translation['status'];
  1097. }
  1098. $enabled = $published > 1;
  1099. }
  1100. $description = $enabled ?
  1101. t('An unpublished translation will not be visible for non-administrators.') :
  1102. t('Only this translation is published. You must publish at least one more translation to unpublish this one.');
  1103. $form['translation']['status'] = array(
  1104. '#type' => 'checkbox',
  1105. '#title' => t('This translation is published'),
  1106. '#default_value' => $status,
  1107. '#description' => $description,
  1108. '#disabled' => !$enabled,
  1109. );
  1110. $translate = !$new_translation && $translations->data[$form_langcode]['translate'];
  1111. if (!$translate) {
  1112. $form['translation']['retranslate'] = array(
  1113. '#type' => 'checkbox',
  1114. '#title' => t('Flag translations as outdated'),
  1115. '#default_value' => 0,
  1116. '#description' => t('If you made a significant change, which means translations should be updated, you can flag all translations of this post as outdated. This will not change any other property of those posts, like whether they are published or not.'),
  1117. );
  1118. }
  1119. else {
  1120. $form['translation']['translate'] = array(
  1121. '#type' => 'checkbox',
  1122. '#title' => t('This translation needs to be updated'),
  1123. '#default_value' => $translate,
  1124. '#description' => t('When this option is checked, this translation needs to be updated because the source post has changed. Uncheck when the translation is up to date again.'),
  1125. '#disabled' => !$translate,
  1126. );
  1127. }
  1128. $translation_author = $new_translation ? $GLOBALS['user'] : user_load($translations->data[$form_langcode]['uid']);
  1129. $name = isset($translation_author->name) ? $translation_author->name : '';
  1130. $form['translation']['name'] = array(
  1131. '#type' => 'textfield',
  1132. '#title' => t('Authored by'),
  1133. '#maxlength' => 60,
  1134. '#autocomplete_path' => 'user/autocomplete',
  1135. '#default_value' => $name,
  1136. '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  1137. );
  1138. $date = $new_translation ? REQUEST_TIME : $translations->data[$form_langcode]['created'];
  1139. $form['translation']['created'] = array(
  1140. '#type' => 'textfield',
  1141. '#title' => t('Authored on'),
  1142. '#maxlength' => 25,
  1143. '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array('%time' => format_date($date, 'custom', 'Y-m-d H:i:s O'), '%timezone' => format_date($date, 'custom', 'O'))),
  1144. '#default_value' => $new_translation ? '' : format_date($date, 'custom', 'Y-m-d H:i:s O'),
  1145. );
  1146. }
  1147. // If Menu translation is available translate the menu strings.
  1148. if (module_exists('entity_translation_i18n_menu')) {
  1149. $this->menuForm($form, $form_state);
  1150. }
  1151. // Process entity form submission.
  1152. $form['#submit'][] = 'entity_translation_entity_form_submit';
  1153. // This allows to intercept deletions. The check is needed because
  1154. // action-specific submit handlers prevent global ones from being called.
  1155. if (!empty($form['actions']['delete']['#submit'])) {
  1156. $form['actions']['delete']['#submit'][] = 'entity_translation_entity_form_submit';
  1157. }
  1158. }
  1159. /**
  1160. * @see EntityTranslationHandlerInterface::entityFormSharedElements()
  1161. *
  1162. * Either remove access or add a translatability clue depending on the current
  1163. * user's "edit translation shared fields" permissions.
  1164. */
  1165. public function entityFormSharedElements(&$element, $access = NULL) {
  1166. static $ignored_types, $shared_labels;
  1167. if (!isset($ignored_types)) {
  1168. $ignored_types = array_flip(array('actions', 'value', 'hidden', 'vertical_tabs', 'token'));
  1169. }
  1170. if (!isset($shared_labels)) {
  1171. $shared_labels = variable_get('entity_translation_shared_labels', TRUE);
  1172. }
  1173. if (!isset($access)) {
  1174. $access = $this->getSharedFieldsAccess();
  1175. }
  1176. foreach (element_children($element) as $key) {
  1177. if (!isset($element[$key]['#type'])) {
  1178. $this->entityFormSharedElements($element[$key], $access);
  1179. }
  1180. else {
  1181. // Ignore non-widget form elements.
  1182. if (isset($ignored_types[$element[$key]['#type']])) {
  1183. continue;
  1184. }
  1185. // Elements are considered to be non multilingual by default.
  1186. // Update #access only if it has not been set already or if we have
  1187. // explicit mlutilingual support.
  1188. if (!isset($element[$key]['#access']) || isset($element[$key]['#multilingual'])) {
  1189. $element[$key]['#access'] = (!isset($element[$key]['#access']) || $element[$key]['#access']) && ($access || !empty($element[$key]['#multilingual']));
  1190. }
  1191. // Add translatability clue for visible elements.
  1192. if ($access && $shared_labels) {
  1193. _entity_translation_element_add_callback($element[$key], '#process', 'entity_translation_element_translatability_clue');
  1194. }
  1195. }
  1196. }
  1197. }
  1198. /**
  1199. * @see EntityTranslationHandlerInterface::entityFormLanguageWidget()
  1200. */
  1201. public function entityFormLanguageWidget(&$form, &$form_state) {
  1202. if (entity_translation_enabled($this->entityType, $this->bundle)) {
  1203. $is_new = $this->isNewEntity();
  1204. $is_translation = !empty($form_state['entity_translation']['is_translation']);
  1205. $translations = $this->getTranslations();
  1206. $settings = entity_translation_settings($this->entityType, $this->bundle);
  1207. $languages = entity_translation_languages($this->entityType, $this->entity);
  1208. $options = count($translations->data) > 1 || !empty($settings['exclude_language_none']) ? array() : array(LANGUAGE_NONE => t('Language neutral'));
  1209. foreach ($languages as $langcode => $language) {
  1210. // Disable languages for existing translations, so it is not possible to
  1211. // switch this entity to some language which is already in the
  1212. // translation set.
  1213. if (!isset($translations->data[$langcode]) || empty($translations->data[$langcode]['source'])) {
  1214. $options[$langcode] = t($language->name);
  1215. }
  1216. }
  1217. $langcode = $is_new ? $this->getDefaultLanguage() : $this->getLanguage();
  1218. $language_key = $this->getLanguageKey();
  1219. $form[$language_key] = array(
  1220. '#type' => 'select',
  1221. '#title' => t('Language'),
  1222. '#default_value' => $langcode,
  1223. '#options' => $options,
  1224. '#access' => empty($settings['hide_language_selector']),
  1225. '#disabled' => $is_translation || (!$is_new && !empty($settings['lock_language'])),
  1226. '#multilingual' => TRUE,
  1227. );
  1228. if ($is_translation) {
  1229. // @todo Consider supporting the ability to change translation language.
  1230. $form[$language_key]['#title'] = t('Original language');
  1231. }
  1232. }
  1233. // Prepend an empty form element to the form array so that we can update the
  1234. // form language before any other form handler has been called.
  1235. $form = array(
  1236. 'entity_translation_entity_form_language_update' => array(
  1237. '#element_validate' => array('entity_translation_entity_form_language_update'),
  1238. '#entity_type' => $this->entityType,
  1239. ),
  1240. ) + $form;
  1241. }
  1242. /**
  1243. * Performs alterations to the menu widget if available.
  1244. */
  1245. protected function menuForm(&$form, &$form_state) {
  1246. // By default do nothing: only nodes are currently supported.
  1247. }
  1248. /**
  1249. * @see EntityTranslationHandlerInterface::entityFormValidate()
  1250. */
  1251. public function entityFormValidate($form, &$form_state) {
  1252. if (!empty($form_state['values']['translation'])) {
  1253. $values = $form_state['values']['translation'];
  1254. // Validate the "authored by" field.
  1255. if (!empty($values['name']) && !($account = user_load_by_name($values['name']))) {
  1256. form_set_error('translation][name', t('The translation authoring username %name does not exist.', array('%name' => $values['name'])));
  1257. }
  1258. // Validate the "authored on" field.
  1259. if (!empty($values['created']) && strtotime($values['created']) === FALSE) {
  1260. form_set_error('translation][created', t('You have to specify a valid translation authoring date.'));
  1261. }
  1262. }
  1263. }
  1264. /**
  1265. * Update the current form language based on the submitted value.
  1266. */
  1267. protected function updateFormLanguage($form_state) {
  1268. // Update the form language as it might have changed. We exploit the
  1269. // validation phase to be sure to act as early as possible.
  1270. $language_key = $this->getLanguageKey();
  1271. if (isset($form_state['values'][$language_key]) && !$this->isTranslationForm()) {
  1272. $langcode = $form_state['values'][$language_key];
  1273. $this->setFormLanguage($langcode);
  1274. }
  1275. }
  1276. /**
  1277. * @see EntityTranslationHandlerInterface::entityFormLanguageWidgetSubmit()
  1278. */
  1279. public function entityFormLanguageWidgetSubmit($form, &$form_state) {
  1280. if (!entity_translation_enabled($this->entityType, $this->bundle)) {
  1281. return;
  1282. }
  1283. $this->updateFormLanguage($form_state);
  1284. $form_langcode = $this->getFormLanguage();
  1285. foreach (field_info_instances($this->entityType, $this->bundle) as $instance) {
  1286. $field_name = $instance['field_name'];
  1287. if (isset($form[$field_name]['#language'])) {
  1288. $field = field_info_field($field_name);
  1289. $previous_langcode = $form[$field_name]['#language'];
  1290. // Handle a possible language change: new language values are inserted,
  1291. // previous ones are deleted.
  1292. if ($field['translatable'] && $previous_langcode != $form_langcode && isset($form_state['values'][$field_name][$previous_langcode])) {
  1293. $form_state['values'][$field_name][$form_langcode] = $form_state['values'][$field_name][$previous_langcode];
  1294. $form_state['values'][$field_name][$previous_langcode] = array();
  1295. }
  1296. }
  1297. }
  1298. }
  1299. /**
  1300. * @see EntityTranslationHandlerInterface::entityFormSubmit()
  1301. */
  1302. public function entityFormSubmit($form, &$form_state) {
  1303. $form_langcode = $this->getFormLanguage();
  1304. $translations = $this->getTranslations();
  1305. $is_translation = !empty($form_state['entity_translation']['is_translation']);
  1306. $new_translation = !isset($translations->data[$form_langcode]);
  1307. $values = isset($form_state['values']['translation']) ? $form_state['values']['translation'] : array();
  1308. // Ensure every key has at least a default value. Subclasses may provide use
  1309. // entity-specific values to alter them.
  1310. $values += array(
  1311. 'status' => TRUE,
  1312. 'retranslate' => 0,
  1313. 'name' => isset($GLOBALS['user']->name) ? $GLOBALS['user']->name : '',
  1314. );
  1315. if (!isset($translations->data[$form_langcode])) {
  1316. // If we have a new translation the language is the original entity
  1317. // language.
  1318. $translation = $is_translation ? array('language' => $form_langcode, 'source' => $this->getSourceLanguage()) : array('language' => $form_langcode, 'source' => '');
  1319. }
  1320. else {
  1321. $translation = $translations->data[$form_langcode];
  1322. }
  1323. if (isset($values['translate'])) {
  1324. $translation['translate'] = intval($values['translate']);
  1325. }
  1326. else {
  1327. $this->setOutdated($values['retranslate']);
  1328. }
  1329. // Handle possible language changes for the original values.
  1330. if (!$is_translation) {
  1331. $this->setOriginalLanguage($form_langcode);
  1332. }
  1333. $translation['status'] = intval($values['status']);
  1334. $translation['uid'] = $values['name'] ? user_load_by_name($values['name'])->uid : 0;
  1335. $translation['created'] = empty($values['created']) ? REQUEST_TIME : strtotime($values['created']);
  1336. $this->setTranslation($translation);
  1337. // If no redirect has been explicitly set, go to the edit form for the
  1338. // current form language.
  1339. if ($new_translation && empty($form_state['redirect']) && !$this->isNewEntity()) {
  1340. $form_state['redirect'] = $this->getEditPath($form_langcode);
  1341. }
  1342. }
  1343. /**
  1344. * @see EntityTranslationHandlerInterface::localTasksAlter()
  1345. */
  1346. public function localTasksAlter(&$data, $router_item, $root_path) {
  1347. $translations = $this->getTranslations();
  1348. if (count($translations->data) > 0) {
  1349. $languages = language_list();
  1350. $form_langcode = $this->getFormLanguage();
  1351. $language_tabs = array();
  1352. if ($this->getSourceLanguage()) {
  1353. foreach ($data['tabs'][1]['output'] as $index => &$add_tab) {
  1354. if ($add_tab['#link']['path'] == $root_path) {
  1355. $add_tab['#link']['title'] = $languages[$form_langcode]->name;
  1356. $add_tab['#link']['weight'] = $languages[$form_langcode]->weight;
  1357. $add_tab['#active'] = TRUE;
  1358. $add_tab['#language_tab'] = TRUE;
  1359. $language_tabs[] = $add_tab;
  1360. unset($data['tabs'][1]['output'][$index]);
  1361. break;
  1362. }
  1363. }
  1364. }
  1365. foreach ($translations->data as $langcode => $translation) {
  1366. if ($this->getTranslationAccess($langcode)) {
  1367. $links = $this->languageSwitchLinks($this->getEditPath($langcode));
  1368. $link = !empty($links->links[$langcode]) ? $links->links[$langcode] : array();
  1369. if (isset($link['href'])) {
  1370. $tab = array();
  1371. $tab['#theme'] = 'menu_local_task';
  1372. $tab['#active'] = $langcode == $form_langcode;
  1373. $tab['#language_tab'] = TRUE;
  1374. $tab['#link'] = array(
  1375. 'href' => $link['href'],
  1376. 'title' => t($languages[$langcode]->name),
  1377. 'weight' => $languages[$langcode]->weight,
  1378. 'localized_options' => $link,
  1379. ) + $router_item;
  1380. $language_tabs[] = $tab;
  1381. }
  1382. }
  1383. }
  1384. // Reorder tabs to make the add tab respect language weights.
  1385. usort($language_tabs, array($this, 'translationTabSort'));
  1386. // Merge the reordered language tabs into the second level tabs.
  1387. if (count($language_tabs) > 1) {
  1388. if (empty($data['tabs'][1])) {
  1389. $data['tabs'][1] = array('output' => array());
  1390. }
  1391. $data['tabs'][1]['output'] = array_merge($data['tabs'][1]['output'], $language_tabs);
  1392. $data['tabs'][1]['count'] = count($data['tabs'][1]['output']);
  1393. }
  1394. }
  1395. }
  1396. /**
  1397. * Helper callback. Sorts language tabs by weight.
  1398. */
  1399. protected function translationTabSort($a, $b) {
  1400. return $a['#link']['weight'] > $b['#link']['weight'];
  1401. }
  1402. /**
  1403. * Returns the title to be used for the entity form page.
  1404. *
  1405. * This may contain HTML markup and thus needs to be sanitized, since it will
  1406. * be used as page title as-is.
  1407. */
  1408. protected function entityFormTitle() {
  1409. return check_plain($this->getLabel());
  1410. }
  1411. /**
  1412. * Returns TRUE if an entity translation is being edited.
  1413. */
  1414. protected function isTranslationForm() {
  1415. return !$this->isNewEntity() && $this->getFormLanguage() != $this->getLanguage();
  1416. }
  1417. /**
  1418. * Returns the translation object key for the wrapped entity type.
  1419. */
  1420. protected function getTranslationsKey() {
  1421. return isset($this->entityInfo['entity keys']['translations']) ? $this->entityInfo['entity keys']['translations'] : FALSE;
  1422. }
  1423. /**
  1424. * Returns the entity accessibility.
  1425. */
  1426. protected function getStatus() {
  1427. return TRUE;
  1428. }
  1429. /**
  1430. * Returns the entity identifier.
  1431. */
  1432. protected function getEntityId() {
  1433. return $this->entityId;
  1434. }
  1435. /**
  1436. * Initializes handler path variables based on the active path scheme.
  1437. *
  1438. * @throws Exception
  1439. */
  1440. private function initPathVariables() {
  1441. if (empty($this->pathScheme) || !isset($this->entityInfo['translation']['entity_translation']['path schemes'][$this->pathScheme])) {
  1442. throw new Exception("Cannot initialize entity translation path variables (invalid path scheme).");
  1443. }
  1444. $path_scheme = $this->entityInfo['translation']['entity_translation']['path schemes'][$this->pathScheme];
  1445. $this->pathWildcard = $path_scheme['path wildcard'];
  1446. $this->basePath = isset($path_scheme['base path']) ? $this->getPathInstance($path_scheme['base path']) : FALSE;
  1447. $this->editPath = isset($path_scheme['edit path']) ? $this->getPathInstance($path_scheme['edit path']) : FALSE;
  1448. $this->translatePath = isset($path_scheme['translate path']) ? $this->getPathInstance($path_scheme['translate path']) : FALSE;
  1449. $this->viewPath = isset($path_scheme['view path']) ? $this->getPathInstance($path_scheme['view path']) : FALSE;
  1450. }
  1451. /**
  1452. * Returns an instance of the given path.
  1453. *
  1454. * @param $path
  1455. * An internal path containing the entity id wildcard.
  1456. *
  1457. * @return
  1458. * The instantiated path.
  1459. */
  1460. protected function getPathInstance($path) {
  1461. $path_segments = explode('/', $path);
  1462. foreach ($path_segments as $index => $segment) {
  1463. if ($segment == $this->pathWildcard) {
  1464. $path_segments[$index] = $this->getEntityId();
  1465. }
  1466. elseif ($segment{0} == '%' && isset($this->routerMap[$index])) {
  1467. $path_segments[$index] = $this->routerMap[$index];
  1468. }
  1469. }
  1470. return implode('/', $path_segments);
  1471. }
  1472. /**
  1473. * Returns an empty translations data structure.
  1474. */
  1475. protected static function emptyTranslations() {
  1476. return (object) array('original' => NULL, 'data' => array());
  1477. }
  1478. }