View source
<?php
module_load_include('inc', 'entity_translation', 'entity_translation.node');
module_load_include('inc', 'entity_translation', 'entity_translation.taxonomy');
define('ENTITY_TRANSLATION_LANGUAGE_DEFAULT', 'xx-et-default');
define('ENTITY_TRANSLATION_LANGUAGE_CURRENT', 'xx-et-current');
define('ENTITY_TRANSLATION_LANGUAGE_AUTHOR', 'xx-et-author');
define('I18N_MODE_ENTITY_TRANSLATION', 32768);
function entity_translation_hook_info() {
$hooks['entity_translation_insert'] = array(
'group' => 'entity_translation',
);
$hooks['entity_translation_update'] = array(
'group' => 'entity_translation',
);
$hooks['entity_translation_delete'] = array(
'group' => 'entity_translation',
);
return $hooks;
}
function entity_translation_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
case 'menu_alter':
case 'entity_info_alter':
$group = $implementations['entity_translation'];
unset($implementations['entity_translation']);
$implementations['entity_translation'] = $group;
break;
}
}
function entity_translation_language_types_info_alter(array &$language_types) {
unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
}
function entity_translation_entity_info() {
$info = array();
$info['node'] = array(
'translation' => array(
'entity_translation' => array(
'class' => 'EntityTranslationNodeHandler',
'access callback' => 'entity_translation_node_tab_access',
'access arguments' => array(
1,
),
'admin theme' => variable_get('node_admin_theme'),
'bundle callback' => 'entity_translation_node_supported_type',
'default settings' => array(
'default_language' => LANGUAGE_NONE,
'hide_language_selector' => FALSE,
),
),
),
);
if (module_exists('comment')) {
$info['comment'] = array(
'translation' => array(
'entity_translation' => array(
'class' => 'EntityTranslationCommentHandler',
'admin theme' => FALSE,
'bundle callback' => 'entity_translation_comment_supported_type',
'default settings' => array(
'default_language' => ENTITY_TRANSLATION_LANGUAGE_CURRENT,
'hide_language_selector' => TRUE,
),
),
),
);
}
if (module_exists('taxonomy')) {
$info['taxonomy_term'] = array(
'translation' => array(
'entity_translation' => array(
'class' => 'EntityTranslationTaxonomyTermHandler',
'access callback' => 'entity_translation_taxonomy_term_tab_access',
'access arguments' => array(
1,
),
'base path' => 'taxonomy/term/%taxonomy_term',
'edit form' => 'term',
'bundle callback' => 'entity_translation_taxonomy_term_enabled_vocabulary',
),
),
);
}
$info['user'] = array(
'translation' => array(
'entity_translation' => array(
'class' => 'EntityTranslationUserHandler',
'skip original values access' => TRUE,
'skip shared fields access' => TRUE,
),
),
);
return $info;
}
function _entity_translation_process_path_schemes($entity_type, &$et_info) {
$path_scheme_keys = array_flip(array(
'base path',
'view path',
'edit path',
'translate path',
'path wildcard',
'admin theme',
'edit tabs',
));
$default_scheme = array_intersect_key($et_info, $path_scheme_keys);
if (!empty($default_scheme)) {
$et_info['path schemes']['default'] = $default_scheme;
$et_info = array_diff_key($et_info, $path_scheme_keys);
}
if (empty($et_info['path schemes']['default']['base path'])) {
$et_info['path schemes']['default']['base path'] = "{$entity_type}/%{$entity_type}";
}
foreach ($et_info['path schemes'] as $delta => $scheme) {
if (!empty($scheme['base path'])) {
$view_path = $scheme['base path'];
$edit_path = $scheme['base path'] . '/edit';
$translate_path = $scheme['base path'] . '/translate';
$et_info['path schemes'][$delta] += array(
'view path' => $view_path,
'edit path' => $edit_path,
'translate path' => $translate_path,
);
}
$et_info['path schemes'][$delta] += array(
'admin theme' => TRUE,
'path wildcard' => "%{$entity_type}",
'edit tabs' => TRUE,
);
}
}
function entity_translation_entity_info_alter(&$entity_info) {
foreach ($entity_info as $entity_type => $info) {
if (!isset($entity_info[$entity_type]['translation']['entity_translation'])) {
$entity_info[$entity_type]['translation']['entity_translation'] = array();
}
$et_info =& $entity_info[$entity_type]['translation']['entity_translation'];
$et_info += array(
'class' => 'EntityTranslationDefaultHandler',
);
if (!isset($entity_info[$entity_type]['entity keys'])) {
$entity_info[$entity_type]['entity keys'] = array();
}
$entity_info[$entity_type]['entity keys'] += array(
'translations' => 'translations',
);
if (entity_translation_enabled($entity_type, NULL, TRUE)) {
$entity_info[$entity_type]['language callback'] = 'entity_translation_language';
_entity_translation_process_path_schemes($entity_type, $et_info);
$et_info += array(
'access callback' => 'entity_translation_tab_access',
'access arguments' => array(
$entity_type,
),
);
if (!isset($et_info['edit form']) || $et_info['edit form'] === TRUE) {
$et_info['edit form'] = $entity_type;
}
}
}
}
function entity_translation_menu() {
$items = array();
$items['admin/config/regional/entity_translation'] = array(
'title' => 'Entity translation',
'description' => 'Configure which entities can be translated and enable or disable language fallback.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'entity_translation_admin_form',
),
'access arguments' => array(
'administer entity translation',
),
'file' => 'entity_translation.admin.inc',
'module' => 'entity_translation',
);
$items['admin/config/regional/entity_translation/translatable/%'] = array(
'title' => 'Confirm change in translatability.',
'description' => 'Confirmation page for changing field translatability.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'entity_translation_translatable_form',
5,
),
'access arguments' => array(
'toggle field translatability',
),
'file' => 'entity_translation.admin.inc',
);
$items['entity_translation/taxonomy_term/autocomplete'] = array(
'title' => 'Entity translation autocomplete',
'page callback' => 'entity_translation_taxonomy_term_autocomplete',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function _entity_translation_validate_path_schemes(&$schemes, $entity_type_label, $items = FALSE, $warnings = FALSE) {
$paths =& drupal_static(__FUNCTION__);
static $regex = '|%[^/]+|';
if (!empty($items)) {
$paths = array();
foreach ($items as $path => $item) {
$stripped_path = preg_replace($regex, '%', $path);
$paths[$stripped_path] = $path;
}
}
if (empty($schemes)) {
return;
}
if (empty($paths)) {
throw new Exception('The Entity Translation path scheme validation function has not been initialized properly.');
}
foreach ($schemes as $delta => &$scheme) {
if (empty($scheme['path wildcard'])) {
if ($warnings) {
$t_args = array(
'%scheme' => $delta,
'%entity_type' => $entity_type_label,
);
watchdog('entity_translation', 'Entity Translation path scheme %scheme for entities of type %entity_type does not declare a path wildcard.', $t_args);
}
unset($schemes[$delta]);
continue;
}
$wildcard = $scheme['path wildcard'];
$validate_keys = array(
'base path' => FALSE,
'view path' => FALSE,
'edit path' => FALSE,
'translate path' => TRUE,
);
foreach ($validate_keys as $key => $check_parent) {
if (isset($scheme[$key])) {
$path = $scheme[$key];
$parts = explode('/', $path);
$scheme[$key . ' parts'] = $parts;
if (!in_array($wildcard, $parts)) {
if ($warnings) {
$t_args = array(
'%path_key' => $key,
'%entity_type' => $entity_type_label,
'%wildcard' => $wildcard,
'%path' => $path,
);
drupal_set_message(t('Invalid %path_key defined for entities of type %entity_type: entity wildcard %wildcard not found in %path.', $t_args), 'warning');
}
unset($scheme[$key]);
continue;
}
$trailing_path_element = FALSE;
if ($check_parent) {
$trailing_path_element = array_pop($parts);
$path = implode('/', $parts);
}
$stripped_path = preg_replace($regex, '%', $path);
if (!isset($paths[$stripped_path])) {
if ($warnings) {
$t_args = array(
'%path_key' => $key,
'%entity_type' => $entity_type_label,
'%path' => $path,
);
$msg = $check_parent ? t('Invalid %path_key defined for entities of type %entity_type: parent menu item not found for %path', $t_args) : t('Invalid %path_key defined for entities of type %entity_type: matching menu item not found for %path', $t_args);
drupal_set_message($msg, 'warning');
}
unset($scheme[$key]);
}
else {
$real_path = $paths[$stripped_path];
$real_parts = explode('/', $real_path);
if ($trailing_path_element) {
$real_path .= '/' . $trailing_path_element;
$real_parts[] = $trailing_path_element;
}
$scheme['real ' . $key] = $real_path;
$scheme['real ' . $key . ' parts'] = $real_parts;
}
}
}
}
}
function entity_translation_menu_alter(&$items) {
$backup = array();
$_null = NULL;
_entity_translation_validate_path_schemes($_null, FALSE, $items);
foreach (entity_get_info() as $entity_type => $info) {
if (entity_translation_enabled($entity_type)) {
$et_info = $info['translation']['entity_translation'];
$translate_ui_attached = FALSE;
_entity_translation_validate_path_schemes($et_info['path schemes'], $info['label'], FALSE, TRUE);
foreach ($et_info['path schemes'] as $scheme) {
$translate_item = NULL;
$edit_item = NULL;
if (isset($scheme['translate path'])) {
$translate_path = $scheme['translate path'];
$keys = array(
'theme callback',
'theme arguments',
'access callback',
'access arguments',
'load arguments',
);
$item = array_intersect_key($info['translation']['entity_translation'], drupal_map_assoc($keys));
$item += array(
'file' => 'entity_translation.admin.inc',
'module' => 'entity_translation',
);
$entity_position = array_search($scheme['path wildcard'], $scheme['translate path parts']);
if ($item['access callback'] == 'entity_translation_tab_access') {
$item['access arguments'][] = $entity_position;
}
if (isset($items[$translate_path])) {
$backup[$entity_type] = $items[$translate_path];
}
$items[$translate_path] = array(
'title' => 'Translate',
'page callback' => 'entity_translation_overview',
'page arguments' => array(
$entity_type,
$entity_position,
),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
) + $item;
$language_position = count($scheme['translate path parts']) + 1;
$items["{$translate_path}/delete/%entity_translation_language"] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'entity_translation_delete_confirm',
$entity_type,
$entity_position,
$language_position,
),
) + $item;
$translate_item =& $items[$translate_path];
}
if (isset($scheme['edit path'])) {
$real_edit_path_parts = $scheme['real edit path parts'];
do {
$edit_item =& $items[implode('/', $real_edit_path_parts)];
array_pop($real_edit_path_parts);
} while (!empty($edit_item['type']) && $edit_item['type'] == MENU_DEFAULT_LOCAL_TASK);
$edit_path = $scheme['edit path'];
$edit_path_parts = $scheme['edit path parts'];
$entity_position = array_search($scheme['path wildcard'], $edit_path_parts);
$original_item = $edit_item + array(
'page arguments' => array(),
'access arguments' => array(),
);
$args = array(
$entity_type,
$entity_position,
FALSE,
$original_item,
);
$edit_item['page callback'] = 'entity_translation_edit_page';
$edit_item['page arguments'] = array_merge($args, $original_item['page arguments']);
$edit_item['access callback'] = 'entity_translation_edit_access';
$edit_item['access arguments'] = array_merge($args, $original_item['access arguments']);
if ($scheme['edit tabs'] !== FALSE) {
$translation_position = count($edit_path_parts);
$args = array(
$entity_type,
$entity_position,
$translation_position,
$original_item,
);
$items["{$edit_path}/%entity_translation_language"] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title callback' => 'entity_translation_edit_title',
'title arguments' => array(
$translation_position,
),
'page callback' => 'entity_translation_edit_page',
'page arguments' => array_merge($args, $original_item['page arguments']),
'access callback' => 'entity_translation_edit_access',
'access arguments' => array_merge($args, $original_item['access arguments']),
) + $original_item;
}
$add_path = "{$edit_path}/add/%entity_translation_language/%entity_translation_language";
$source_position = count($edit_path_parts) + 1;
$target_position = count($edit_path_parts) + 2;
$args = array(
$entity_type,
$entity_position,
$source_position,
$target_position,
$original_item,
);
$items[$add_path] = array(
'title callback' => 'Add translation',
'page callback' => 'entity_translation_add_page',
'page arguments' => array_merge($args, $original_item['page arguments']),
'type' => MENU_LOCAL_TASK,
'access callback' => 'entity_translation_add_access',
'access arguments' => array_merge($args, $original_item['access arguments']),
) + $original_item;
}
if ($translate_item && $edit_item && isset($edit_item['weight'])) {
$translate_item['weight'] = $edit_item['weight'] + 1;
}
$translate_ui_attached = $translate_ui_attached || $translate_item && $edit_item;
unset($translate_item, $edit_item);
}
if ($translate_ui_attached == FALSE) {
watchdog('entity_translation', 'The entities of type %entity_type do not define a valid path scheme: it will not be possible to translate them.', array(
'%entity_type' => $info['label'],
));
}
$function = 'entity_translation_' . $entity_type . '_menu_alter';
if (function_exists($function)) {
$function($items, $backup);
}
}
}
drupal_static_reset('_entity_translation_validate_path_schemes');
}
function entity_translation_edit_title($langcode) {
$languages = entity_translation_languages();
return isset($languages[$langcode]) ? t($languages[$langcode]->name) : '';
}
function entity_translation_edit_page() {
$args = func_get_args();
$entity_type = array_shift($args);
$entity = array_shift($args);
$langcode = array_shift($args);
$edit_form_item = array_shift($args);
$handler = entity_translation_get_handler($entity_type, $entity);
$handler
->initPathScheme();
$langcode = entity_translation_get_existing_language($entity_type, $entity, $langcode);
$handler
->setActiveLanguage($langcode);
return _entity_translation_callback($edit_form_item['page callback'], $args, $edit_form_item);
}
function entity_translation_edit_access() {
$args = func_get_args();
$entity_type = array_shift($args);
$entity = array_shift($args);
$langcode = array_shift($args);
$edit_form_item = array_shift($args);
$access_callback = isset($edit_form_item['access callback']) ? $edit_form_item['access callback'] : 'user_access';
$handler = entity_translation_get_handler($entity_type, $entity);
if (empty($handler)) {
return _entity_translation_callback($access_callback, $args, $edit_form_item);
}
$translations = $handler
->getTranslations();
$langcode = entity_translation_get_existing_language($entity_type, $entity, $langcode);
if (!$handler
->getTranslationAccess($langcode)) {
return FALSE;
}
if (empty($translations->data) || isset($translations->data[$langcode])) {
$enabled_languages = entity_translation_languages($entity_type, $entity);
if (isset($enabled_languages[$langcode]) || $langcode == LANGUAGE_NONE) {
return _entity_translation_callback($access_callback, $args, $edit_form_item);
}
}
return FALSE;
}
function entity_translation_form_language($langcode, $handler) {
return entity_translation_get_existing_language($handler
->getEntity(), $handler
->getEntityType(), $langcode);
}
function entity_translation_get_existing_language($entity_type, $entity, $langcode = NULL) {
$handler = entity_translation_get_handler($entity_type, $entity);
if (empty($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
$translations = $handler
->getTranslations();
$fallback = drupal_multilingual() ? language_fallback_get_candidates() : array(
LANGUAGE_NONE,
);
while (!empty($langcode) && !isset($translations->data[$langcode])) {
$langcode = array_shift($fallback);
}
return !empty($langcode) ? $langcode : $handler
->getLanguage();
}
function entity_translation_add_access() {
$args = func_get_args();
$entity_type = array_shift($args);
$entity = array_shift($args);
$source = array_shift($args);
$langcode = array_shift($args);
$handler = entity_translation_get_handler($entity_type, $entity);
$translations = $handler
->getTranslations();
if (!isset($translations->data[$langcode]) && $langcode != $source) {
$enabled_languages = entity_translation_languages($entity_type, $entity);
if (isset($enabled_languages[$langcode])) {
$edit_form_item = array_shift($args);
$access_callback = isset($edit_form_item['access callback']) ? $edit_form_item['access callback'] : 'user_access';
return _entity_translation_callback($access_callback, $args, $edit_form_item);
}
}
return FALSE;
}
function entity_translation_add_page() {
$args = func_get_args();
$entity_type = array_shift($args);
$entity = array_shift($args);
$source = array_shift($args);
$langcode = array_shift($args);
$edit_form_item = array_shift($args);
$handler = entity_translation_get_handler($entity_type, $entity);
$handler
->initPathScheme();
$handler
->setActiveLanguage($langcode);
$handler
->setSourceLanguage($source);
return _entity_translation_callback($edit_form_item['page callback'], $args, $edit_form_item);
}
function _entity_translation_callback($callback, $args, $info = array()) {
if (isset($info['file'])) {
$path = isset($info['file path']) ? $info['file path'] : drupal_get_path('module', $info['module']);
include_once DRUPAL_ROOT . '/' . $path . '/' . $info['file'];
}
return call_user_func_array($callback, $args);
}
function entity_translation_admin_paths() {
$paths = array();
foreach (entity_get_info() as $entity_type => $info) {
if (isset($info['translation']['entity_translation']['path schemes']) && entity_translation_enabled($entity_type, NULL, TRUE)) {
foreach ($info['translation']['entity_translation']['path schemes'] as $scheme) {
if (!empty($scheme['admin theme'])) {
if (isset($scheme['translate path'])) {
$translate_path = preg_replace('|%[^/]*|', '*', $scheme['translate path']);
$paths[$translate_path] = TRUE;
$paths["{$translate_path}/*"] = TRUE;
}
if (isset($scheme['edit path'])) {
$edit_path = preg_replace('|%[^/]*|', '*', $scheme['edit path']);
$paths["{$edit_path}/*"] = TRUE;
}
}
}
}
}
return $paths;
}
function entity_translation_tab_access($entity_type, $entity) {
if (drupal_multilingual() && (user_access('translate any entity') || user_access("translate {$entity_type} entities"))) {
$handler = entity_translation_get_handler($entity_type, $entity);
$entity = $handler
->getEntity();
$enabled = entity_translation_enabled($entity_type, $entity);
return $enabled && $handler
->getLanguage() != LANGUAGE_NONE;
}
return FALSE;
}
function entity_translation_language_load($langcode, $entity_type = NULL, $entity = NULL) {
$enabled_languages = entity_translation_languages($entity_type, $entity);
return isset($enabled_languages[$langcode]) ? $langcode : FALSE;
}
function entity_translation_menu_entity_load($entity_id, $entity_type) {
$entities = entity_load($entity_type, array(
$entity_id,
));
return $entities[$entity_id];
}
function entity_translation_permission() {
$permission = array(
'administer entity translation' => array(
'title' => t('Administer entity translation'),
'description' => t('Select which entities can be translated.'),
),
'toggle field translatability' => array(
'title' => t('Toggle field translatability'),
'description' => t('Toggle translatability of fields performing a bulk update.'),
),
'translate any entity' => array(
'title' => t('Translate any entity'),
'description' => t('Translate field content for any fieldable entity.'),
),
);
$workflow = entity_translation_workflow_enabled();
if ($workflow) {
$permission += array(
'edit translation shared fields' => array(
'title' => t('Edit shared values'),
'description' => t('Edit values shared between translations on the entity form.'),
),
'edit original values' => array(
'title' => t('Edit original values'),
'description' => t('Access any entity form in the original language.'),
),
);
}
foreach (entity_get_info() as $entity_type => $info) {
if ($info['fieldable'] && entity_translation_enabled($entity_type)) {
$label = !empty($info['label']) ? t($info['label']) : $entity_type;
$permission["translate {$entity_type} entities"] = array(
'title' => t('Translate entities of type @type', array(
'@type' => $label,
)),
'description' => t('Translate field content for entities of type @type.', array(
'@type' => $label,
)),
);
if ($workflow) {
if (empty($info['translation']['entity_translation']['skip original values access'])) {
$permission["edit {$entity_type} original values"] = array(
'title' => t('Edit original values on entities of type @type', array(
'@type' => $label,
)),
'description' => t('Access the entity form in the original language for entities of type @type.', array(
'@type' => $label,
)),
);
}
if (empty($info['translation']['entity_translation']['skip shared fields access'])) {
$permission["edit {$entity_type} translation shared fields"] = array(
'title' => t('Edit @type shared values.', array(
'@type' => $label,
)),
'description' => t('Edit values shared between translations on @type forms.', array(
'@type' => $label,
)),
);
}
}
}
}
return $permission;
}
function entity_translation_workflow_enabled() {
return variable_get('entity_translation_workflow_enabled', FALSE);
}
function entity_translation_theme() {
return array(
'entity_translation_unavailable' => array(
'variables' => array(
'element' => NULL,
),
),
'entity_translation_language_tabs' => array(
'render element' => 'element',
),
'entity_translation_overview' => array(
'variables' => array(
'rows' => NULL,
'header' => NULL,
),
'file' => 'entity_translation.admin.inc',
),
'entity_translation_overview_outdated' => array(
'variables' => array(
'message' => NULL,
),
'file' => 'entity_translation.admin.inc',
),
);
}
function entity_translation_entity_load($entities, $entity_type) {
if (entity_translation_enabled($entity_type)) {
EntityTranslationDefaultHandler::loadMultiple($entity_type, $entities);
}
}
function entity_translation_field_extra_fields() {
$extra = array();
$enabled = variable_get('entity_translation_entity_types', array());
$info = entity_get_info();
foreach ($enabled as $entity_type) {
if (entity_translation_enabled($entity_type)) {
$bundles = !empty($info[$entity_type]['bundles']) ? array_keys($info[$entity_type]['bundles']) : array(
$entity_type,
);
foreach ($bundles as $bundle) {
$settings = entity_translation_settings($entity_type, $bundle);
if (empty($settings['hide_language_selector']) && entity_translation_enabled_bundle($entity_type, $bundle) && ($handler = entity_translation_get_handler($entity_type, $bundle))) {
$language_key = $handler
->getLanguageKey();
$extra[$entity_type][$bundle] = array(
'form' => array(
$language_key => array(
'label' => t('Language'),
'description' => t('Language selection'),
'weight' => 5,
),
),
);
}
}
}
}
return $extra;
}
function entity_translation_field_language_alter(&$display_language, $context) {
if (variable_get('locale_field_language_fallback', TRUE) && entity_translation_enabled($context['entity_type'])) {
$entity = $context['entity'];
$entity_type = $context['entity_type'];
$handler = entity_translation_get_handler($entity_type, $entity);
$translations = $handler
->getTranslations();
if (isset($translations->data[$context['language']]) && !entity_translation_access($entity_type, $translations->data[$context['language']])) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$instances = field_info_instances($entity_type, $bundle);
$entity = clone $entity;
foreach ($translations->data as $langcode => $translation) {
if ($langcode == $context['language'] || !entity_translation_access($entity_type, $translations->data[$langcode])) {
foreach ($instances as $instance) {
unset($entity->{$instance['field_name']}[$langcode]);
}
}
}
locale_field_language_fallback($display_language, $entity, $context['language']);
}
elseif (!field_has_translation_handler($entity_type, 'locale')) {
locale_field_language_fallback($display_language, $entity, $context['language']);
}
}
}
function entity_translation_field_attach_view_alter(&$output, $context) {
if (!variable_get('locale_field_language_fallback', TRUE) && entity_translation_enabled($context['entity_type'])) {
$handler = entity_translation_get_handler($context['entity_type'], $context['entity']);
$translations = $handler
->getTranslations();
$langcode = !empty($context['language']) ? $context['language'] : $GLOBALS['language_content']->language;
if (!empty($translations->data) && (!isset($translations->data[$langcode]) && !isset($translations->data[LANGUAGE_NONE]) || isset($translations->data[$langcode]) && !entity_translation_access($context['entity_type'], $translations->data[$langcode]))) {
$output['#entity'] = $context['entity'];
$output['#entity_type'] = $context['entity_type'];
$output['#view_mode'] = $context['view_mode'];
$output['#entity_translation_unavailable'] = theme('entity_translation_unavailable', array(
'element' => $output,
));
$output['#post_render']['entity_translation'] = 'entity_translation_unavailable';
}
}
}
function entity_translation_unavailable($children, $element) {
return $element['#entity_translation_unavailable'];
}
function theme_entity_translation_unavailable($variables) {
$element = $variables['element'];
$handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']);
$args = array(
'%language' => t($GLOBALS['language_content']->name),
'%label' => $handler
->getLabel(),
);
$message = t('%language translation unavailable for %label.', $args);
$classes = $element['#entity_type'] . ' ' . $element['#entity_type'] . '-' . $element['#view_mode'];
return "<div class=\"{$classes}\"><div class=\"messages warning\">{$message}</div></div>";
}
function entity_translation_field_info_alter(&$info) {
$columns = array(
'fid',
);
$supported_types = array(
'file' => $columns,
'image' => $columns,
);
foreach ($info as $field_type => &$field_type_info) {
if (!isset($field_type_info['settings'])) {
$field_type_info['settings'] = array();
}
$field_type_info['settings'] += array(
'entity_translation_sync' => isset($supported_types[$field_type]) ? $supported_types[$field_type] : FALSE,
);
if (!isset($field_type_info['instance_settings'])) {
$field_type_info['instance_settings'] = array();
}
$field_type_info['instance_settings'] += array(
'entity_translation_sync' => FALSE,
);
}
}
function entity_translation_field_attach_presave($entity_type, $entity) {
if (entity_translation_enabled($entity_type, $entity)) {
entity_translation_sync($entity_type, $entity);
}
}
function entity_translation_sync($entity_type, $entity) {
$handler = entity_translation_get_handler($entity_type, $entity, TRUE);
$translations = $handler
->getTranslations();
$original_langcode = $handler
->getSourceLanguage();
if ($handler
->isNewEntity() || count($translations->data) < 2 && !$original_langcode) {
return;
}
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
$instances = field_info_instances($entity_type, $bundle);
$entity_unchanged = isset($entity->original) ? $entity->original : entity_load_unchanged($entity_type, $id);
$langcode = $handler
->getLanguage();
$handler
->setEntity($entity_unchanged);
if ($langcode != $handler
->getLanguage()) {
return;
}
foreach ($instances as $field_name => $instance) {
$field = field_info_field($field_name);
if (!empty($entity->{$field_name}) && !empty($instance['settings']['entity_translation_sync']) && field_is_translatable($entity_type, $field)) {
$columns = $field['settings']['entity_translation_sync'];
$change_map = array();
$source_langcode = entity_language($entity_type, $entity);
$source_items = $entity->{$field_name}[$source_langcode];
$langcode = $original_langcode ? $original_langcode : $source_langcode;
$unchanged_items = !empty($entity_unchanged->{$field_name}[$langcode]) ? $entity_unchanged->{$field_name}[$langcode] : array();
$total = max(array(
count($source_items),
count($unchanged_items),
));
for ($delta = 0; $delta < $total; $delta++) {
foreach ($columns as $column) {
if (isset($unchanged_items[$delta][$column])) {
$value = $unchanged_items[$delta][$column];
$change_map[$column][$value]['old'] = $delta;
}
if (isset($source_items[$delta][$column])) {
$value = $source_items[$delta][$column];
$change_map[$column][$value]['new'] = $delta;
}
}
}
$field_values = $entity->{$field_name};
$entity->{$field_name} = array(
$source_langcode => $source_items,
) + array_diff_key($entity->{$field_name}, $translations->data);
foreach ($translations->data as $langcode => $translation) {
if ($langcode != $source_langcode) {
for ($delta = 0; $delta < $total; $delta++) {
$created = TRUE;
$removed = TRUE;
foreach ($columns as $column) {
if (isset($source_items[$delta][$column])) {
$value = $source_items[$delta][$column];
$created = $created && !isset($change_map[$column][$value]['old']);
$removed = $removed && !isset($change_map[$column][$value]['new']);
}
}
if ($removed) {
if (!isset($entity->{$field_name}[$langcode])) {
$entity->{$field_name}[$langcode] = array();
}
continue;
}
elseif ($created) {
$entity->{$field_name}[$langcode][$delta] = $source_items[$delta];
}
elseif (!empty($change_map[$column][$value])) {
$old_delta = $change_map[$column][$value]['old'];
$new_delta = $change_map[$column][$value]['new'];
$items = isset($field_values[$langcode][$old_delta]) ? $field_values[$langcode][$old_delta] : $source_items[$new_delta];
$entity->{$field_name}[$langcode][$new_delta] = $items;
}
}
}
}
}
}
}
function entity_translation_field_attach_insert($entity_type, $entity) {
if (entity_translation_enabled($entity_type, $entity)) {
$handler = entity_translation_get_handler($entity_type, $entity);
$handler
->initTranslations();
$handler
->saveTranslations();
}
}
function entity_translation_field_attach_update($entity_type, $entity) {
if (entity_translation_enabled($entity_type, $entity)) {
$handler = entity_translation_get_handler($entity_type, $entity, TRUE);
$handler
->updateTranslations();
$handler
->saveTranslations();
}
}
function entity_translation_field_attach_delete($entity_type, $entity) {
if (entity_translation_enabled($entity_type, $entity)) {
$handler = entity_translation_get_handler($entity_type, $entity);
$handler
->removeTranslations();
$handler
->saveTranslations();
}
}
function entity_translation_field_attach_delete_revision($entity_type, $entity) {
if (entity_translation_enabled($entity_type, $entity)) {
$handler = entity_translation_get_handler($entity_type, $entity);
$handler
->removeRevisionTranslations();
$handler
->saveTranslations();
}
}
function entity_translation_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (empty($form['#entity_translation_source_form']) && entity_translation_enabled($entity_type, $bundle)) {
$handler = entity_translation_get_handler($entity_type, $entity);
$langcode = !empty($langcode) ? $langcode : $handler
->getLanguage();
$form_langcode = $handler
->getActiveLanguage();
$translations = $handler
->getTranslations();
$update_langcode = $form_langcode && $form_langcode != $langcode;
$source = $handler
->getSourceLanguage();
$new_translation = !isset($translations->data[$form_langcode]);
if ($update_langcode || $source && !isset($translations->data[$form_langcode]) && isset($translations->data[$source]) && empty($form_state['rebuild'])) {
foreach (field_info_instances($entity_type, $bundle) as $instance) {
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
if ($field['translatable'] && isset($form[$field_name])) {
$element =& $form[$field_name];
$element['#entity_type'] = $entity_type;
$element['#entity'] = $entity;
$element['#entity_id'] = $id;
$element['#field_name'] = $field_name;
$element['#source'] = $update_langcode ? $form_langcode : $source;
$element['#previous'] = NULL;
$element['#form_parents'] = $form['#parents'];
if ($update_langcode) {
$element['#previous'] = $element['#language'];
$element['#language'] = $form_langcode;
}
if (!isset($element['#process'])) {
$element['#process'] = array();
}
array_unshift($element['#process'], 'entity_translation_prepare_element');
}
}
}
if (!$handler
->isNewEntity() && ($new_translation || count($translations->data) > 1)) {
$shared_access = $handler
->getSharedFieldsAccess();
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
foreach (field_info_instances($entity_type, $bundle) as $instance) {
$field_name = $instance['field_name'];
if (isset($form[$field_name])) {
$field = field_info_field($field_name);
$form[$field_name]['#access'] = (!isset($form[$field_name]['#access']) || $form[$field_name]['#access']) && ($field['translatable'] || $shared_access);
$form[$field_name]['#multilingual'] = (bool) $field['translatable'];
}
}
}
if (module_exists('pathauto') && $handler
->getSourceLanguage()) {
$entity->path['pathauto'] = variable_get('entity_translation_pathauto_state_active_new_translation', TRUE);
}
}
}
function entity_translation_prepare_element($element, &$form_state) {
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast =& drupal_static(__FUNCTION__, array(
'source_forms' => array(),
'source_form_states' => array(),
));
}
$source_forms =& $drupal_static_fast['source_forms'];
$source_form_states =& $drupal_static_fast['source_form_states'];
$form = $form_state['complete form'];
$build_id = $form['#build_id'];
$source = $element['#source'];
$entity_type = $element['#entity_type'];
$id = $element['#entity_id'];
if (!isset($source_forms[$build_id][$source][$entity_type][$id])) {
$source_form = array(
'#entity_translation_source_form' => TRUE,
'#parents' => $element['#form_parents'],
);
$source_form_state = $form_state;
field_attach_form($entity_type, $element['#entity'], $source_form, $source_form_state, $source);
$source_forms[$build_id][$source][$entity_type][$id] =& $source_form;
$source_form_states[$build_id][$source][$entity_type][$id] =& $source_form_state;
}
$source_form =& $source_forms[$build_id][$source][$entity_type][$id];
$source_form_state = $source_form_states[$build_id][$source][$entity_type][$id];
$langcode = $element['#language'];
$field_name = $element['#field_name'];
if (isset($source_form[$field_name][$source])) {
$element[$langcode] = $source_form[$field_name][$source];
entity_translation_form_element_language_replace($element, $source, $langcode);
entity_translation_form_element_state_replace($element, $source_form[$field_name], $field_name, $source_form_state, $form_state);
unset($element[$element['#previous']]);
}
return $element;
}
function entity_translation_form_element_state_replace($element, $source_element, $field_name, $source_form_state, &$form_state) {
if (isset($source_element['#language'])) {
$source = $source_element['#language'];
foreach (element_children($element) as $key) {
if (isset($source_element[$key])) {
entity_translation_form_element_state_replace($element[$key], $source_element[$key], $key, $source_form_state, $form_state);
}
elseif (isset($source_element[$source])) {
entity_translation_form_element_state_replace($element[$key], $source_element[$source], $key, $source_form_state, $form_state);
}
}
if (isset($source_element[$source]['#field_parents'])) {
$source_parents = $source_element[$source]['#field_parents'];
$langcode = $element['#language'];
$parents = $element[$langcode]['#field_parents'];
$source_state = field_form_get_state($source_parents, $field_name, $source, $source_form_state);
drupal_alter('entity_translation_source_field_state', $source_state);
field_form_set_state($parents, $field_name, $langcode, $form_state, $source_state);
}
}
}
function entity_translation_form_element_language_replace(&$element, $source, $langcode) {
$element_children = element_children($element);
$element_properties = array_diff(array_keys($element), $element_children);
foreach ($element_children as $key) {
entity_translation_form_element_language_replace($element[$key], $source, $langcode);
}
foreach ($element_properties as $key) {
if ($key === '#language' && $element[$key] != LANGUAGE_NONE) {
$element[$key] = $langcode;
}
elseif ($key === '#parents' || $key === '#field_parents') {
foreach ($element[$key] as $delta => $value) {
if ($value === $source) {
$element[$key][$delta] = $langcode;
}
}
}
elseif ($key === '#limit_validation_errors') {
foreach ($element[$key] as $section => $section_value) {
foreach ($element[$key][$section] as $delta => $value) {
if ($value === $source) {
$element[$key][$section][$delta] = $langcode;
}
}
}
}
}
}
function entity_translation_element_translatability_clue($element) {
if (empty($element['#multilingual'])) {
_entity_translation_element_title_append($element, ' (' . t('all languages') . ')');
}
if (!isset($element['#attributes'])) {
$element['#attributes'] = array();
}
if (!isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = array();
}
$element['#attributes']['class'][] = 'entity-translation-' . (!empty($element['#multilingual']) ? 'field-translatable' : 'field-shared');
return $element;
}
function _entity_translation_element_add_callback(&$element, $type, $function, $prepend = TRUE) {
if (!isset($element[$type])) {
$element_type = isset($element['#type']) ? $element['#type'] : 'markup';
$element_info = element_info($element_type);
$element[$type] = isset($element_info[$type]) ? $element_info[$type] : array();
}
if ($prepend) {
array_unshift($element[$type], $function);
}
else {
$element[$type][] = $function;
}
}
function _entity_translation_element_title_append(&$element, $suffix) {
static $fapi_title_elements;
if (!isset($fapi_title_elements)) {
$fapi_title_elements = array_flip(array(
'checkbox',
'checkboxes',
'date',
'fieldset',
'file',
'item',
'password',
'password_confirm',
'radio',
'radios',
'select',
'text_format',
'textarea',
'textfield',
'weight',
));
}
if (isset($element['#type']) && isset($fapi_title_elements[$element['#type']]) && isset($element['#title'])) {
$element['#title'] .= $suffix;
}
elseif (isset($element['#title']) && isset($element['#cardinality']) && $element['#cardinality'] != 1) {
$element['#title'] .= $suffix;
}
elseif ($children = element_children($element)) {
foreach ($children as $delta) {
_entity_translation_element_title_append($element[$delta], $suffix);
}
}
elseif (isset($element['#title'])) {
$element['#title'] .= $suffix;
}
}
function entity_translation_form_alter(&$form, &$form_state) {
if ($info = entity_translation_edit_form_info($form, $form_state)) {
$handler = entity_translation_get_handler($info['entity type'], $info['entity']);
if (entity_translation_enabled($info['entity type'], $info['entity'])) {
if (!$handler
->isNewEntity()) {
$handler
->entityForm($form, $form_state);
$translations = $handler
->getTranslations();
$form_langcode = $handler
->getActiveLanguage();
if (!isset($translations->data[$form_langcode]) || count($translations->data) > 1) {
$handler
->entityFormSharedElements($form);
}
}
else {
$handler
->entityFormLanguageWidget($form, $form_state);
}
array_unshift($form['#validate'], 'entity_translation_entity_form_validate');
}
else {
$handler
->entityFormLanguageWidget($form, $form_state);
}
}
}
function entity_translation_entity_form_source_language_submit($form, &$form_state) {
$handler = entity_translation_entity_form_get_handler($form, $form_state);
$langcode = $form_state['values']['source_language']['language'];
$path = "{$handler->getEditPath()}/add/{$langcode}/{$handler->getActiveLanguage()}";
$options = array();
if (isset($_GET['destination'])) {
$options['query'] = drupal_get_destination();
unset($_GET['destination']);
}
$form_state['redirect'] = array(
$path,
$options,
);
$languages = language_list();
drupal_set_message(t('Source translation set to: %language', array(
'%language' => t($languages[$langcode]->name),
)));
}
function entity_translation_entity_form_delete_translation_submit($form, &$form_state) {
$handler = entity_translation_entity_form_get_handler($form, $form_state);
$path = "{$handler->getTranslatePath()}/delete/{$handler->getActiveLanguage()}";
$options = array();
if (isset($_GET['destination'])) {
$options['query'] = drupal_get_destination();
unset($_GET['destination']);
}
$form_state['redirect'] = array(
$path,
$options,
);
}
function entity_translation_entity_form_validate($form, &$form_state) {
$handler = entity_translation_entity_form_get_handler($form, $form_state);
if (!empty($handler)) {
$handler
->entityFormValidate($form, $form_state);
}
}
function entity_translation_entity_form_language_update($element, &$form_state, $form) {
$handler = entity_translation_entity_form_get_handler($form, $form_state);
if (!empty($form_state['entity_translation']['form_langcode'])) {
$handler
->setActiveLanguage($form_state['entity_translation']['form_langcode']);
}
if (empty($form_state['triggering_element']['#ajax'])) {
$handler
->entityFormLanguageWidgetSubmit($form, $form_state);
}
}
function entity_translation_entity_form_submit($form, &$form_state) {
if ($form_state['clicked_button']['#value'] == t('Delete')) {
$handler = entity_translation_entity_form_get_handler($form, $form_state);
if (count($handler
->getTranslations()->data) > 1) {
$info = entity_get_info($form['#entity_type']);
drupal_set_message(t('This will delete all the @entity_type translations.', array(
'@entity_type' => drupal_strtolower($info['label']),
)), 'warning');
}
}
}
function entity_translation_field_attach_submit($entity_type, $entity, $form, &$form_state) {
if (($handler = entity_translation_entity_form_get_handler($form, $form_state)) && entity_translation_enabled($entity_type, $entity)) {
$handler
->setEntity($entity);
$handler
->entityFormSubmit($form, $form_state);
entity_translation_taxonomy_term_field_attach_submit($entity_type, $entity, $form, $form_state);
}
}
function entity_translation_menu_local_tasks_alter(&$data, $router_item, $root_path) {
$handler = entity_translation_get_handler();
if (!empty($handler) && $handler
->isEntityForm() && $handler
->getLanguage() != LANGUAGE_NONE && drupal_multilingual()) {
$handler
->localTasksAlter($data, $router_item, $root_path);
}
}
function entity_translation_preprocess_page(&$variables) {
if (!empty($variables['tabs']['#secondary'])) {
$language_tabs = array();
foreach ($variables['tabs']['#secondary'] as $index => $tab) {
if (!empty($tab['#language_tab'])) {
$language_tabs[] = $tab;
unset($variables['tabs']['#secondary'][$index]);
}
}
if (!empty($language_tabs)) {
if (count($variables['tabs']['#secondary']) <= 1) {
$variables['tabs']['#secondary'] = $language_tabs;
}
else {
$variables['tabs']['#secondary']['#language_tabs'] = $language_tabs;
$variables['tabs']['#secondary']['#pre_render']['entity_translation'] = 'entity_translation_language_tabs_render';
}
}
}
}
function entity_translation_language_tabs_render($element) {
$build = array(
'#theme' => 'menu_local_tasks',
'#theme_wrappers' => array(
'entity_translation_language_tabs',
),
'#secondary' => $element['#language_tabs'],
'#attached' => array(
'css' => array(
drupal_get_path('module', 'entity_translation') . '/entity-translation.css',
),
),
);
$element['#suffix'] .= drupal_render($build);
return $element;
}
function theme_entity_translation_language_tabs($variables) {
return '<div class="entity-translation-language-tabs">' . $variables['element']['#children'] . '</div>';
}
function entity_translation_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
$instance = $form['#instance'];
$entity_type = $instance['entity_type'];
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
if (!empty($field['settings']['entity_translation_sync']) && field_is_translatable($entity_type, $field)) {
$form['instance']['settings']['entity_translation_sync'] = array(
'#prefix' => '<label>' . t('Field synchronization') . '</label>',
'#type' => 'checkbox',
'#title' => t('Enable field synchronization'),
'#description' => t('Check this option if you wish to synchronize the value of this field across its translations.'),
'#default_value' => !empty($instance['settings']['entity_translation_sync']),
);
}
$translatable = $field['translatable'];
$label = t('Field translation');
$title = t('Users may translate all occurrences of this field:') . _entity_translation_field_desc($field);
$form_state['field_has_data'] = field_has_data($field);
if ($form_state['field_has_data']) {
$path = "admin/config/regional/entity_translation/translatable/{$field_name}";
$status = $translatable ? $title : t('All occurrences of this field are untranslatable:') . _entity_translation_field_desc($field);
$link_title = !$translatable ? t('Enable translation') : t('Disable translation');
$form['field']['translatable'] = array(
'#prefix' => '<div class="translatable"><label>' . $label . '</label>',
'#suffix' => '</div>',
'message' => array(
'#markup' => $status . ' ',
),
'link' => array(
'#type' => 'link',
'#title' => $link_title,
'#href' => $path,
'#options' => array(
'query' => drupal_get_destination(),
),
'#access' => user_access('toggle field translatability'),
),
);
}
else {
$form['field']['translatable'] = array(
'#prefix' => '<label>' . $label . '</label>',
'#type' => 'checkbox',
'#title' => $title,
'#default_value' => $translatable,
);
}
$function = 'entity_translation_form_field_ui_field_edit_' . $instance['widget']['type'] . '_form_alter';
if (function_exists($function)) {
$function($form, $form_state);
}
}
function _entity_translation_field_desc($field) {
$instances = array();
foreach ($field['bundles'] as $entity_type => $bundle_names) {
$entity_type_info = entity_get_info($entity_type);
foreach ($bundle_names as $bundle_name) {
$instance_info = field_info_instance($entity_type, $field['field_name'], $bundle_name);
$instances[] = t('@instance_label in %entity_label', array(
'@instance_label' => $instance_info['label'],
'%entity_label' => $entity_type_info['bundles'][$bundle_name]['label'],
));
}
}
return theme('item_list', array(
'items' => $instances,
));
}
function entity_translation_enabled($entity_type, $entity = NULL, $skip_handler = FALSE) {
$enabled_types = variable_get('entity_translation_entity_types', array());
$enabled = !empty($enabled_types[$entity_type]) && ($skip_handler || field_has_translation_handler($entity_type, 'entity_translation'));
if (!$enabled || !isset($entity)) {
return $enabled;
}
$bundle = FALSE;
if (is_object($entity)) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
}
elseif (is_string($entity)) {
$bundle = $entity;
}
return $bundle && entity_translation_enabled_bundle($entity_type, $bundle);
}
function entity_translation_enabled_bundle($entity_type, $bundle) {
$info = entity_get_info($entity_type);
$bundle_callback = isset($info['translation']['entity_translation']['bundle callback']) ? $info['translation']['entity_translation']['bundle callback'] : FALSE;
return empty($bundle_callback) || call_user_func($bundle_callback, $bundle);
}
function entity_translation_settings($entity_type, $bundle) {
$settings = variable_get('entity_translation_settings_' . $entity_type . '__' . $bundle, array());
if (empty($settings)) {
$info = entity_get_info($entity_type);
if (!empty($info['translation']['entity_translation']['default settings'])) {
$settings = $info['translation']['entity_translation']['default settings'];
}
}
$settings += array(
'default_language' => ENTITY_TRANSLATION_LANGUAGE_DEFAULT,
'hide_language_selector' => TRUE,
'exclude_language_none' => FALSE,
'lock_language' => FALSE,
'shared_fields_original_only' => FALSE,
);
return $settings;
}
function entity_translation_language($entity_type, $entity) {
$handler = entity_translation_get_handler($entity_type, $entity);
if (!$handler) {
return LANGUAGE_NONE;
}
if (entity_translation_enabled($entity_type, $entity)) {
$langcode = $handler
->getActiveLanguage();
return $langcode ? $langcode : $handler
->getLanguage();
}
else {
return $handler
->getLanguage();
}
}
function entity_translation_get_handler($entity_type = NULL, $entity = NULL) {
$factory = EntityTranslationHandlerFactory::getInstance();
return empty($entity) ? $factory
->getLastHandler($entity_type) : $factory
->getHandler($entity_type, $entity);
}
function entity_translation_entity_form_get_handler($form, $form_state) {
$handler = FALSE;
if ($info = entity_translation_edit_form_info($form, $form_state)) {
$handler = entity_translation_get_handler($info['entity type'], $info['entity']);
}
return $handler;
}
function entity_translation_current_form_get_handler() {
$handler = FALSE;
if (!empty($_POST['form_build_id'])) {
$form_state = form_state_defaults();
if ($form = form_get_cache($_POST['form_build_id'], $form_state)) {
$handler = entity_translation_entity_form_get_handler($form, $form_state);
}
}
return $handler;
}
function entity_translation_edit_form_info($form, $form_state) {
$info = FALSE;
$entity_type = isset($form['#entity_type']) && is_string($form['#entity_type']) ? $form['#entity_type'] : FALSE;
if ($entity_type) {
$entity_info = entity_get_info($form['#entity_type']);
if (!empty($entity_info['translation']['entity_translation']['edit form'])) {
$entity_keys = explode('][', $entity_info['translation']['entity_translation']['edit form']);
$key_exists = FALSE;
$entity = drupal_array_get_nested_value($form_state, $entity_keys, $key_exists);
if ($key_exists) {
$info = array(
'entity type' => $form['#entity_type'],
'entity' => (object) $entity,
);
}
}
}
return $info;
}
function entity_translation_access($entity_type, $translation) {
return $translation['status'] || user_access('translate any entity') || user_access("translate {$entity_type} entities");
}
function entity_translation_languages($entity_type = NULL, $entity = NULL) {
if (isset($entity) && $entity_type == 'node' && module_exists('i18n_node')) {
}
elseif (variable_get('entity_translation_languages_enabled', FALSE)) {
$languages = language_list('enabled');
return $languages[1];
}
return language_list();
}
function entity_translation_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'entity_translation') . '/views',
);
}
function entity_translation_uuid_entities_features_export_entity_alter($entity, $entity_type) {
if (entity_translation_enabled($entity_type, $entity)) {
$fields = array(
'entity_type',
'entity_id',
'uid',
'created',
'changed',
);
$handler = entity_translation_get_handler($entity_type, $entity);
$translations = $handler
->getTranslations();
if ($translations && isset($translations->data)) {
foreach ($translations->data as &$translation) {
foreach ($fields as $field) {
unset($translation[$field]);
}
}
}
}
}
function entity_translation_entity_uuid_presave(&$entity, $entity_type) {
$entity_info = entity_get_info($entity_type);
if (isset($entity_info['entity keys']) && isset($entity_info['entity keys']['translations'])) {
$key = $entity_info['entity keys']['translations'];
if (isset($entity->{$key})) {
$entity->{$key} = (object) $entity->{$key};
}
}
}
function entity_translation_pathauto_alias_alter(&$alias, array &$context) {
$info = entity_get_info();
$entity_type = $context['module'];
$operations_modes = array(
'bulkupdate' => variable_get('entity_translation_pathauto_state_mode_bulkupdate', 'generate_all_aliases_all_languages'),
'update' => variable_get('entity_translation_pathauto_state_mode_update', 'generate_alias_active_language'),
);
$operation = $context['op'];
if (!in_array($operation, array_keys($operations_modes)) || empty($info[$entity_type]['token type']) || empty($context['data'][$info[$entity_type]['token type']])) {
return;
}
if (in_array($operation, array_keys($operations_modes)) && $operations_modes[$operation] === 'generate_alias_active_language') {
return;
}
$entity = $context['data'][$info[$entity_type]['token type']];
if (entity_translation_enabled($entity_type, $entity)) {
$translations = entity_translation_get_handler($entity_type, $entity)
->getTranslations();
foreach ($translations->data as $language => $translation) {
if ($language === $context['language']) {
continue;
}
$no_existing_alias = FALSE === _pathauto_existing_alias_data($context['source'], $language);
if ($operations_modes[$operation] === 'generate_all_aliases_all_languages' || $no_existing_alias && $operations_modes[$operation] === 'generate_missing_aliases_all_languages') {
pathauto_create_alias($entity_type, $operation . '_translation', $context['source'], $context['data'], $context['type'], $language);
}
}
}
}
function path_entity_translation_delete($entity_type, $entity, $langcode) {
$handler = entity_translation_get_handler($entity_type, $entity);
path_delete(array(
'source' => $handler
->getViewPath(),
'language' => $langcode,
));
}
function entity_translation_entity_save($entity_type, $entity) {
if (module_exists('entity')) {
entity_save($entity_type, $entity);
}
else {
field_attach_presave($entity_type, $entity);
field_attach_update($entity_type, $entity);
}
}
function entity_translation_field_attach_prepare_translation_alter(&$entity, $context) {
$handler = entity_translation_get_handler('node', $entity);
$handler
->setActiveLanguage($context['langcode']);
}