translation.handler_factory.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $handler = new $class($entity_type, $entity_info, $entity);
  80. $handler->setFactory($this);
  81. $this->handlers[$entity_type][$id] = $handler;
  82. }
  83. $this->last = $this->handlers[$entity_type][$id];
  84. $this->lastByType[$entity_type] = $this->last;
  85. $this->last->setEntity($entity);
  86. return $this->last;
  87. }
  88. /**
  89. * Retrieves the translation handler identifier for the given entity.
  90. *
  91. * @param $entity_type
  92. * The type of the entity the translation handler will wrap.
  93. * @param $entity
  94. * The entity the translation handler will wrap.
  95. */
  96. public function getHandlerId($entity_type, $entity) {
  97. if (!isset($entity->entity_translation_handler_id)) {
  98. list($id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  99. $revision_id = isset($revision_id) ? $revision_id : 0;
  100. $bundle = isset($bundle) ? $bundle : $entity_type;
  101. $entity->entity_translation_handler_id = $entity_type . '-' . $bundle . '-' . (!empty($id) ? 'eid-' . $id . '-' . $revision_id : 'new-' . self::$newId++);
  102. }
  103. return $entity->entity_translation_handler_id;
  104. }
  105. /**
  106. * Returns the last translation handler retrieved.
  107. *
  108. * @param $entity_type
  109. * (optional) The entity type of the translation handler. Defaults to the
  110. * last one.
  111. *
  112. * @return EntityTranslationHandlerInterface
  113. * A class implementing EntityTranslationHandlerInterface.
  114. */
  115. public function getLastHandler($entity_type = NULL) {
  116. if (isset($entity_type)) {
  117. return isset($this->lastByType[$entity_type]) ? $this->lastByType[$entity_type] : NULL;
  118. }
  119. return $this->last;
  120. }
  121. }