Allow to customize the key for fields in the response

Created on 9 April 2024, 8 months ago

Problem/Motivation

Instead of returning the field name (often field_*) in the response as key, you might want to set a prettier name or somehow customize the key.

Instead of the following:

"transform_mode": "default",
"field_last_activity": "tir, 09/04/2024 - 14:06",
"uid": {
  "type": "entity",
  "entity_type": "user",
[..]

Customization would allow the following:

"transform_mode": "default",
"last_activity": "tir, 09/04/2024 - 14:06",
"creator": {
  "type": "entity",
  "entity_type": "user",
[..]

Proposed resolution

We could add a textfield in the settings for allowing customization. See image for visual.

Something like:

public function settingsForm(array $form, FormStateInterface $form_state) {
    return [
      'key' => [
        '#type' => 'textfield',
        '#title' => $this->t('Key for field in response'),
        '#default_value' => $this->getSetting('key'),
        '#required' => FALSE,
      ],
    ];
  }

And then figure out how to use it in Transformer.php.

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇩🇰Denmark nicklasmf

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024