class TMGMTEntitySourcePluginController in Translation Management Tool 7
@file Provides the Entity source controller.
Hierarchy
- class \TMGMTPluginBase implements TMGMTPluginBaseInterface
- class \TMGMTDefaultSourcePluginController implements TMGMTSourcePluginControllerInterface
Expanded class hierarchy of TMGMTEntitySourcePluginController
1 string reference to 'TMGMTEntitySourcePluginController'
- tmgmt_entity_tmgmt_source_plugin_info in sources/
entity/ tmgmt_entity.module - Implements hook_tmgmt_source_plugin_info().
File
- sources/
entity/ tmgmt_entity.plugin.inc, line 8 - Provides the Entity source controller.
View source
class TMGMTEntitySourcePluginController extends TMGMTDefaultSourcePluginController {
public function getLabel(TMGMTJobItem $job_item) {
if ($entity = entity_load_single($job_item->item_type, $job_item->item_id)) {
return entity_label($job_item->item_type, $entity);
}
}
public function getUri(TMGMTJobItem $job_item) {
if ($entity = entity_load_single($job_item->item_type, $job_item->item_id)) {
return entity_uri($job_item->item_type, $entity);
}
}
/**
* {@inheritdoc}
*
* Returns the data from the fields as a structure that can be processed by
* the Translation Management system.
*/
public function getData(TMGMTJobItem $job_item) {
$entity = entity_load_single($job_item->item_type, $job_item->item_id);
if (!$entity) {
throw new TMGMTException(t('Unable to load entity %type with id %id', array(
'%type' => $job_item->item_type,
$job_item->item_id,
)));
}
if (entity_language($job_item->item_type, $entity) == LANGUAGE_NONE) {
throw new TMGMTException(t('Entity %entity could not be translated because it is language neutral', array(
'%entity' => entity_label($job_item->item_type, $entity),
)));
}
return tmgmt_field_get_source_data($job_item->item_type, $entity, $job_item
->getJob()->source_language, TRUE);
}
/**
* {@inheritdoc}
*/
public function saveTranslation(TMGMTJobItem $job_item) {
$entity = entity_load_single($job_item->item_type, $job_item->item_id);
$job = tmgmt_job_load($job_item->tjid);
tmgmt_field_populate_entity($job_item->item_type, $entity, $job->target_language, $job_item
->getData());
// Change the active language of the entity to the target language.
$handler = entity_translation_get_handler($job_item->item_type, $entity);
$handler
->setFormLanguage($job_item
->getJob()->target_language);
entity_save($job_item->item_type, $entity);
$translation = array(
// @todo Improve hardcoded values.
'translate' => 0,
'status' => TRUE,
'language' => $job_item
->getJob()->target_language,
'source' => $job_item
->getJob()->source_language,
);
$handler
->setTranslation($translation);
$handler
->saveTranslations();
$job_item
->accepted();
}
/**
* {@inheritdoc}
*/
public function getType(TMGMTJobItem $job_item) {
if ($entity = entity_load_single($job_item->item_type, $job_item->item_id)) {
$bundles = tmgmt_entity_get_translatable_bundles($job_item->item_type);
$info = entity_get_info($job_item->item_type);
list(, , $bundle) = entity_extract_ids($job_item->item_type, $entity);
// Display entity type and label if we have one and the bundle isn't
// the same as the entity type.
if (isset($bundles[$bundle]) && $bundle != $job_item->item_type) {
return t('@type (@bundle)', array(
'@type' => $info['label'],
'@bundle' => $bundles[$bundle],
));
}
elseif (isset($info['label'])) {
return $info['label'];
}
return parent::getType($job_item);
}
}
/**
* {@inheritdoc}
*/
public function getSourceLangCode(TMGMTJobItem $job_item) {
$entity = entity_load_single($job_item->item_type, $job_item->item_id);
return isset($entity->translations->original) ? $entity->translations->original : NULL;
}
/**
* {@inheritdoc}
*/
public function getExistingLangCodes(TMGMTJobItem $job_item) {
if ($entity = entity_load_single($job_item->item_type, $job_item->item_id)) {
$entity_info = entity_get_info($job_item->item_type);
if (isset($entity_info['entity keys']['translations'])) {
$translations_key = $entity_info['entity keys']['translations'];
return array_keys($entity->{$translations_key}->data);
}
}
return array();
}
}