You are here

function tmgmt_entity_tmgmt_source_suggestions in Translation Management Tool 7

Implements hook_tmgmt_source_suggestions()

File

sources/entity/tmgmt_entity.module, line 248
Source plugin for the Translation Management system that handles entities.

Code

function tmgmt_entity_tmgmt_source_suggestions(array $items, TMGMTJob $job) {
  $suggestions = array();

  // Get all translatable entity types.
  $entity_types = array_filter(variable_get('entity_translation_entity_types', array()));
  foreach ($items as $item) {
    if ($item instanceof TMGMTJobItem && $item->plugin == 'entity' || $item->plugin == 'node') {

      // Load the entity and extract the bundle name to get all fields from the
      // current entity.
      $entity = entity_load_single($item->item_type, $item->item_id);
      list(, , $bundle) = entity_extract_ids($item->item_type, $entity);
      $field_instances = field_info_instances($item->item_type, $bundle);

      // Loop over all fields, check if they are NOT translatable. Only if a
      // field is not translatable we may suggest a referenced entity. If so,
      // check for a supported field type (image and file currently here).
      foreach ($field_instances as $instance) {
        $field = field_info_field($instance['field_name']);
        $field_type = $field['type'];
        $field_name = $field['field_name'];
        switch ($field_type) {
          case 'file':
          case 'image':

            // 'File' (and images) must be translatable entity types.
            // Other files we not suggest here. Get all field items from the
            // current field and suggest them as translatable.
            if (isset($entity_types['file']) && ($field_items = field_get_items($item->item_type, $entity, $field_name))) {

              // Add all files as a suggestion.
              foreach ($field_items as $field_item) {
                $file_entity = entity_load_single('file', $field_item['fid']);

                // Check if there is already a translation available for this
                // file. If so, just continue with the next file.
                $handler = entity_translation_get_handler('file', $file_entity);
                if ($handler instanceof EntityTranslationHandlerInterface) {
                  $translations = $handler
                    ->getTranslations();
                  if (isset($translations->data[$job->target_language])) {
                    continue;
                  }
                }

                // Add the translation as a suggestion.
                $suggestions[] = array(
                  'job_item' => tmgmt_job_item_create('entity', 'file', $file_entity->fid),
                  'reason' => t('Field @label', array(
                    '@label' => $instance['label'],
                  )),
                  'from_item' => $item->tjiid,
                );
              }
            }
            break;
          case 'entityreference':
            $target_type = $field['settings']['target_type'];

            // Make sure only tranlatable entity types are suggested.
            if (isset($entity_types[$target_type]) && ($field_items = field_get_items($item->item_type, $entity, $field_name))) {

              // Add all referenced entities as suggestion.
              foreach ($field_items as $field_item) {
                $ref_entity = entity_load_single($target_type, $field_item['target_id']);

                // Check if field refer to a missing entity.
                if (!$ref_entity) {
                  continue;
                }

                // Check if entity is language neutral and cannot be translated.
                if (entity_language($target_type, $ref_entity) == LANGUAGE_NONE) {
                  continue;
                }

                // Check if there is already a translation available for this
                // entity. If so, just continue with the next one.
                $handler = entity_translation_get_handler($target_type, $ref_entity);
                if ($handler instanceof EntityTranslationHandlerInterface) {
                  $translations = $handler
                    ->getTranslations();
                  if (isset($translations->data[$job->target_language])) {
                    continue;
                  }
                }

                // Add suggestion.
                $suggestions[] = array(
                  'job_item' => tmgmt_job_item_create('entity', $target_type, $field_item['target_id']),
                  'reason' => t('Field @label', array(
                    '@label' => $instance['label'],
                  )),
                  'from_item' => $item->tjiid,
                );
              }
            }
            break;
        }
      }
    }
  }
  return $suggestions;
}