2

I created a media image field in my custom entity content:

$fields['image'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Promotion image'))
  ->setDescription(t('Recommended image size is 1600x900 pixels.'))
  ->setRevisionable(TRUE)
  ->setTranslatable(TRUE)
  ->setRequired(FALSE)
  ->setSetting('target_type', 'media')
  ->setSetting('handler', 'default:media')
  ->setSettings([
    'handler_settings' => [
      'target_bundles' => [
        'image' => 'image',
      ],
      'sort' => [
        'field' => '_none',
      ],
      'auto_create' => FALSE,
      'auto_create_bundle' => '',
    ],
  ])
  ->setCardinality(1)
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE)
  ->setDisplayOptions('form', [
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_display' => 'rendered_entity',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'open' => TRUE,
      'selection_mode' => 'selection_append',
      'field_widget_display_settings' => [
        'view_mode' => 'default',
      ],
      'field_widget_replace' => FALSE,
    ],
    'type' => 'entity_browser_entity_reference',
    'weight' => 3,
  ])
  ->setDisplayOptions('view', [
    'label' => 'hidden',
    'settings' => [
      'target_type' => 'media',
    ],
    'weight' => 3,
  ]);

Even though the entity itself, and the field are set to translatable, the ALT and TITLE attributes of the image are not translated.

If I create this field manually in the UI, it works as intended, and gets translated. How could I define this field to be translatable programmatically?

3
  • What are you trying to do? Translating the reference field means you need to replace the media item with a translated version, but it looks more like you want to keep the media item and translate ALT and TITLE in the media entity.
    – 4uk4
    Commented Mar 3, 2019 at 10:56
  • Good catch, thanks. I intend to make the media image translatable as a whole entity, the image, the ALT and TITLE attributes... Current code example doesn't do that though. This is as much as I could gather from the config files when I manually added and configured the media field as such. Seems i'm missing some more. Any ideas?
    – osman
    Commented Mar 3, 2019 at 17:55
  • 1
    I wouldn't make the reference field translatable then. This can be quite confusing if you have already configured the media type including the image field with the file, title and alt property as translatable.
    – 4uk4
    Commented Mar 3, 2019 at 18:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.