2

I've created a custom field formatter widget for numeric fields. The formatter has A LOT of settings to customize the visual output of that number, to whatever designers/marketing wants (adding a lot of HTML markup, with[out] leading/trailing zeros, zeros as dash, one or two fractions, rounding, prefixes/infixes/suffixes,... ). Some of those settings are language-dependant though, so I'd like to enter different formatter settings in

/en/admin/structure/types/manage/MY_CONTENT_TYPE/display /de/admin/structure/types/manage/MY_CONTENT_TYPE/display /fr/admin/structure/types/manage/MY_CONTENT_TYPE/display

I've noticed that there is a $langcode parameter in
FormatterInterface::viewElements(FieldItemListInterface $items, $langcode)

but I don't know how to make
FormatterInterface.php FormatterInterface::settingsForm(array $form, FormStateInterface $form_state)
language-aware.

Is it possible at all to have language-specific settings in a field formatter? If yes, how to I store and retrieve those settings in the correct language?

4
+50
  1. Define a schema in your module config/schema/mymodule.schema.yml.

    Example from the date range module:

    core/modules/datetime_range/config/schema/datetime_range.schema.yml

    field.formatter.settings.daterange_default:
      type: field.formatter.settings.datetime_default
      label: 'Date range default display format settings'
      mapping:
        separator:
          type: label
          label: 'Separator'
          translation context: 'Date range separator'
    

    For more information see Block configuration form values are not translated

  2. You need an UI to translate, which is unfortunately not ready yet, see this issue:

    Entity view/form mode formatter/widget settings have no translation UI

    Until then you can edit the translated yaml file of an exported config in config/sync/language/[LanguageID] or install the patch from the issue.

  • My module already has a working schema file, used for global settings though (same options, but not "per-node-bundle-scope"). I've added a top level key field.formatter.settings.MY_MODULE of type: mapping with all my translatable labels and did a config export. But in which config files are the per-nodebundle-specific formatter settings supposed to be stored? I did not find any additional keys except my already existing (global) module config. – Hudri Sep 28 '18 at 13:30
  • The translated yaml files in language/[LanguageID] are named identically as the untranslated ones in the main sync folder. The main difference is that they only contain the translatable values. But why not fork your local site and try out the patch. You don't need the patch later in production when you deploy the config. – 4k4 Sep 28 '18 at 13:46
  • OK, got it by trial & error. Even after creating the schema, no config for the field formatter was exported in the 2nd lang. I manually copied core.entity_view_display.node.NODE_TYPE.VIEW_MODE.yml to config/lang/ISO2CODE, deleted all keys except those marked for translation and reimported the config. Then it worked. – Hudri Sep 28 '18 at 13:49

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.