EntityTranslationActionsBasic.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @file
  4. * Et_action plugin class.
  5. */
  6. interface EntityTranslationActionsInterface {
  7. /**
  8. * Function checks if plugin available for selected entity type.
  9. */
  10. public function available();
  11. /**
  12. * Function function executes plugin actions.
  13. */
  14. public function action($entity, $context, $handler = NULL);
  15. /**
  16. * Function builds form elements for action.
  17. */
  18. public function formBuild(&$form, &$form_state);
  19. /**
  20. * Function validates form data.
  21. */
  22. public function formValidate($form, &$form_state);
  23. /**
  24. * Function process form data.
  25. */
  26. public function formSubmit($form, &$form_state, $options);
  27. /**
  28. * Function returns entity language.
  29. */
  30. public function entityLanguage($entity, $handler, $default);
  31. }
  32. class EntityTranslationActionsBasic implements EntityTranslationActionsInterface {
  33. public $entityType;
  34. public $options;
  35. /**
  36. * Class constructor.
  37. */
  38. public function __construct($entity_type, $options) {
  39. $this->entityType = $entity_type;
  40. $this->options = $options;
  41. }
  42. /**
  43. * Function checks if plugin available for selected entity type.
  44. */
  45. public function available() {
  46. return FALSE;
  47. }
  48. /**
  49. * Function function executes plugin actions.
  50. */
  51. public function action($entity, $context, $handler = NULL) {
  52. }
  53. /**
  54. * Function builds form elements for action.
  55. */
  56. public function formBuild(&$form, &$form_state) {
  57. }
  58. /**
  59. * Function validates form data.
  60. */
  61. public function formValidate($form, &$form_state) {
  62. }
  63. /**
  64. * Function process form data.
  65. */
  66. public function formSubmit($form, &$form_state, $options) {
  67. $this->options = $options;
  68. }
  69. /**
  70. * Function returns entity language.
  71. */
  72. public function entityLanguage($entity, $handler, $default) {
  73. // Get name of language field.
  74. $language_key = $handler->getLanguageKey();
  75. if ($language_key) {
  76. if (isset($entity->{$language_key}) && !empty($entity->{$language_key})) {
  77. // Return entity language from a propertie.
  78. return $entity->{$language_key};
  79. }
  80. }
  81. // Get all entity translations.
  82. $translations = $handler->getTranslations();
  83. if (!empty($translations->original)) {
  84. // Return entity original language.
  85. return $translations->original;
  86. }
  87. else {
  88. // Default fallback.
  89. return $default;
  90. }
  91. }
  92. /**
  93. * Function returns list of available languages for translations.
  94. */
  95. protected function languagesList() {
  96. $languages = entity_translation_languages();
  97. $language_none = array(LANGUAGE_NONE => t('Language neutral'));
  98. $languages_list = $language_none;
  99. foreach ($languages as $language) {
  100. $languages_list[$language->language] = $language->name;
  101. }
  102. return $languages_list;
  103. }
  104. /**
  105. * Function returns options for action form.
  106. */
  107. protected function languagesOptions() {
  108. $languages_options = array(
  109. '-entity-' => t('Entity language'),
  110. ) + $this->languagesList();
  111. return $languages_options;
  112. }
  113. }