I have a module which uses the field API to create a custom field. So I have an install file implementing hook_field_schema, and module file using some standard hooks like hook_field_info(), hook_field_widget_form(), etc.. I followed the Drupal examples module code pretty closely.
This module works fine for storing the values for one language.
I'm running into a problem that when we try to translate the values to another language, the values in the field_data & field_revision tables seem to be updated rather than new records inserted.
So from the admin's point of view - you save values in the German page, okay.. then you click translate tab and go to edit the English page, and save the values there - okay.. then go back to German and those values are now gone. In the database, there's only one set of values that just keeps being edited, either to be "en" or "de". What am I missing?
Here is part of my module that might help:
/**
* Implements hook_field_widget_form().
*
* hook_widget_form() is where Drupal tells us to create form elements for
* our field's widget.
*
*/
function specs_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
global $bspecs; //this is an array of all the product specs
$widget = $element;
$widget['#delta'] = $delta;
switch ($instance['widget']['type']) {
case 'specs_text':
$widget += array(
'#type' => 'fieldset',
'#element_validate' => array('specs_text_validate'),
'#delta' => $delta,
'#attached' => array(
'css' => array(drupal_get_path('module', 'specs') . '/specs.css'),
),
);
//create text field for each spec
foreach ($bspecs as $key => $title) {
$widget[$key] = array(
'#type' => 'textfield',
'#title' => t($title),
'#size' => 20,
'#default_value' => isset($items[$delta][$key]) ? $items[$delta][$key] : '',
'#attributes' => array('class' => array('specs-entry')),
);
// Since Form API doesn't allow a fieldset to be required, we
// have to require each field element individually.
if ($instance['required'] == 1) $widget[$key]['#required'] = 1;
}
break;
}
$element['specs'] = $widget;
return $element;
}
/**
* Implements hook_field_presave
*/
function specs_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items){
//$items[0] = array();
if (empty($items)) {
return;
}
//reformat the items array for our data to store into our schema correctly
$items[0] = $items[0]['specs'];
}
The above code is based somewhat on this example:
https://api.drupal.org/api/examples/field_example!field_example.module/g...
Comments
Comment #1
fprevos2 CreditAttribution: fprevos2 commentedI'm having a similar issue I'm not sure if it's the same bug both we both wrap our field in a fieldset. (I'm creating a custom radio/checkbox field with a wrapping fieldset and it won't translate the options.)
Comment #2
espurnesSimilar issue here.
I have a custom field with text_filed + text_format. It works well in a non multilingual site but with multilingual site the body content (text_format) blow away.
Steps to reproduce:
Custom field with two components: tab-title (text_field) and tab-body (text-format).
I guess is a misconfiguration of my custom module. Perhaps in HOOK_field_presave. Any suggestion?
Here you have my module's code:
T_field_format_tabs.install
T_field_format_tabs.module
I used this page to create the module.