0

By default when you add a translation your fields get pre-filled with the content of the original language. Is there a way to stop this behaviour for specific fields, in this case an image field. Both the node and field are set to translatable. The image field still needs to be translatable, but the original content should not get copied into the translation.

Reasoning for this is because on our project we have a master language which will provide default content if no localised content is found. So if we upload an image in the master language en and no image is uploaded for nl we use the image of the en language.

But by default the image is copied into the translation, so if the image needs to be updated for the master language, you then need to re-visit every translation and change the image here as well.

Practical example. I created a node in the language English and uploaded an image for field field_series_image. Now when I add a translation for Dutch when I hit the node/{nid}/edit/add/en/nl page I would like to have an empty image field and not pre-filled with the values from the source language en.

I have tried setting and unsetting a lot of values but none of them seemed to work. I tried to do this in the form_alter hook like this:

my_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id === 'my_custom_node_form_id') {
    if (strpos($form['#action'], '/edit/add/') > 0) {
      // Logic went here.
    }
  }
}

Things I have tried:

  • Unsetting the field in the $form_state['build_info']['args'] array
  • Unsetting the default value in the $form array
  • Unsetting the field in the $form['#entity'] array.
  • Setting the default value to 0 ($form['field_series_image']['nl'][0]['#default_value'] = 0)

None of them seemed to work, maybe because the image field is a widget?

TLDNR; When adding a translation (so being on node/{nid}/edit/add/en/nl) I want to empty a image field using some hook. So the field is empty on the page and not saved using the values from the original node.

0

Figured it out after spending about a whole day of trying in the wrong hook.

Turns out you need the hook_field_widget_form_alter for this.

/**
 * Implements hook_field_widget_form_alter();
 */
function my_module_field_widget_form_alter(&$element, &$form_state, &$context) {
  $field_name = isset($element[0]['#field_name']) ? $element[0]['#field_name'] : NULL;
  if ($field_name === 'field_series_image') {
    if (strpos($form_state['complete form']['#action'], '/edit/add/') > 0) {
      // Works for single image fields (product display).
      $element[0]['#default_value']['fid'] = 0;

      // In case we have more images uploaded, unset all.
      for ($index = $element['#file_upload_delta']; $index > 0; $index--) {
        unset($element[$index]);
      }

      // Also reset the upload delta to 0.
      $element['#file_upload_delta'] = 0;
    }
  }
}

Code above suits our needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.