You are here

public function EntityTranslationHandlerFactory::getHandler in Entity Translation 7

Translation handler factory.

Parameters

$entity_type: The type of $entity; e.g. 'node' or 'user'.

$entity: The entity to be translated. A bundle name may be passed to instantiate an empty entity.

Return value

EntityTranslationHandlerInterface A class implementing EntityTranslationHandlerInterface.

File

includes/translation.handler_factory.inc, line 77
Translation handler factory for the Entity Translation module.

Class

EntityTranslationHandlerFactory
Class implementing the entity translation handler factory.

Code

public function getHandler($entity_type, $entity) {
  if (is_numeric($entity)) {
    $entities = entity_load($entity_type, array(
      $entity,
    ));
    $entity = reset($entities);
  }
  elseif (is_string($entity)) {
    $entity = entity_create_stub_entity($entity_type, array(
      NULL,
      NULL,
      $entity,
    ));
  }
  $id = $this
    ->getHandlerId($entity_type, $entity);
  if (!isset($this->handlers[$entity_type][$id])) {
    $entity_info = entity_get_info($entity_type);
    $class = $entity_info['translation']['entity_translation']['class'];
    $handler = new $class($entity_type, $entity_info, $entity);
    $handler
      ->setFactory($this);
    $this->handlers[$entity_type][$id] = $handler;
  }
  $this->last = $this->handlers[$entity_type][$id];
  $this->lastByType[$entity_type] = $this->last;
  $this->last
    ->setEntity($entity);
  return $this->last;
}