diff --git a/entity_translation.admin.inc b/entity_translation.admin.inc index cf278f2..9f17614 100644 --- a/entity_translation.admin.inc +++ b/entity_translation.admin.inc @@ -48,13 +48,20 @@ function entity_translation_admin_form($form, $form_state) { // Avoid bloating memory with unused data. drupal_static_reset('_entity_translation_validate_path_schemes'); + $enabled_types = variable_get('entity_translation_entity_types', array()); - $form['entity_translation_entity_types'] = array( - '#type' => 'checkboxes', + $form['enabled'] = array( + '#type' => 'fieldset', '#title' => t('Translatable entity types'), '#description' => t('Select which entities can be translated.'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $form['enabled']['entity_translation_entity_types'] = array( + '#type' => 'checkboxes', '#options' => $options, - '#default_value' => variable_get('entity_translation_entity_types', array()), + '#default_value' => $enabled_types, ); $form['tabs'] = array( @@ -65,54 +72,64 @@ function entity_translation_admin_form($form, $form_state) { '#tree' => TRUE, ); - $et_settings = variable_get('entity_translation_settings', array()); - $languages = array( - 'xx-et-default' => t('Default language'), - 'xx-et-current' => t('Current language'), - 'xx-et-author' => t('Author language'), + ENTITY_TRANSLATION_LANGUAGE_DEFAULT => t('Default language'), + ENTITY_TRANSLATION_LANGUAGE_CURRENT => t('Current language'), + ENTITY_TRANSLATION_LANGUAGE_AUTHOR => t('Author language'), LANGUAGE_NONE => t('Language neutral'), ); foreach (entity_translation_languages($entity_type) as $langcode => $language) { $languages[$langcode] = t($language->name); } - foreach ($options as $entity_type => $label) { - $form['entity_translation_settings'][$entity_type] = array( - '#type' => 'fieldset', - '#group' => 'tabs', - '#title' => $label, - ); + foreach (array_filter($enabled_types) as $entity_type) { + $label = $options[$entity_type]; $entity_info = entity_get_info($entity_type); $bundles = !empty($entity_info['bundles']) ? $entity_info['bundles'] : array($entity_type => array('label' => $label)); + $enabled_bundles = 0; foreach ($bundles as $bundle => $info) { - $form['entity_translation_settings'][$entity_type][$bundle] = array(); - - if (count($bundles) > 1) { - $form['entity_translation_settings'][$entity_type][$bundle] += array( - '#type' => 'fieldset', - '#title' => $info['label'], - '#collapsible' => TRUE, - '#collapsed' => TRUE, + if (entity_translation_enabled_bundle($entity_type, $bundle)) { + $enabled_bundles++; + $settings = entity_translation_settings($entity_type, $bundle); + + // Show fieldsets only if there is more than one bundle defined. + if (count($bundles) > 1) { + $form['entity_translation_settings'][$entity_type][$bundle] = array( + '#type' => 'fieldset', + '#title' => $info['label'], + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + } + + $form['entity_translation_settings'][$entity_type][$bundle]['default_language'] = array( + '#type' => 'select', + '#title' => t('Default language'), + '#options' => $languages, + '#default_value' => $settings['default_language'], ); - } - $settings = isset($et_settings[$entity_type][$bundle]) ? $et_settings[$entity_type][$bundle] : array(); + $form['entity_translation_settings'][$entity_type][$bundle]['hide_language_selector'] = array( + '#type' => 'checkbox', + '#title' => t('Hide language selector'), + '#default_value' => $settings['hide_language_selector'], + ); + } + } - $form['entity_translation_settings'][$entity_type][$bundle]['default_language'] = array( - '#type' => 'select', - '#title' => t('Default language'), - '#options' => $languages, - '#default_value' => !empty($settings['default_language']) ? $settings['default_language'] : LANGUAGE_NONE, + if (!empty($form['entity_translation_settings'][$entity_type])) { + $form['entity_translation_settings'][$entity_type] += array( + '#type' => 'fieldset', + '#group' => 'tabs', + '#title' => $label, ); - $form['entity_translation_settings'][$entity_type][$bundle]['hide_language_selector'] = array( - '#type' => 'checkbox', - '#title' => t('Hide language selector'), - '#default_value' => !empty($settings['hide_language_selector']), - ); + // If we have only one enabled bundle remove the fieldset. + if ($enabled_bundles == 1) { + unset($form['entity_translation_settings'][$entity_type][$bundle]['#type']); + } } } @@ -134,6 +151,30 @@ function entity_translation_admin_form_submit($form, $form_state) { } /** + * Applies the given settings to every defined bundle. + * + * @param $entity_type + * The entity type the settings refer to. + * @param $settings + * (optional) The settings to be applied. Defaults to the entity default + * settings. + */ +function entity_translation_settings_init($entity_type, $settings = NULL) { + if (entity_translation_enabled($entity_type)) { + $info = entity_get_info($entity_type); + $bundles = !empty($info['bundles']) ? array_keys($info['bundles']) : array($entity_type); + + foreach ($bundles as $bundle) { + if (entity_translation_enabled_bundle($entity_type, $bundle)) { + $et_settings[$entity_type][$bundle] = !empty($settings) ? $settings : entity_translation_settings($entity_type, $bundle); + } + } + } + + variable_set('entity_translation_settings', $et_settings); +} + +/** * Translations overview page callback. */ function entity_translation_overview($entity_type, $entity, $callback = NULL) { diff --git a/entity_translation.api.php b/entity_translation.api.php index cc6e8dd..33b447f 100644 --- a/entity_translation.api.php +++ b/entity_translation.api.php @@ -63,6 +63,10 @@ * alterations to the entity form. Defaults to ENTITY_TYPE. * - skip original values access: A flag specifying whether skipping access * control when editing original values for this entity. Defaults to FALSE. + * - bundle callback: A callback to check whether the passed bundle has entity + * translation enabled. If empty all bundles are supposed to be enabled. + * - default settings: The defaults to be applied to settings when an explicit + * choice is missing. */ function hook_entity_info() { $info['custom_entity'] = array( @@ -73,6 +77,11 @@ function hook_entity_info() { 'access callback' => 'custom_entity_tab_access', 'access arguments' => array(1), 'edit form' => 'custom_entity_form_state_key', + 'bundle callback' => 'custom_entity_translation_enabled_bundle', + 'default settings' => array( + 'default_language' => LANGUAGE_NONE, + 'hide_language_selector' => FALSE, + ), ), ), ); diff --git a/entity_translation.install b/entity_translation.install index 8fa0b07..e37c344 100644 --- a/entity_translation.install +++ b/entity_translation.install @@ -217,6 +217,7 @@ function entity_translation_uninstall() { variable_del('entity_translation_disabled_content_types'); variable_del('entity_translation_languages_enabled'); variable_del('entity_translation_shared_labels'); + variable_del('entity_translation_settings'); foreach (node_type_get_types() as $type => $object) { variable_del("entity_translation_node_metadata_$type"); @@ -240,3 +241,13 @@ function entity_translation_update_7001() { function entity_translation_update_7002() { return _entity_translation_grant_original_values_permissions(); } + +/** + * Configure node and comment language settings to the prior default behavior. + */ +function entity_translation_update_7003() { + module_load_include('inc', 'entity_translation', 'entity_translation.admin'); + foreach (array_keys(entity_get_info()) as $entity_type) { + entity_translation_settings_init($entity_type); + } +} diff --git a/entity_translation.module b/entity_translation.module index 931ef59..66c4f47 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -9,6 +9,25 @@ module_load_include('inc', 'entity_translation', 'entity_translation.node'); /** + * + * @var unknown_type + */ +define('ENTITY_TRANSLATION_LANGUAGE_DEFAULT', 'xx-et-default'); + +/** + * + * @var unknown_type + */ +define('ENTITY_TRANSLATION_LANGUAGE_CURRENT', 'xx-et-current'); + +/** + * + * @var unknown_type + */ +define('ENTITY_TRANSLATION_LANGUAGE_AUTHOR', 'xx-et-author'); + + +/** * Implements hook_hook_info(). */ function entity_translation_hook_info() { @@ -59,6 +78,11 @@ function entity_translation_entity_info() { '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, + ), ), ), ); @@ -69,6 +93,11 @@ function entity_translation_entity_info() { '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, + ), ), ), ); @@ -649,8 +678,9 @@ function entity_translation_admin_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); - return $handler->getLanguage() != LANGUAGE_NONE; + list(, , $bundle) = entity_extract_ids($entity_type, $entity); + $enabled = entity_translation_enabled_bundle($entity_type, $bundle); + return $enabled && entity_translation_get_handler($entity_type, $entity)->getLanguage() != LANGUAGE_NONE; } return FALSE; } @@ -1468,6 +1498,40 @@ function entity_translation_enabled($entity_type, $skip_handler = FALSE) { } /** + * Helper function. Determines whether the given entity bundle is translatable. + */ +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); +} + +/** + * Return the entity translation settings for the given entity type and bundle. + */ +function entity_translation_settings($entity_type, $bundle) { + $settings = array(); + $et_settings = variable_get('entity_translation_settings', array()); + + if (isset($et_settings[$entity_type][$bundle])) { + $settings = $et_settings[$entity_type][$bundle]; + } + else { + $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, + ); + + return $settings; +} + +/** * Entity language callback. * * This callback changes the entity language from the actual one to the active diff --git a/entity_translation.node.inc b/entity_translation.node.inc index c5d7b13..ad67056 100644 --- a/entity_translation.node.inc +++ b/entity_translation.node.inc @@ -63,16 +63,13 @@ function entity_translation_node_menu_alter(&$items, $backup) { function entity_translation_node_tab_access() { $args = func_get_args(); $node = array_shift($args); - if ($node->language != LANGUAGE_NONE) { - if (entity_translation_node_supported_type($node->type)) { - return entity_translation_tab_access('node', $node); - } - elseif (entity_translation_node('node', $node)) { - $function = array_shift($args); - return call_user_func_array($function, $args); - } + if (entity_translation_node('node', $node)) { + $function = array_shift($args); + return call_user_func_array($function, $args); + } + else { + return entity_translation_tab_access('node', $node); } - return FALSE; } /** @@ -127,8 +124,9 @@ function entity_translation_node_view($node, $build_mode) { function entity_translation_form_node_type_form_alter(&$form, &$form_state) { $type = $form['#node_type']->type; + $t_args = array('!url' => url('admin/config/regional/entity_translation')); $form['workflow']['language_content_type']['#options'][ENTITY_TRANSLATION_ENABLED] = t('Enabled, with field translation'); - $form['workflow']['language_content_type']['#description'] .= ' ' . t('

If field translation is selected you can have per-field translation for each available language.'); + $form['workflow']['language_content_type']['#description'] .= '

' . t('If field translation is selected you can have per-field translation for each available language. You can find more options in the entity translation settings.', $t_args) . '

'; $form['display']['entity_translation_node_metadata'] = array( '#type' => 'radios', @@ -195,6 +193,17 @@ function entity_translation_preprocess_node(&$variables) { } /** + * Returns whether the given comment type has support for translations. + * + * @return + * Boolean value. + */ +function entity_translation_comment_supported_type($comment_type) { + $type = str_replace('comment_node_', '', $comment_type); + return entity_translation_node_supported_type($type); +} + +/** * Implements hook_query_TAG_alter(). * * Filters out node comments by content language. diff --git a/includes/translation.handler.inc b/includes/translation.handler.inc index 67795cd..41c0e0f 100644 --- a/includes/translation.handler.inc +++ b/includes/translation.handler.inc @@ -633,24 +633,26 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa if (isset($et_settings[$this->entityType][$bundle])) { switch ($et_settings[$this->entityType][$bundle]['default_language']) { - case 'xx-et-default': + case ENTITY_TRANSLATION_LANGUAGE_DEFAULT: $langcode = language_default()->language; break; - case 'xx-et-current': + case ENTITY_TRANSLATION_LANGUAGE_CURRENT: $langcode = $GLOBALS[LANGUAGE_TYPE_CONTENT]->language; break; - case 'xx-et-author': + case ENTITY_TRANSLATION_LANGUAGE_AUTHOR: $langcode = $GLOBALS['user']->language; break; default: + // An actual language code has been explicitly configured. $langcode = $et_settings[$this->entityType][$bundle]['default_language']; } } else { - $langcode = LANGUAGE_NONE; + // Fall back to the default language to keep backward compatibility. + $langcode = language_default()->language; } return $langcode;