123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /**
- * @file
- * Entity translation actions module.
- */
- // Skip any actions if translation for language exists.
- define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_SKIP', 0);
- // Clear values from language. Add all values from source language.
- define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_REPLACE', 1);
- // Keep existed values for language. Add new values from source language.
- define('ENTITY_TRANSLATION_ACTIONS_TRANSLATION_MERGE', 2);
- define('ENTITY_TRANSLATION_ACTIONS_RESULT_CREATED', 'SUCCESS CREATED');
- define('ENTITY_TRANSLATION_ACTIONS_RESULT_REPLACED', 'SUCCESS REPLACED');
- define('ENTITY_TRANSLATION_ACTIONS_RESULT_DELETED', 'SUCCESS DELETED');
- define('ENTITY_TRANSLATION_ACTIONS_RESULT_EXISTS', 'WARNING EXISTS');
- define('ENTITY_TRANSLATION_ACTIONS_RESULT_EQUAL', 'WARNING EQUAL');
- define('ENTITY_TRANSLATION_ACTIONS_RESULT_NO_SOURCE', 'WARNING NO SOURCE');
- /**
- * Implements hook_views_api().
- */
- function entity_translation_actions_views_api($module = NULL, $api = NULL) {
- return array('api' => '3.0');
- }
- /**
- * Implements hook_menu().
- */
- function entity_translation_actions_menu() {
- // Page with list of available views.
- $items['admin/content/entity-translations'] = array(
- 'title' => 'Entity translations',
- 'description' => 'Manage entity translations.',
- 'page callback' => 'system_admin_menu_block_page',
- 'access arguments' => array('administer entity translation'),
- 'file' => 'system.admin.inc',
- 'file path' => drupal_get_path('module', 'system'),
- );
- return $items;
- }
- /**
- * Implements hook_action_info().
- *
- * Registers custom VBO translation actions as Drupal actions.
- */
- function entity_translation_actions_action_info() {
- $actions['entity_translation_actions_modify_action'] = array(
- 'type' => 'entity',
- 'label' => t('Modify entity translations'),
- 'behavior' => array('changes_property'),
- 'configurable' => TRUE,
- 'vbo_configurable' => TRUE,
- 'triggers' => array('any'),
- );
- return $actions;
- }
- /**
- * Implements hook_ctools_plugin_type().
- *
- * Define ctools plugin type.
- */
- function entity_translation_actions_ctools_plugin_type() {
- return array(
- 'et_action' => array(
- 'use hooks' => FALSE,
- 'classes' => array('handler'),
- ),
- );
- }
- /**
- * Implements hook_ctools_plugin_directory().
- *
- * Define plugins with basic translation actions.
- */
- function entity_translation_actions_ctools_plugin_directory($owner, $plugin_type) {
- switch ($owner) {
- case 'entity_translation_actions':
- if (in_array($plugin_type, array('et_action'))) {
- return 'plugins/' . $plugin_type;
- }
- break;
- }
- }
- /**
- * Function returns plugin instance by it id.
- */
- function entity_translation_actions_plugin($id) {
- ctools_include('plugins');
- $plugin = ctools_get_plugins('entity_translation_actions', 'et_action', $id);
- return $plugin;
- }
- /**
- * Function returns all plugin instances.
- */
- function entity_translation_actions_plugins() {
- ctools_include('plugins');
- $plugins = ctools_get_plugins('entity_translation_actions', 'et_action');
- // Order list of plugins according to propertie "weight".
- uasort($plugins, '_user_sort');
- return $plugins;
- }
- /**
- * Action callback function.
- *
- * Goes through list of entities and apply action of selected handler.
- */
- function entity_translation_actions_modify_action($entity, $context) {
- $context['action_handler']->action($entity, $context);
- }
- /**
- * Action form function.
- *
- * Form provide list of available actions. Form rebuilds according to selected
- * plugin.
- */
- function entity_translation_actions_modify_action_form($context, &$form_state) {
- $form = array();
- $form_state['entity_type'] = $entity_type = $context['entity_type'];
- $settings = isset($form_state['values']['settings']) ? $form_state['values']['settings'] : array(
- 'action' => FALSE,
- );
- $form['settings'] = array(
- '#tree' => 1,
- );
- $action_options = array(
- '' => t('None'),
- );
- foreach (entity_translation_actions_plugins() as $plugin_name => $plugin) {
- $class = ctools_plugin_get_class($plugin, 'handler');
-
- if (!$class) {
- continue;
- }
-
- $handler = new $class($entity_type, array());
- if ($handler->available()) {
- $action_options[$plugin_name] = $plugin['title'];
- }
- }
- $form['settings']['action'] = array(
- '#type' => 'radios',
- '#title' => t('Select action'),
- '#options' => $action_options,
- '#default_value' => $settings['action'],
- '#required' => TRUE,
- '#multiple' => FALSE,
- '#ajax' => array(
- 'callback' => 'entity_translation_actions_modify_action_ajax_callback',
- ),
- );
- // Check if any plugin selected.
- if (!empty($settings['action'])) {
- $settings += array($settings['action'] => array());
- // Build plugin handler.
- $plugin = entity_translation_actions_plugin($settings['action']);
- $class = ctools_plugin_get_class($plugin, 'handler');
- $handler = new $class($entity_type, $settings[$settings['action']]);
- // Keep handler object during whole process,
- $form_state['action_handler'] = $handler;
- // Generate custom form for selected plugin.
- $form['settings'][$settings['action']] = array(
- '#type' => 'fieldset',
- '#title' => $action_options[$settings['action']],
- '#description' => $plugin['description'],
- );
- $handler->formBuild($form['settings'][$settings['action']], $form_state);
- }
- return $form;
- }
- /**
- * Ajax callback for entity_translation_actions_modify_action_form.
- *
- * Function rebuilds form and returns replace command.
- */
- function entity_translation_actions_modify_action_ajax_callback($form, &$form_state) {
- $commands[] = ajax_command_replace('form:has(input[value="' . $form['#build_id'] . '"])', render($form));
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- /**
- * Validate callback for entity_translation_actions_modify_action_form.
- *
- * Allow plugin to execute validate function.
- */
- function entity_translation_actions_modify_action_validate($form, &$form_state) {
- if (isset($form_state['action_handler'])) {
- $form_state['action_handler']->formValidate($form, $form_state);
- }
- }
- /**
- * Submit callback for entity_translation_actions_modify_action_form.
- *
- * Allow plugin to execute submit function. Return context for VBO action.
- */
- function entity_translation_actions_modify_action_submit($form, $form_state) {
- if (isset($form_state['action_handler'])) {
- $settings = &$form_state['values']['settings'];
- // Name of selected action.
- $action = $settings['action'];
- // Options for selected action.
- $options = isset($settings[$action]) ? $settings[$action] : array();
- // Execute plugin specific actions on submit.
- $form_state['action_handler']->formSubmit($form, $form_state, $options);
- // Return handler object as context.
- return array(
- 'action_handler' => $form_state['action_handler'],
- );
- }
- }
|