You are here

class TMGMTEntitySourcePluginController in Translation Management Tool 7

@file Provides the Entity source controller.

Hierarchy

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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TMGMTDefaultSourcePluginController::getItemTypeLabel public function Returns the label of a source item type. Overrides TMGMTSourcePluginControllerInterface::getItemTypeLabel
TMGMTDefaultSourcePluginController::getItemTypes public function Returns an array of translatable source item types. Overrides TMGMTSourcePluginControllerInterface::getItemTypes
TMGMTEntitySourcePluginController::getData public function Returns the data from the fields as a structure that can be processed by the Translation Management system. Overrides TMGMTSourcePluginControllerInterface::getData
TMGMTEntitySourcePluginController::getExistingLangCodes public function Gets existing translation language codes of the job item source. Overrides TMGMTDefaultSourcePluginController::getExistingLangCodes
TMGMTEntitySourcePluginController::getLabel public function Return a title for this job item. Overrides TMGMTDefaultSourcePluginController::getLabel
TMGMTEntitySourcePluginController::getSourceLangCode public function Gets language code of the job item source. Overrides TMGMTSourcePluginControllerInterface::getSourceLangCode
TMGMTEntitySourcePluginController::getType public function Returns the type of a job item. Overrides TMGMTDefaultSourcePluginController::getType
TMGMTEntitySourcePluginController::getUri public function Returns the Uri for this job item. Overrides TMGMTDefaultSourcePluginController::getUri
TMGMTEntitySourcePluginController::saveTranslation public function Saves a translation. Overrides TMGMTSourcePluginControllerInterface::saveTranslation
TMGMTPluginBase::$pluginInfo protected property
TMGMTPluginBase::$pluginType protected property
TMGMTPluginBase::pluginInfo public function Returns the info of the type of the plugin. Overrides TMGMTPluginBaseInterface::pluginInfo
TMGMTPluginBase::pluginType public function Returns the type of the plugin. Overrides TMGMTPluginBaseInterface::pluginType
TMGMTPluginBase::__construct public function Constructor. Overrides TMGMTPluginBaseInterface::__construct