TranslatableInterface.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * Interface for translatable data.
  5. *
  6. * Classes implementing this interface do not necessarily support translations.
  7. *
  8. * To detect whether an entity type supports translation, call
  9. * EntityTypeInterface::isTranslatable().
  10. *
  11. * Many entity interfaces are composed of numerous other interfaces such as this
  12. * one, which allow implementations to pick and choose which features to support
  13. * through stub implementations of various interface methods. This means that
  14. * even if an entity class implements TranslatableInterface, it might only have
  15. * a stub implementation and not a functional one.
  16. *
  17. * @see \Drupal\Core\Entity\EntityTypeInterface::isTranslatable()
  18. * @see https://www.drupal.org/docs/8/api/entity-api/structure-of-an-entity-annotation
  19. * @see https://www.drupal.org/docs/8/api/entity-api/entity-translation-api
  20. */
  21. interface TranslatableInterface {
  22. /**
  23. * Returns the translation language.
  24. *
  25. * @return \Drupal\Core\Language\LanguageInterface
  26. * The language object.
  27. */
  28. public function language();
  29. /**
  30. * Checks whether the translation is the default one.
  31. *
  32. * @return bool
  33. * TRUE if the translation is the default one, FALSE otherwise.
  34. */
  35. public function isDefaultTranslation();
  36. /**
  37. * Checks whether the translation is new.
  38. *
  39. * @return bool
  40. * TRUE if the translation is new, FALSE otherwise.
  41. */
  42. public function isNewTranslation();
  43. /**
  44. * Returns the languages the data is translated to.
  45. *
  46. * @param bool $include_default
  47. * (optional) Whether the default language should be included. Defaults to
  48. * TRUE.
  49. *
  50. * @return \Drupal\Core\Language\LanguageInterface[]
  51. * An associative array of language objects, keyed by language codes.
  52. */
  53. public function getTranslationLanguages($include_default = TRUE);
  54. /**
  55. * Gets a translation of the data.
  56. *
  57. * The returned translation has to be of the same type than this typed data
  58. * object.
  59. *
  60. * @param $langcode
  61. * The language code of the translation to get or
  62. * LanguageInterface::LANGCODE_DEFAULT
  63. * to get the data in default language.
  64. *
  65. * @return $this
  66. * A typed data object for the translated data.
  67. *
  68. * @throws \InvalidArgumentException
  69. * If an invalid or non-existing translation language is specified.
  70. */
  71. public function getTranslation($langcode);
  72. /**
  73. * Returns the translatable object referring to the original language.
  74. *
  75. * @return $this
  76. * The translation object referring to the original language.
  77. */
  78. public function getUntranslated();
  79. /**
  80. * Checks there is a translation for the given language code.
  81. *
  82. * @param string $langcode
  83. * The language code identifying the translation.
  84. *
  85. * @return bool
  86. * TRUE if the translation exists, FALSE otherwise.
  87. */
  88. public function hasTranslation($langcode);
  89. /**
  90. * Adds a new translation to the translatable object.
  91. *
  92. * @param string $langcode
  93. * The language code identifying the translation.
  94. * @param array $values
  95. * (optional) An array of initial values to be assigned to the translatable
  96. * fields. Defaults to none.
  97. *
  98. * @return $this
  99. *
  100. * @throws \InvalidArgumentException
  101. * If an invalid or existing translation language is specified.
  102. */
  103. public function addTranslation($langcode, array $values = []);
  104. /**
  105. * Removes the translation identified by the given language code.
  106. *
  107. * @param string $langcode
  108. * The language code identifying the translation to be removed.
  109. */
  110. public function removeTranslation($langcode);
  111. /**
  112. * Returns the translation support status.
  113. *
  114. * @return bool
  115. * TRUE if the object has translation support enabled.
  116. */
  117. public function isTranslatable();
  118. }