EntityTranslationActionsSet.class.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @file
  4. * Et_action plugin class.
  5. */
  6. class EntityTranslationActionsSet extends EntityTranslationActionsBasic {
  7. /**
  8. * Function checks if plugin available for selected entity type.
  9. */
  10. public function available() {
  11. return entity_translation_enabled($this->entityType);
  12. }
  13. /**
  14. * Function function executes plugin actions.
  15. */
  16. public function action($entity, $context, $handler = NULL) {
  17. $handler = $handler ? $handler : entity_translation_get_handler($this->entityType, $entity);
  18. $lang_source = $this->entityLanguage($entity, $handler, FALSE);
  19. if ($lang_source == $this->options['target']) {
  20. return ENTITY_TRANSLATION_ACTIONS_RESULT_EQUAL;
  21. }
  22. $translations = $handler->getTranslations();
  23. // Add translation for new language if it doesn`t exist.
  24. if (!isset($translations->data[$this->options['target']]) && $lang_source && $this->options['target'] != LANGUAGE_NONE) {
  25. $options_add = array(
  26. 'source' => $lang_source,
  27. 'language' => $this->options['target'],
  28. 'translation_exists' => ENTITY_TRANSLATION_ACTIONS_TRANSLATION_REPLACE,
  29. );
  30. $add_plugin = entity_translation_actions_plugin('EntityTranslationActionsAdd');
  31. $add_class = ctools_plugin_get_class($add_plugin, 'handler');
  32. if ($add_class) {
  33. $add_handler = new $add_class($this->entityType, $options_add);
  34. $add_handler->action($entity, $context, $handler);
  35. }
  36. }
  37. // Set original translation for entity.
  38. $language_key = $handler->getLanguageKey();
  39. $translations->original = $this->options['target'];
  40. $entity->{$language_key} = $this->options['target'];
  41. // Process entity translations.
  42. foreach ($translations->data as &$translation) {
  43. if ($translation['language'] == LANGUAGE_NONE && $this->options['target'] != LANGUAGE_NONE) {
  44. // Remove translation for language_none if new language is not empty.
  45. $handler->removeTranslation($translation['language']);
  46. }
  47. elseif ($translation['language'] == $this->options['target']) {
  48. // Remove source from translation for new language. It will be original.
  49. $translation['source'] = FALSE;
  50. $handler->setTranslation($translation);
  51. }
  52. else {
  53. // Set source language for other translations.
  54. $translation['source'] = $this->options['target'];
  55. $handler->setTranslation($translation);
  56. }
  57. // Delete reference to translation in order to prevent mix of data.
  58. unset($translation);
  59. }
  60. return ENTITY_TRANSLATION_ACTIONS_RESULT_REPLACED;
  61. }
  62. /**
  63. * Function builds form elements for action.
  64. */
  65. public function formBuild(&$form, &$form_state) {
  66. $form['target'] = array(
  67. '#type' => 'radios',
  68. '#options' => $this->languagesList(),
  69. '#title' => t('Select language:'),
  70. '#required' => TRUE,
  71. );
  72. }
  73. }