0

How can I get the field formatter settings inside a field plugin? The field plugin extends FieldItemBase and the corresponding field formatter plugin extends FormatterBase.

If there is no direct way, maybe I could use the entity_display.repository service. But to use the getViewDisplay I need to know which view mode is used. I don't know either how to get the current view mode in the field plugin.

The field plugin is a computed field that makes a calculation depending on some variables. I wanted to set theses variables as the formatter settings. But maybe this is the wrong approach. Maybe it would be better to use module settings for this purpose?

1
  • 1
    This is the classic XY problem - if you could edit the question to explain what you're trying to accomplish, a better way could probably be suggested
    – Clive
    Commented May 10 at 20:13

1 Answer 1

2

You wouldn't. The field definition (extending FieldItemBase) is the data definition, without any representation on how that data should be displayed. Fields are are not meant to be dependent upon a single formatter, but rather can have any applicable formatter used for the field. This is a purposeful disambiguation. Formatters provide different ways of displaying the field data. So if you are trying to get formatter info in the field definition, it's backwards. You should only be getting field data from the formatter, not the other way around.

Based on your update, you want instead to create a computed field. You can do this by creating a custom plugin, or using the Computed Field module.

2
  • I edit my question. Where should I perform a calculation for a computed field and where/how can I store variables for the calculation if not in the formatter settings?
    – Mike
    Commented May 11 at 13:33
  • nuez.io/posts/computed-fields-drupal-8
    – Jaypan
    Commented May 11 at 14:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.