Environment:

  1. Media (latest dev)
  2. Entity Translation
  3. i18n

Trying to be as succinct as possible here:

'media_filter' calls 'media_token_to_markup' in order to convert the json array into markup. Everything works as expected when when you are using the interface to save your entities.

The problem occurs when you aren't using the interface to create entities. Perhaps using some tools such as Migrate, Feeds, UUID_Features and so on. Inside the function 'media_token_to_markup' the file_load function is called which in turns calls the entity_controller and media's own 'MediaEntityTranslationHandler'. The problem with this handler is that it only bases itself on the $global language. So say you are using migrate to bring in your entities the $global language will always be 'en' and file_load($fid) will only ever return the english alt value.

I have created a patch that does a few things:

  1. Converts function media_filter to full form so langcode of the field can be taken into account.
  2. Function media_token_to_markup takes the langcode parameters and passes it to:
  • media_get_file_without_label
  • field_attach_prepare_view
  • entity_prepare_view
  • field_attach_view
  • Function media_get_file_without_label has additional logic to check if the entity has field translation enabled and leverages field_get_items to correctly determine the alt based on the field langcode as per the code below.
  •   // Field Translation Support.
      if (field_has_translation_handler('file')) {
        $field_items = field_get_items('file', $file, 'field_file_image_alt_text', $langcode);
        foreach ($field_items as $field_item) {
          $element['#alt'] = $field_item['safe_value'];
        }
      }
    

    I was wondering if all of this makes sense or there something I clearly missed that negates the need to do all of this?

    Comments

    sylus’s picture

    Title: Media alt attributes only via the interface method. » Media alt attributes only work via the interface
    sylus’s picture

    StatusFileSize
    new4.13 KB
    sylus’s picture

    Issue summary: View changes
    sylus’s picture

    Issue summary: View changes
    sylus’s picture

    Minor styling adjustments.

    ParisLiakos’s picture

    Status: Active » Needs review
    ParisLiakos’s picture

    looks good and makes sense to me. i was just wondering if we could get a test here to make sure nothing breaks again in the future

    sylus’s picture

    StatusFileSize
    new4.18 KB

    Adding updated patch to use field_view_value.

    sylus’s picture

    StatusFileSize
    new4.18 KB

    Sorry bad patch.

    sylus’s picture

    StatusFileSize
    new4.28 KB

    Attaching updated patch for latest media dev after the split into sub modules.

    Status: Needs review » Needs work

    The last submitted patch, 10: media_alt_attributes_et-2129273-010.patch, failed testing.

    sylus’s picture

    StatusFileSize
    new4.37 KB

    Function isn't full form.

    sylus’s picture

    Status: Needs work » Needs review

    Status: Needs review » Needs work

    The last submitted patch, 12: media_alt_attributes_et-2129273-011.patch, failed testing.

    sylus’s picture

    StatusFileSize
    new4.81 KB

    Third time's the charm.

    sylus’s picture

    Status: Needs work » Needs review
    sylus’s picture

    StatusFileSize
    new4.9 KB

    Updating the filter code as was only parsing the first MEDIA_WYSIWYG_TOKEN_REGEX found causing a duplicate on rendered node.

    sylus’s picture

    StatusFileSize
    new4.9 KB

    Attaching updated patch for latest dev. I will post a patch with tests within a week.

    sylus’s picture

    StatusFileSize
    new4.9 KB

    Attaching updated patch to latest dev of media (b2ba2da)

    sylus’s picture

    StatusFileSize
    new4.81 KB

    Updating patch to fix: Cannot access empty property in field.attach.inc

    Status: Needs review » Needs work

    The last submitted patch, 20: media_alt_attributes_et-2129273-20.patch, failed testing.

    letrotteur’s picture

    I found it usefull to pass the langcode to the media_wysiwyg_token_to_markup alter

    It solved a issue with translation of field inside a fielded entity inserted into the wysiwyg.

    drupal_alter('media_wysiwyg_token_to_markup', $element, $tag_info, $settings, $langcode);

    letrotteur’s picture

    StatusFileSize
    new5.13 KB

    Made a patch with the addon...

    sylus’s picture

    Title: Media alt attributes only work via the interface » Pass langcode to media_wysiwyg_token_to_markup resolving media alt attributes in interface
    StatusFileSize
    new5.75 KB

    Thanks for the drupal_alter addon definitely makes sense to pass the langcode there as well.

    Attaching new patch that applies to latest dev and which also works for 2.0-beta1.

    sylus’s picture

    Status: Needs work » Needs review

    The last submitted patch, 23: media_alt_attributes_et_2129273-23.patch, failed testing.

    letrotteur’s picture

    Status: Needs review » Reviewed & tested by the community

    Works great for me with 2.0-beta1.

    Setting this To RTBC

    joseph.olstad’s picture

    Status: Reviewed & tested by the community » Fixed

    fixed in 7.x-2.x dev branch

    sylus’s picture

    Thanks a bunch!

    joseph.olstad’s picture

    There's a regression with this patch that was reported by @shelane who is not using i18n , in her environment the langcode is undefined.

    I've attempted to resolve the regression with a new patch in the followup issue. My strategy was to check for NULL langcode , I haven't tested the patch and I'm not yet sure if it'll pass simpletest as it's currently queued.

    Please followup in the new issue.
    #2822662: Media items added or edited no longer displaying addition fields

    Your assistance is appreciated, this patch was tagged as a beta3 release so it'd be nice to fix this asap and push out a new release with the fix to this regression.

    Status: Fixed » Closed (fixed)

    Automatically closed - issue fixed for 2 weeks with no activity.