translation.handler_factory.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Translation handler factory for the Entity Translation module.
  5. */
  6. /**
  7. * Class implementing the entity translation handler factory.
  8. */
  9. class EntityTranslationHandlerFactory {
  10. /**
  11. * A singleton instance of the factory.
  12. *
  13. * @var EntityTranslationHandlerFactory
  14. */
  15. protected static $instance;
  16. /**
  17. * Counter used to generate handler ids for new entities.
  18. *
  19. * @var int
  20. */
  21. protected static $newId = 1;
  22. /**
  23. * Handlers cache.
  24. *
  25. * @var array
  26. */
  27. protected $handlers = array();
  28. /**
  29. * The last translation handler retrieved.
  30. *
  31. * @var EntityTranslationHandlerInterface
  32. */
  33. protected $last;
  34. /**
  35. * An array of translation handler retrieved by type.
  36. *
  37. * @var array
  38. */
  39. protected $lastByType = array();
  40. /**
  41. * Returns the singleton instance of the translation handler factory.
  42. *
  43. * @return EntityTranslationHandlerFactory
  44. */
  45. public static function getInstance() {
  46. if (!isset(self::$instance)) {
  47. self::$instance = new EntityTranslationHandlerFactory();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * Prevents the factory from being publicly instantiated.
  53. */
  54. protected function __construct() {}
  55. /**
  56. * Translation handler factory.
  57. *
  58. * @param $entity_type
  59. * The type of $entity; e.g. 'node' or 'user'.
  60. * @param $entity
  61. * The entity to be translated. A bundle name may be passed to instantiate
  62. * an empty entity.
  63. *
  64. * @return EntityTranslationHandlerInterface
  65. * A class implementing EntityTranslationHandlerInterface.
  66. */
  67. public function getHandler($entity_type, $entity) {
  68. if (is_numeric($entity)) {
  69. $entities = entity_load($entity_type, array($entity));
  70. $entity = reset($entities);
  71. }
  72. elseif (is_string($entity)) {
  73. $entity = entity_create_stub_entity($entity_type, array(NULL, NULL, $entity));
  74. }
  75. $id = $this->getHandlerId($entity_type, $entity);
  76. if (!isset($this->handlers[$entity_type][$id])) {
  77. $entity_info = entity_get_info($entity_type);
  78. $class = $entity_info['translation']['entity_translation']['class'];
  79. // @todo Remove the fourth parameter once 3rd-party translation handlers
  80. // have been fixed and no longer require the deprecated entity_id
  81. // parameter.
  82. $handler = new $class($entity_type, $entity_info, $entity, NULL);
  83. $handler->setFactory($this);
  84. $this->handlers[$entity_type][$id] = $handler;
  85. }
  86. $this->last = $this->handlers[$entity_type][$id];
  87. $this->lastByType[$entity_type] = $this->last;
  88. $this->last->setEntity($entity);
  89. return $this->last;
  90. }
  91. /**
  92. * Retrieves the translation handler identifier for the given entity.
  93. *
  94. * @param $entity_type
  95. * The type of the entity the translation handler will wrap.
  96. * @param $entity
  97. * The entity the translation handler will wrap.
  98. */
  99. public function getHandlerId($entity_type, $entity) {
  100. if (!isset($entity->entity_translation_handler_id)) {
  101. list($id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  102. $revision_id = isset($revision_id) ? $revision_id : 0;
  103. $bundle = isset($bundle) ? $bundle : $entity_type;
  104. $entity->entity_translation_handler_id = $entity_type . '-' . $bundle . '-' . (!empty($id) ? 'eid-' . $id . '-' . $revision_id : 'new-' . self::$newId++);
  105. }
  106. return $entity->entity_translation_handler_id;
  107. }
  108. /**
  109. * Returns the last translation handler retrieved.
  110. *
  111. * @param $entity_type
  112. * (optional) The entity type of the translation handler. Defaults to the
  113. * last one.
  114. *
  115. * @return EntityTranslationHandlerInterface
  116. * A class implementing EntityTranslationHandlerInterface.
  117. */
  118. public function getLastHandler($entity_type = NULL) {
  119. if (isset($entity_type)) {
  120. return isset($this->lastByType[$entity_type]) ? $this->lastByType[$entity_type] : NULL;
  121. }
  122. return $this->last;
  123. }
  124. }