pathauto_i18n_node.module 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @file
  4. * Provides tools for creating multilanguage aliases for nodes.
  5. */
  6. /**
  7. * Implements hook_form_BASE_FORM_ID_alter().
  8. */
  9. function pathauto_i18n_form_node_form_alter(&$form, &$form_state) {
  10. pathauto_i18n_configuration_form($form, $form_state['node'], 'node', $form['#node']->type);
  11. }
  12. /**
  13. * Implements hook_node_insert().
  14. */
  15. function pathauto_i18n_node_node_insert($node) {
  16. pathauto_i18n_init_property($node, 'node', $node->type);
  17. pathauto_i18n_process_entity_object($node, 'node', $node->path['pathauto_i18n_status'], 'insert');
  18. }
  19. /**
  20. * Implements hook_node_update().
  21. */
  22. function pathauto_i18n_node_node_update($node) {
  23. pathauto_i18n_init_property($node, 'node', $node->type);
  24. pathauto_i18n_process_entity_object($node, 'node', $node->path['pathauto_i18n_status'], 'update');
  25. }
  26. /**
  27. * Implements hook_node_delete().
  28. */
  29. function pathauto_i18n_node_node_delete($node) {
  30. pathauto_i18n_delete_settings($node->nid, 'node');
  31. }
  32. /**
  33. * Implements hook_node_load().
  34. */
  35. function pathauto_i18n_node_node_load($nodes, $types) {
  36. // Attach pathauto i18n settings to node object.
  37. foreach ($nodes as $node) {
  38. $nids[] = $node->nid;
  39. }
  40. if (!empty($nids)) {
  41. $result = pathauto_i18n_load_settings($nids, 'node');
  42. $settings = array();
  43. foreach ($result as $record) {
  44. $settings[$record->entity_id] = $record->path_status;
  45. }
  46. foreach ($nodes as $nid => $node) {
  47. if (array_key_exists($nid, $settings)) {
  48. $nodes[$nid]->path['pathauto_i18n_status'] = $settings[$nid];
  49. }
  50. else {
  51. $nodes[$nid]->path['pathauto_i18n_status'] = pathauto_i18n_get_bundle_default('node', $node->type);
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * Implements hook_pathauto_alias_alter().
  58. */
  59. function pathauto_i18n_node_pathauto_alias_alter(&$alias, &$context) {
  60. $operations = array('insert', 'update', 'bulkupdate');
  61. // Skip insert of alias for all languages.
  62. if (!empty($context['module']) && $context['module'] == 'node' && in_array($context['op'], $operations) && !empty($context['data']['node'])) {
  63. $entity = $context['data']['node'];
  64. // Run bulk update.
  65. if ($context['op'] == 'bulkupdate') {
  66. pathauto_i18n_node_node_update($entity);
  67. }
  68. if (isset($entity->path['pathauto_i18n_status']) && $entity->path['pathauto_i18n_status'] && $context['language'] == LANGUAGE_NONE) {
  69. $alias = '';
  70. }
  71. }
  72. }
  73. /**
  74. * Implements hook_form_alter().
  75. */
  76. function pathauto_i18n_node_form_alter(&$form, &$form_state, $form_id) {
  77. if (!empty($form['#node_edit_form']) && $form['#node_edit_form']) {
  78. // Add pathauto value if pathauto_i18n_status TRUE.
  79. // Remove alias value to prevent overwriting.
  80. if (isset($form['#node']->path['pathauto_i18n_status']) && $form['#node']->path['pathauto_i18n_status']) {
  81. $form['path']['pathauto']['#default_value'] = TRUE;
  82. $form['path']['alias']['#default_value'] = '';
  83. }
  84. }
  85. }
  86. /**
  87. * Implements hook_module_implements_alter().
  88. */
  89. function pathauto_i18n_node_module_implements_alter(&$implementations, $hook) {
  90. // Move pathauto_i18n_node_form_alter to the end of the list.
  91. // By default there not available pathauto value.
  92. if ($hook == 'form_alter') {
  93. $group = $implementations['pathauto_i18n_node'];
  94. unset($implementations['pathauto_i18n_node']);
  95. $implementations['pathauto_i18n_node'] = $group;
  96. }
  97. }
  98. /**
  99. * Implements hook_action_info().
  100. */
  101. function pathauto_i18n_node_action_info() {
  102. return array(
  103. 'pathauto_i18n_node_generate_alias' => array(
  104. 'type' => 'node',
  105. 'label' => t('Enable generation of aliases for all languages'),
  106. 'configurable' => FALSE,
  107. 'behavior' => array('changes_property'),
  108. 'triggers' => array(
  109. 'node_presave',
  110. ),
  111. ),
  112. 'pathauto_i18n_node_remove_alias' => array(
  113. 'type' => 'node',
  114. 'label' => t('Disable generation of aliases for all languages'),
  115. 'configurable' => FALSE,
  116. 'behavior' => array('changes_property'),
  117. 'triggers' => array(
  118. 'node_presave',
  119. ),
  120. ),
  121. );
  122. }
  123. /**
  124. * Sets the status of a pathauto_i18n_status to 1.
  125. *
  126. * @param object $node
  127. * A node object.
  128. * @param array $context
  129. * (optional) Array of additional information about what triggered the action.
  130. * Not used for this action.
  131. *
  132. * @ingroup actions
  133. */
  134. function pathauto_i18n_node_generate_alias($node, $context = array()) {
  135. $node->path['pathauto_i18n_status'] = 1;
  136. }
  137. /**
  138. * Sets the status of a pathauto_i18n_status to 0.
  139. *
  140. * @param object $node
  141. * A node object.
  142. * @param array $context
  143. * (optional) Array of additional information about what triggered the action.
  144. * Not used for this action.
  145. *
  146. * @ingroup actions
  147. */
  148. function pathauto_i18n_node_remove_alias($node, $context = array()) {
  149. $node->path['pathauto_i18n_status'] = 0;
  150. }
  151. /**
  152. * Implements hook_form_FORM_ID_alter().
  153. *
  154. * This will add pathauto_i18n options to the node type form. These settings
  155. * will be used as default for every node of this node type.
  156. */
  157. function pathauto_i18n_node_form_node_type_form_alter(&$form, $form_state) {
  158. // Add the pathauto i18n form.
  159. $form['workflow']['pathauto_i18n_default_node'] = array(
  160. '#type' => 'checkbox',
  161. '#title' => t('Generate automatic URL alias for all languages by default'),
  162. '#default_value' => pathauto_i18n_get_bundle_default('node', $form['#node_type']->type),
  163. '#description' => t('Set the default behaviour generating aliases for all available languages.'),
  164. );
  165. }