entity_translation_upgrade.module 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Provides permanent redirects for unavailable node translations.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function entity_translation_upgrade_menu() {
  10. return array(
  11. 'admin/config/regional/entity_translation/upgrade' => array(
  12. 'title' => 'Entity Translation Upgrade',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('entity_translation_upgrade_form'),
  15. 'access arguments' => array('administer software updates'),
  16. 'file' => 'entity_translation_upgrade.admin.inc',
  17. 'type' => MENU_CALLBACK,
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements hook_menu_alter().
  23. */
  24. function entity_translation_upgrade_menu_alter(&$items) {
  25. // Obsolete node translations might be left unpublished instead of being
  26. // deleted.
  27. $items['node/%node']['access callback'] = 'entity_translation_upgrade_access';
  28. $items['node/%node']['access arguments'] = array(1);
  29. }
  30. /**
  31. * Access callback.
  32. *
  33. * Performs a redirect to the corresponding field-based translation if the
  34. * current user has not the permission to access the requested node translation.
  35. */
  36. function entity_translation_upgrade_access($node) {
  37. // If the user has the right to access the node, we need to do nothing.
  38. if (node_access('view', $node)) {
  39. return TRUE;
  40. }
  41. // If we have a node translation, we need to redirect the user to the original
  42. // node.
  43. if ($node->tnid && $node->nid != $node->tnid) {
  44. entity_translation_upgrade_redirect($node->tnid, $node->language);
  45. }
  46. return FALSE;
  47. }
  48. /**
  49. * Implements hook_init().
  50. */
  51. function entity_translation_upgrade_init() {
  52. // If have a node/$nid path but we are not able to load a node for the given
  53. // nid we might have an upgraded translation, hence we need to look for a
  54. // record matching the requested nid in the history table.
  55. if ($nid = entity_translation_upgrade_check_path() && $data = entity_translation_upgrade_load($nid)) {
  56. entity_translation_upgrade_redirect($data->tnid, $data->language);
  57. }
  58. }
  59. /**
  60. * Implements hook_form_FORM_ID_alter().
  61. */
  62. function entity_translation_upgrade_form_entity_translation_admin_form_alter(&$form, $form_state) {
  63. $form['entity_translation_upgrade'] = array(
  64. '#type' => 'fieldset',
  65. '#title' => t('Entity Translation Upgrade'),
  66. '#description' => t('This will create an entity translation for each available node translation, which will be then unpublished.'),
  67. '#collapsible' => TRUE,
  68. '#collapsed' => TRUE,
  69. );
  70. $options = array();
  71. foreach (node_type_get_types() as $type) {
  72. $options[$type->type] = $type->name;
  73. }
  74. $form['entity_translation_upgrade']['types'] = array(
  75. '#type' => 'checkboxes',
  76. '#title' => t('Node types'),
  77. '#description' => t('Select which node types will be upgraded.'),
  78. '#options' => $options,
  79. );
  80. $form['entity_translation_upgrade']['upgrade'] = array(
  81. '#type' => 'submit',
  82. '#value' => t('Upgrade'),
  83. '#validate' => array('entity_translation_upgrade_validate'),
  84. '#submit' => array('entity_translation_upgrade_submit'),
  85. );
  86. }
  87. /**
  88. * Validation handler for the entity_translation_admin_form() form.
  89. */
  90. function entity_translation_upgrade_validate($form, &$form_state) {
  91. if (!count(array_filter($form_state['values']['types']))) {
  92. form_set_error('types', t('Please specify at least one node type.'));
  93. }
  94. }
  95. /**
  96. * Submit handler for the entity_translation_admin_form() form.
  97. */
  98. function entity_translation_upgrade_submit($form, &$form_state) {
  99. module_load_include('inc', 'entity_translation_upgrade', 'entity_translation_upgrade.admin');
  100. entity_translation_upgrade_start(array_filter($form_state['values']['types']));
  101. }
  102. /**
  103. * Performs the redirect to original node with the given language.
  104. */
  105. function entity_translation_upgrade_redirect($nid, $langcode) {
  106. $languages = language_list();
  107. drupal_goto("node/$nid", array('language' => $languages[$langcode]), 301);
  108. }
  109. /**
  110. * Checks wether the requested path belongs to an upgraded translation.
  111. */
  112. function entity_translation_upgrade_check_path() {
  113. $result = arg(0) == 'node' && ($nid = arg(1)) && is_int($nid) && !node_load($nid);
  114. return $result ? $nid : FALSE;
  115. }
  116. /**
  117. * Loads the upgrade history entry for the given nid.
  118. */
  119. function entity_translation_upgrade_load($nid) {
  120. return db_select('entity_translation_upgrade_history', 'etu')
  121. ->fields('etu')
  122. ->condition('etu.nid', $nid)
  123. ->execute()
  124. ->fetchObject();
  125. }