You are here

globallink_entity.module in GlobalLink Connect for Drupal 7.6

GlobalLink entity translation module.

This module adds entity translation support with configuration options.

File

globallink_entity/globallink_entity.module
View source
<?php

/**
 * @file
 * GlobalLink entity translation module.
 *
 * This module adds entity translation support with configuration options.
 */

/*
 * Adds a validation handler to check for change in multilingual options.
 */
function globallink_entity_form_node_type_form_alter(&$form, $form_state) {
  array_unshift($form['#validate'], 'globallink_entity_form_node_type_form_validate');
}

/*
 * Validation to check if any active submission exists for this content type
 * only if there is a change in multilingual options.
 */
function globallink_entity_form_node_type_form_validate($form, &$form_state) {
  module_load_include('inc', 'globallink', 'globallink');
  $language_content_type = $form_state['values']['language_content_type'];
  if ($language_content_type != 2 && globallink_pending_submission_exists_for_content_type($form_state['values']['old_type'])) {
    form_set_error('language_content_type', t('Active submission exists for this content type in GlobalLink.'));
  }
}

/**
 * Implements hook_node_presave().
 */
function globallink_entity_node_presave($node) {
  if (isset($node->tpt_skip) && $node->tpt_skip == TRUE || !isset($node->type)) {
    return;
  }
  if (!entity_translation_node_supported_type($node->type)) {
    return;
  }
  if (!isset($node->original)) {
    return;
  }
  $handler = entity_translation_get_handler('node', $node);
  $config_fields = globallink_get_translatable_fields($node->type, $node->type);
  $orig = $node->original;
  $lang = $handler
    ->getFormLanguage();
  foreach ($config_fields as $field) {
    if ($field->translatable != 1) {
      continue;
    }
    $field_name = $field->field_name;
    switch ($field_name) {
      case 'title':
        if ($orig->title != $node->title) {
          globallink_update_change_detection_entity($node, $lang);
          break 2;
        }
        break 1;
      case 'metatags':
        continue 2;
    }
    $field_info = field_info_field($field_name);
    switch ($field_info['type']) {
      case 'list_boolean':
      case 'image':
      case 'file':
      case 'taxonomy_term_reference':
      case 'field_collection':
        continue 2;
        break;
    }
    if (!isset($node->{$field_name}) || !isset($orig->{$field_name})) {
      globallink_update_change_detection_entity($node, $lang);
      break;
    }
    $o_arr = isset($orig->{$field_name}) ? $orig->{$field_name} : array();
    $n_arr = isset($node->{$field_name}) ? $node->{$field_name} : array();
    if (empty($o_arr) || empty($n_arr) || count($o_arr) != count($n_arr)) {
      globallink_update_change_detection_entity($node, $lang);
      continue;
    }
    if (!is_array($o_arr) || !is_array($n_arr)) {
      if ($o_arr != $n_arr) {
        globallink_update_change_detection_entity($node, $lang);
        break;
      }
      continue;
    }
    if (empty($o_arr[$lang]) && empty($n_arr[$lang]) && isset($o_arr[LANGUAGE_NONE]) && isset($n_arr[LANGUAGE_NONE])) {
      $lang = LANGUAGE_NONE;
    }
    if (isset($o_arr[$lang]) && isset($n_arr[$lang]) && count($o_arr[$lang]) != count($n_arr[$lang])) {
      globallink_update_change_detection_entity($node, $lang);
      break;
    }
    if (isset($o_arr[$lang]) && empty($n_arr[$lang]) || empty($o_arr[$lang]) && isset($n_arr[$lang])) {
      globallink_update_change_detection_entity($node, $lang);
      break;
    }
    if (count($o_arr[$lang]) != count($n_arr[$lang])) {
      globallink_update_change_detection_entity($node, $lang);
      break;
    }
    foreach ($o_arr[$lang] as $delta => $n_field) {
      if (isset($n_arr[$lang][$delta]) && isset($n_arr[$lang][$delta]['value'])) {
        if ($n_field['value'] != $n_arr[$lang][$delta]['value']) {
          globallink_update_change_detection_entity($node, $lang);
          break 2;
        }
      }
      else {
        globallink_update_change_detection_entity($node, $lang);
        break 2;
      }
    }
  }
}

/**
 * Updates change detection entity with new node ID and language.
 *
 * @param object $node
 *   The node object.
 * @param string $lang
 *   The target language.
 */
function globallink_update_change_detection_entity($node, $lang) {
  module_load_include('inc', 'globallink', 'globallink');
  $tpt_locale_code = globallink_get_locale_code($lang);
  db_update('globallink_core_entity')
    ->fields(array(
    'last_modified' => REQUEST_TIME,
    'changed' => 1,
  ))
    ->condition('nid', $node->nid, '=')
    ->condition('source', $tpt_locale_code, '=')
    ->execute();
}

/**
 * Gets entity rows that have been sent for translation.
 *
 * @param string $nid
 *   The entity node ID.
 * @param string $source
 *   The target language of the entity.
 *
 * @return array
 *   Array of entity rows that have been sent for translation.
 */
function get_tpt_entity_sent_rows($nid, $source) {
  module_load_include('inc', 'globallink', 'globallink');
  $arr = array();
  $query = db_select('globallink_core_entity', 'tc')
    ->fields('tc')
    ->condition('nid', $nid, '=')
    ->condition('status', array(
    'Sent for Translations',
    'Error',
    'Cancelled',
  ), 'IN')
    ->condition('source', globallink_get_locale_code($source), '=');
  $result = $query
    ->execute();
  foreach ($result as $row) {
    $arr[] = globallink_get_drupal_locale_code($row->target);
  }
  return $arr;
}

/**
 * Implements hook_menu().
 */
function globallink_entity_menu() {
  $items = array();
  $items['admin/globallink-translations/dashboard/entity'] = array(
    'title' => ' Entity ',
    'page callback' => 'globallink_entity_dashboard',
    'access callback' => 'globallink_access_callback_any',
    'type' => MENU_LOCAL_TASK,
    'file' => 'globallink_entity_send.inc',
    'page arguments' => array(
      'entity',
    ),
  );
  $items['admin/globallink-translations/activeSubmissions/entity'] = array(
    'title' => ' Entity ',
    'page callback' => 'globallink_entity_active_submissions',
    'access callback' => 'globallink_access_callback_any',
    'file' => 'globallink_entity_active_submissions.inc',
    'type' => MENU_LOCAL_TASK,
    'page arguments' => array(
      'entity',
    ),
  );
  $items['admin/globallink-translations/receiveTranslation/entity'] = array(
    'title' => ' Entity ',
    'page callback' => 'globallink_entity_receive_submissions',
    'access callback' => 'globallink_access_callback_any',
    'type' => MENU_LOCAL_TASK,
    'file' => 'globallink_entity_receive.inc',
    'page arguments' => array(
      'entity',
    ),
  );
  $items['admin/globallink-translations/receiveTranslation/entity/preview'] = array(
    'title' => 'Preview Translation',
    'page callback' => 'globallink_entity_preview_translated_content',
    'file' => 'globallink_entity_receive.inc',
    'access arguments' => array(
      TPT_ROLE_MANAGE_TRANSLATIONS,
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

Functions

Namesort descending Description
get_tpt_entity_sent_rows Gets entity rows that have been sent for translation.
globallink_entity_form_node_type_form_alter
globallink_entity_form_node_type_form_validate
globallink_entity_menu Implements hook_menu().
globallink_entity_node_presave Implements hook_node_presave().
globallink_update_change_detection_entity Updates change detection entity with new node ID and language.