Problem/Motivation

Currently it is not possible to fully "translate" image file field. It is possible to translate only Image file (different files for different translations) OR only "Alt" and "Title" attributes. If you try to translate both (image file and it's attributes) - it doesn't work correctly.

Symptoms and steps to reproduce

Feature that doesn't work

  1. Allow image field translation and select "File" in Translatable elements section.
  2. Translate a node that has image field:
    1. Upload new image for the translation
    2. Translate image title attribute
    3. Translate image alt attribute

Result
Uploaded image file overwrites all language versions of the node. This can be seen perfectly clear inside node__[image_field] table after translation of a node with "translated" image is saved.

The expected behavior is to update only the translated version of the node with new file and it's new attributes.

However this feature will work partially in two cases:

  1. If you leave "Alt" and "Title" attributes blank, then you'll be able to upload different images for each translation.
  2. If you set Translatable elements section to only "Alt" and "Title", everything will work as expected. In this case an image will have different Alt and Title attributes for each language, however the actual image file will always be the same.
    This works fine but is a bit limitng

Interestingly, the generic file field is NOT affected by this issue. Translated nodes can have different versions of files uploaded. Only the image field is affected by this issue.

Proposed resolution

Allow uploading different language versions of image files the same way as generic files.

Remaining tasks

Find out why this does not work and create a patch.

User interface changes

None.

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SiliconMind’s picture

Issue summary: View changes

Added inline images.

SiliconMind’s picture

Fixed language-content tag

SiliconMind’s picture

Issue summary: View changes
FileSize
4.21 KB

It turns out that it is possible to have different image files for each translation BUT only if "Alt" and "Title" attributes are left blank. Updating issue description.

Any clues why this is happening?

SiliconMind’s picture

Comment Removed.

Once again fooled by virtually undebugable Entity class.

SiliconMind’s picture

FileSize
155.66 KB

The problem seems to be that Node::values holds correct data but Node::fields holds broken data. They don't match and the data that is saved comes from Node::fields array.

This image shows kint dump of a node just before it gets saved with ContentEntityDatabaseStorage::doSaveFieldItems().

Kint dump of Node entity before save

Below is the fragment of ContentEntityDatabaseStorage::doSaveFieldItems():

      $langcodes = $field->isTranslatable() ? $translation_langcodes : array($default_langcode);
      foreach ($langcodes as $langcode) {
        $delta_count = 0;
        $items = $entity->getTranslation($langcode)->get($field_name);

        // At this point we have broken data inside $items array!
        $items->filterEmptyItems();

        foreach ($items as $delta => $item) {
          // We now know we have someting to insert.
          $do_insert = TRUE;
          $record = array(
            'entity_id' => $id,
            'revision_id' => $vid,
            'bundle' => $bundle,
            'delta' => $delta,
            'langcode' => $langcode,
          );
          foreach ($field->getColumns() as $column => $attributes) {
            $column_name = static::_fieldColumnName($field, $column);
            // Serialize the value if specified in the column schema.
            $record[$column_name] = !empty($attributes['serialize']) ? serialize($item->$column) : $item->$column;
          }
          $query->values($record);
          $revision_query->values($record);

          if ($field->getCardinality() != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && ++$delta_count == $field->getCardinality()) {
            break;
          }
        }
      }
Jose Reyero’s picture

matsbla’s picture

Status: Active » Closed (fixed)

I tested this again now in D8 beta9, and seems like this is no longer an issue! :)