diff --git a/modules/product/commerce_product.module b/modules/product/commerce_product.module index d76a519..5d7da5a 100644 --- a/modules/product/commerce_product.module +++ b/modules/product/commerce_product.module @@ -85,6 +85,7 @@ function commerce_product_entity_info() { 'bundle' => 'type', 'label' => 'title', 'revision' => 'revision_id', + 'language' => 'language', ), 'bundle keys' => array( 'bundle' => 'type', @@ -115,6 +116,11 @@ function commerce_product_entity_info() { 'locale' => TRUE, 'entity_translation' => array( 'class' => 'EntityTranslationCommerceProductHandler', + 'bundle callback' => 'commerce_product_entity_translation_supported_type', + 'default settings' => array( + 'default_language' => LANGUAGE_NONE, + 'hide_language_selector' => FALSE, + ), ), ), @@ -894,13 +900,7 @@ function _commerce_product_match_products_standard($instance, $string = '', $mat * Access callback: determines access to a product's translation tab. */ function commerce_product_entity_translation_tab_access($product) { - if (!empty($product->language) && $product->language != LANGUAGE_NONE) { - if (commerce_product_entity_translation_supported_type($product->type)) { - return entity_translation_tab_access('commerce_product'); - } - } - - return FALSE; + return entity_translation_tab_access('commerce_product', $product); } /** diff --git a/modules/product/commerce_product_ui.module b/modules/product/commerce_product_ui.module index 8a43d4d..e7b1e28 100644 --- a/modules/product/commerce_product_ui.module +++ b/modules/product/commerce_product_ui.module @@ -263,9 +263,6 @@ function commerce_product_ui_entity_info_alter(&$entity_info) { // Add callbacks and urls for administer translations. $entity_info['commerce_product']['translation']['entity_translation'] += array( 'base path' => 'admin/commerce/products/%commerce_product', - 'access callback' => 'commerce_product_entity_translation_tab_access', - 'access arguments' => array(3), - 'edit form' => TRUE, ); // Expose the admin UI for product fields. @@ -470,31 +467,6 @@ function commerce_product_ui_form_commerce_product_ui_product_form_alter(&$form, // Add a submit handler to the save button to add a redirect. $form['actions']['submit']['#submit'][] = 'commerce_product_ui_product_form_submit'; - // If translation support is enabled, provide the suitable languages - if (module_exists('entity_translation') && entity_translation_enabled('commerce_product')) { - $handler = entity_translation_get_handler('commerce_product', $product); - $translations = $handler->getTranslations(); - - $form['language'] = array( - '#type' => 'select', - '#title' => t('Language'), - '#default_value' => isset($product->language) ? $product->language : '', - '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'), - '#weight' => -10, - ); - - // Disable languages for existing translations, so it is not possible to - // switch this product to some language which is already in the translation set. - foreach ($translations->data as $langcode => $translation) { - if ($langcode != $translations->original) { - unset($form['language']['#options'][$langcode]); - } - } - - // Since this function may change the language of the submitted form values, - // it has to be the first called. - array_unshift($form['actions']['submit']['#submit'], 'commerce_product_ui_product_form_translation_submit'); - } // Add the save and continue button for new products. if (empty($product->product_id)) { @@ -530,34 +502,6 @@ function commerce_product_ui_product_form_submit($form, &$form_state) { } /** - * Submit callback for commerce_product_ui_product_form(). - * - * Checks if translation is enabled for the product and handles language changes. - * Since this handler may change the language of submitted form values it should - * be the first submit handler called. - * - * @see commerce_product_ui_form_commerce_product_ui_product_form_alter() - */ -function commerce_product_ui_product_form_translation_submit($form, &$form_state) { - // Get an array of available languages. - $available_languages = field_content_languages(); - list(, , $bundle) = entity_extract_ids('commerce_product', $form_state['commerce_product']); - - foreach (field_info_instances('commerce_product', $bundle) as $instance) { - $field_name = $instance['field_name']; - $field = field_info_field($field_name); - $previous_language = $form[$field_name]['#language']; - - // Handle a possible language change; new language values are inserted and - // the previous values are deleted. - if ($field['translatable'] && $previous_language != $form_state['values']['language']) { - $form_state['values'][$field_name][$form_state['values']['language']] = $form_state['commerce_product']->{$field_name}[$previous_language]; - $form_state['values'][$field_name][$previous_language] = array(); - } - } -} - -/** * Implements hook_form_FORM_ID_alter(). * * The Product UI module instantiates the Product delete form at a particular diff --git a/modules/product/includes/commerce_product.forms.inc b/modules/product/includes/commerce_product.forms.inc index fa58bd3..0927e36 100644 --- a/modules/product/includes/commerce_product.forms.inc +++ b/modules/product/includes/commerce_product.forms.inc @@ -14,8 +14,6 @@ * with only a product type defined. */ function commerce_product_product_form($form, &$form_state, $product) { - $language = !empty($product->language) ? $product->language : LANGUAGE_NONE; - // Ensure this include file is loaded when the form is rebuilt from the cache. $form_state['build_info']['files']['form'] = drupal_get_path('module', 'commerce_product') . '/includes/commerce_product.forms.inc'; @@ -42,7 +40,8 @@ function commerce_product_product_form($form, &$form_state, $product) { // Add the field related form elements. $form_state['commerce_product'] = $product; - field_attach_form('commerce_product', $product, $form, $form_state, $language); + $langcode = entity_language('commerce_product', $product); + field_attach_form('commerce_product', $product, $form, $form_state, $langcode); $form['status'] = array( '#type' => 'radios', @@ -94,7 +93,7 @@ function commerce_product_product_form($form, &$form_state, $product) { // Simply use default language $form['language'] = array( '#type' => 'value', - '#value' => $language, + '#value' => $langcode, ); // We add the form's #submit array to this button along with the actual submit diff --git a/modules/product/includes/commerce_product.translation_handler.inc b/modules/product/includes/commerce_product.translation_handler.inc index a1d38ad..f336529 100644 --- a/modules/product/includes/commerce_product.translation_handler.inc +++ b/modules/product/includes/commerce_product.translation_handler.inc @@ -15,8 +15,8 @@ */ class EntityTranslationCommerceProductHandler extends EntityTranslationDefaultHandler { - public function __construct($entity_type, $entity_info, $entity, $entity_id) { - parent::__construct('commerce_product', $entity_info, $entity, $entity_id); + public function __construct($entity_type, $entity_info, $entity) { + parent::__construct('commerce_product', $entity_info, $entity); } /** @@ -27,17 +27,29 @@ class EntityTranslationCommerceProductHandler extends EntityTranslationDefaultHa } /** - * Returns the original language of the product. + * Checks whether the current user has access to this product. */ - public function getLanguage() { - return $this->entity->language; + public function getAccess($op) { + return commerce_product_access($op, $this->entity); } /** - * Checks whether the current user has access to this product. + * Tweaks the product form to support multilingual elements. */ - public function getAccess($op) { - return commerce_product_access($op, $this->entity); + public function entityForm(&$form, &$form_state) { + parent::entityForm($form, $form_state); + if (isset($form['change_history']['#weight'])) { + $form['translation']['#weight'] = $form['change_history']['#weight'] - 0.01; + } + $form['actions']['delete_translation']['#suffix'] = $form['actions']['submit']['#suffix']; + unset($form['actions']['submit']['#suffix']); + } + + /** + * @see EntityTranslationDefaultHandler::entityFormTitle() + */ + protected function entityFormTitle() { + return commerce_product_ui_product_title($this->entity); } /** diff --git a/modules/product/includes/commerce_product_ui.forms.inc b/modules/product/includes/commerce_product_ui.forms.inc index 711f1f2..fe6da7d 100644 --- a/modules/product/includes/commerce_product_ui.forms.inc +++ b/modules/product/includes/commerce_product_ui.forms.inc @@ -74,7 +74,7 @@ function commerce_product_ui_product_type_form($form, &$form_state, $product_typ $form['product_type']['multilingual'] = array( '#type' => 'radios', '#title' => t('Multilingual support'), - '#description' => t('If Entity translation is enabled it will be possible to provide a different version of the same product for each available language.') . '
' . t('Existing products will not be affected by changing this option.'), + '#description' => t('If Entity translation is enabled it will be possible to provide a different version of the same product for each available language.') . '
' . t('You can find more options in the entity translation settings.', array('!url' => url('admin/config/regional/entity_translation'))) . '
' . t('Existing products will not be affected by changing this option.'), '#options' => array( 0 => t('Disabled'), ENTITY_TRANSLATION_ENABLED => t('Enabled via Entity translation'),