entity_translation_actions.module 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * @file
  4. * Entity translation actions module.
  5. */
  6. // Skip any actions if translation for language exists.
  7. define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_SKIP', 0);
  8. // Clear values from language. Add all values from source language.
  9. define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_REPLACE', 1);
  10. // Keep existed values for language. Add new values from source language.
  11. define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_MERGE', 2);
  12. define('ENTITY_TRANSLATION_ACTIONS_RESULT_CREATED', 'SUCCESS CREATED');
  13. define('ENTITY_TRANSLATION_ACTIONS_RESULT_REPLACED', 'SUCCESS REPLACED');
  14. define('ENTITY_TRANSLATION_ACTIONS_RESULT_DELETED', 'SUCCESS DELETED');
  15. define('ENTITY_TRANSLATION_ACTIONS_RESULT_EXISTS', 'WARNING EXISTS');
  16. define('ENTITY_TRANSLATION_ACTIONS_RESULT_EQUAL', 'WARNING EQUAL');
  17. define('ENTITY_TRANSLATION_ACTIONS_RESULT_NO_SOURCE', 'WARNING NO SOURCE');
  18. /**
  19. * Implements hook_views_api().
  20. */
  21. function entity_translation_actions_views_api($module = NULL, $api = NULL) {
  22. return array('api' => '3.0');
  23. }
  24. /**
  25. * Implements hook_menu().
  26. */
  27. function entity_translation_actions_menu() {
  28. // Page with list of available views.
  29. $items['admin/content/entity-translations'] = array(
  30. 'title' => 'Entity translations',
  31. 'description' => 'Manage entity translations.',
  32. 'page callback' => 'system_admin_menu_block_page',
  33. 'access arguments' => array('administer entity translation'),
  34. 'file' => 'system.admin.inc',
  35. 'file path' => drupal_get_path('module', 'system'),
  36. );
  37. return $items;
  38. }
  39. /**
  40. * Implements hook_action_info().
  41. *
  42. * Registers custom VBO translation actions as Drupal actions.
  43. */
  44. function entity_translation_actions_action_info() {
  45. $actions['entity_translation_actions_modify_action'] = array(
  46. 'type' => 'entity',
  47. 'label' => t('Modify entity translations'),
  48. 'behavior' => array('changes_property'),
  49. 'configurable' => TRUE,
  50. 'vbo_configurable' => TRUE,
  51. 'triggers' => array('any'),
  52. );
  53. return $actions;
  54. }
  55. /**
  56. * Implements hook_ctools_plugin_type().
  57. *
  58. * Define ctools plugin type.
  59. */
  60. function entity_translation_actions_ctools_plugin_type() {
  61. return array(
  62. 'et_action' => array(
  63. 'use hooks' => FALSE,
  64. 'classes' => array('handler'),
  65. ),
  66. );
  67. }
  68. /**
  69. * Implements hook_ctools_plugin_directory().
  70. *
  71. * Define plugins with basic translation actions.
  72. */
  73. function entity_translation_actions_ctools_plugin_directory($owner, $plugin_type) {
  74. switch ($owner) {
  75. case 'entity_translation_actions':
  76. if (in_array($plugin_type, array('et_action'))) {
  77. return 'plugins/' . $plugin_type;
  78. }
  79. break;
  80. }
  81. }
  82. /**
  83. * Function returns plugin instance by it id.
  84. */
  85. function entity_translation_actions_plugin($id) {
  86. ctools_include('plugins');
  87. $plugin = ctools_get_plugins('entity_translation_actions', 'et_action', $id);
  88. return $plugin;
  89. }
  90. /**
  91. * Function returns all plugin instances.
  92. */
  93. function entity_translation_actions_plugins() {
  94. ctools_include('plugins');
  95. $plugins = ctools_get_plugins('entity_translation_actions', 'et_action');
  96. // Order list of plugins according to propertie "weight".
  97. uasort($plugins, '_user_sort');
  98. return $plugins;
  99. }
  100. /**
  101. * Action callback function.
  102. *
  103. * Goes through list of entities and apply action of selected handler.
  104. */
  105. function entity_translation_actions_modify_action($entity, $context) {
  106. $context['action_handler']->action($entity, $context);
  107. }
  108. /**
  109. * Action form function.
  110. *
  111. * Form provide list of available actions. Form rebuilds according to selected
  112. * plugin.
  113. */
  114. function entity_translation_actions_modify_action_form($context, &$form_state) {
  115. $form = array();
  116. $form_state['entity_type'] = $entity_type = $context['entity_type'];
  117. $settings = isset($form_state['values']['settings']) ? $form_state['values']['settings'] : array(
  118. 'action' => FALSE,
  119. );
  120. $form['settings'] = array(
  121. '#tree' => 1,
  122. );
  123. $action_options = array(
  124. '' => t('None'),
  125. );
  126. foreach (entity_translation_actions_plugins() as $plugin_name => $plugin) {
  127. $class = ctools_plugin_get_class($plugin, 'handler');
  128. if (!$class) {
  129. continue;
  130. }
  131. $handler = new $class($entity_type, array());
  132. if ($handler->available()) {
  133. $action_options[$plugin_name] = $plugin['title'];
  134. }
  135. }
  136. $form['settings']['action'] = array(
  137. '#type' => 'radios',
  138. '#title' => t('Select action'),
  139. '#options' => $action_options,
  140. '#default_value' => $settings['action'],
  141. '#required' => TRUE,
  142. '#multiple' => FALSE,
  143. '#ajax' => array(
  144. 'callback' => 'entity_translation_actions_modify_action_ajax_callback',
  145. ),
  146. );
  147. // Check if any plugin selected.
  148. if (!empty($settings['action'])) {
  149. $settings += array($settings['action'] => array());
  150. // Build plugin handler.
  151. $plugin = entity_translation_actions_plugin($settings['action']);
  152. $class = ctools_plugin_get_class($plugin, 'handler');
  153. $handler = new $class($entity_type, $settings[$settings['action']]);
  154. // Keep handler object during whole process,
  155. $form_state['action_handler'] = $handler;
  156. // Generate custom form for selected plugin.
  157. $form['settings'][$settings['action']] = array(
  158. '#type' => 'fieldset',
  159. '#title' => $action_options[$settings['action']],
  160. '#description' => $plugin['description'],
  161. );
  162. $handler->formBuild($form['settings'][$settings['action']], $form_state);
  163. }
  164. return $form;
  165. }
  166. /**
  167. * Ajax callback for entity_translation_actions_modify_action_form.
  168. *
  169. * Function rebuilds form and returns replace command.
  170. */
  171. function entity_translation_actions_modify_action_ajax_callback($form, &$form_state) {
  172. $commands[] = ajax_command_replace('form:has(input[value="' . $form['#build_id'] . '"])', render($form));
  173. return array('#type' => 'ajax', '#commands' => $commands);
  174. }
  175. /**
  176. * Validate callback for entity_translation_actions_modify_action_form.
  177. *
  178. * Allow plugin to execute validate function.
  179. */
  180. function entity_translation_actions_modify_action_validate($form, &$form_state) {
  181. if (isset($form_state['action_handler'])) {
  182. $form_state['action_handler']->formValidate($form, $form_state);
  183. }
  184. }
  185. /**
  186. * Submit callback for entity_translation_actions_modify_action_form.
  187. *
  188. * Allow plugin to execute submit function. Return context for VBO action.
  189. */
  190. function entity_translation_actions_modify_action_submit($form, $form_state) {
  191. if (isset($form_state['action_handler'])) {
  192. $settings = &$form_state['values']['settings'];
  193. // Name of selected action.
  194. $action = $settings['action'];
  195. // Options for selected action.
  196. $options = isset($settings[$action]) ? $settings[$action] : array();
  197. // Execute plugin specific actions on submit.
  198. $form_state['action_handler']->formSubmit($form, $form_state, $options);
  199. // Return handler object as context.
  200. return array(
  201. 'action_handler' => $form_state['action_handler'],
  202. );
  203. }
  204. }