for Field collection translatable Fields (that have "Users may translate this field" option checked (Entity translation) the Field language it's needed instead of LANGUAGE_NONE.
As this field language it's used for the entity field content.
for example

$this->langcode = field_language($entity_type, $entity, $this->field_name);

intead of

$this->langcode = LANGUAGE_NONE;

actually the langcode argument it's really needed here?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

floretan’s picture

Status: Active » Needs review
FileSize
6.04 KB

Here's a patch that uses the language code provided by the Field API hooks instead of hardcoding LANGUAGE_NONE.

The patch also adds a check in the validation handler. This fixes an error on the entity_translation form where the delta of a blank field collection was checked with a non-existent field_state.

Combined with #1367832: Check #parents and #field_parents for source language in new translation form , this patch makes it possible to have translatable field_collection fields with entity_translation!

New_Bee’s picture

Translations are working fine while editing. While adding translation if the parent node has more than one field collection items, only one will be saving for the translation.

duellj’s picture

The problem with only one field collection being saved for a translation comes from the problem with attaching the $field_state['entity'] during the call to field_collection_field_widget_form(). The translation form is built first by entity_translation with empty fields, then the form fields from the source form (node, taxonomy, etc) are added post processing to fill in the populated default values. At that point, $field_state['entity'] only contains one value, instead of the number of values copied over from the source form, so only one value will be saved.

The best fix would be to rework how entity_translation builds the translation form, but that's a lot of work. Here's a patch that adds a form_alter that adds in the necessary $field_state['entity'] objects for each field collection.

longwave’s picture

#3 rebased against latest 7.x-1.x.

This works for me except in Views, where even if I select "current language" as the Views field language, I get all languages if I have field collection items pulled into my View.

longwave’s picture

Actually, it seems the language code doesn't cascade down to the member fields of the collection; the field collection entity itself is given a language code, but the fields that belong to that collection are always of language "und", which is where I think the problem with Views lies.

longwave’s picture

Not sure if the Views issue should be solved by Entity translation, in which case #1330332: Entity translation: Views field language filter looks relevant.

chaskype’s picture

Status: Needs review » Needs work

The last submitted patch, field_collection-1366220-entity_translation.patch, failed testing.

Dentorat’s picture

I manually applied patch at #4 and got the error

Notice: Undefined variable: field_collection_item in field_collection_field_widget_embed_validate() (line 1281 of /sites/default/modules/contrib/field_collection/field_collection.module).

EntityMalformedException: Missing bundle property on entity of type field_collection_item. in entity_extract_ids() (line 7539 of /includes/common.inc).

bmx269’s picture

Are there any updates on getting this to work? Thanks

marcusx’s picture

Status: Needs work » Needs review
FileSize
8.47 KB

Refactoring patch to make it apply to the latest dev (and let it hopefully pass the testbot).

marcusx’s picture

@penthehuman: Where do you get these messages?

I can confirm that it is broken if I try to edit a field collection item via its url.

e.g.: http://my.domain/en/field-collection/field-myfieldname/1/edit

I get errors from entity translation:

Notice: Undefined offset: 0 in entityForm() (line 690 of /.../sites/all/modules/contrib/entity_translation/includes/translation.handler.inc). =>

and

Notice: Trying to get property of non-object in entityForm() (line 690 of /../sites/all/modules/contrib/entity_translation/includes/translation.handler.inc). =>

This comes from the effort of entity translation to append the language to the title.

Here two screenshots to make it clearer whats going on.

Broken Version:
screenshot_title_alter_not_possible.png

Quickfixed Version: (hacking the value into translation.handler.inc to check if all problems are going away)
screenshot_title_alter_fixed.png

  /**
   * @see EntityTranslationHandlerInterface::entityForm()
   */
  public function entityForm(&$form, &$form_state) {
    $translations = $this->getTranslations();
    $form_langcode = $this->getFormLanguage(); //field_collection does not support this.
    $langcode = $this->getLanguage();
    $is_translation = $this->isTranslationForm();
    $new_translation = !isset($translations->data[$form_langcode]);
    $source_language = $this->getSourceLanguage();
    $no_translations = count($translations->data) < 2;
    $languages = language_list();

    // Adjust page title to specify the current language being edited, if we
    // have at least one translation.
    $form_langcode = 'en'; //Set the form_langcode manual for testing.
    if ($form_langcode != LANGUAGE_NONE && (!$no_translations || $new_translation)) {
      drupal_set_title($this->entityFormTitle() . ' [' . t($languages[$form_langcode]->name) . ']', PASS_THROUGH);
    }

But even if the form language if set we cannot save the form. We get

Exception: Invalid translation language in EntityTranslationDefaultHandler->setTranslation() (line 387 of /.../sites/all/modules/contrib/entity_translation/includes/translation.handler.inc).

The exception is thrown here:

  /**
   * @see EntityTranslationHandlerInterface::setTranslation()
   */
  public function setTranslation($translation, $values = NULL) {
    if (isset($translation['source']) && $translation['language'] == $translation['source']) { //Line 387
      throw new Exception('Invalid translation language');
    }

dump for $translation
screenshot_var_dump_fc_trans_exception.png

So there is further investigation necessary. Setting this back to "needs work".

UPDATE:
It will work for the embedded widget as I realized meanwhile.
And you must set the field_collection field (on the nodes manage fields page) to translateable itself. I first thought this isn't needed as it will always hold the same field collection item id - but from there comes the language for the fields inside the item.

phreaknerd’s picture

Added a patch to #1281974: Missing bundle property on entity - related to translations (#52) which seems to fix this issue too.
The fields are now translatable and editable too (by the links in collection view).

One issue still appears: you can't use the "Add"-link in collection view to add a new item. Seemd that the correct relation to the field_collection and parent node is missing.
Another one is that the translated revisions don't get deleted when removing the translation of the parent node.

phreaknerd’s picture

Status: Needs work » Needs review

Resetting the status: please check the previous patch.

Fidelix’s picture

Any news on this?

malberts’s picture

Status: Needs review » Closed (duplicate)

I think this issue can be seen as a sub-issue of integrating Entity Translation.

Therefore marking as a duplicate of #1344672: Field Collection: Field translation (entity_translation) support. where I've recently added a patch for that. Please have a look there so we can try to get that patch fleshed out.

ghalenir’s picture

Status: Closed (duplicate) » Needs review

Status: Needs review » Needs work

The last submitted patch, field_collection-entity_translation-1366220-11.patch, failed testing.

plach’s picture

Status: Needs work » Closed (duplicate)

There is a new patch providing full ET integration in #1344672-45: Field Collection: Field translation (entity_translation) support..