Closed (won't fix)
Project:
Double Field
Version:
4.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
19 Jun 2017 at 15:05 UTC
Updated:
9 Sep 2021 at 12:42 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
dagmarComment #3
dagmarI guess my question is how can I get the schema from the field so I only have to change the keys of the array.
Comment #4
dagmarOh, I think I found the way. Here is the new patch.
Comment #5
dagmarComment #6
chi commentedI would like someone with JSON API experience make a review.
Comment #7
wim leersI am … very confused by this code :D
This patch is achieving "better keys" by subclassing
ResourceFieldEnhancerBase, which is a JSON API extras addition. But JSON API Extras designed to override things.Why not fix the root cause? You can do that by writing a normalizer (see https://www.drupal.org/docs/8/api/serialization-api/serialization-api-ov...). Then it would work in core REST, and in JSON API — even without installing JSON API Extras! See
\Drupal\serialization\Normalizer\TimestampItemNormalizerand\Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizerfor examples. More of these field type-specific normalizers are being worked on, and will be landing in the coming days, weeks and months in Drupal core :)Comment #8
dagmar@Wim Leers. That's a great idea! The only problem I see here is where to define those mappings. Double field currently doesn't have a place to do that. That's the reason ResourceFieldEnhancer seemed a good alternative.
@Chi, Do you think we can add an extra pair of settings to define identifiers for 'first' and 'second' replacements for double fields. Or maybe you already know a some settings we could use for this task.
Comment #9
wim leersOh… well you could add new optional settings to Double Field?
Comment #10
chi commentedCan this be done by setting supportedInterfaceOrClass property of the normalizer?
Comment #11
wim leersYes.
Comment #12
chi commentedThe approach with normalizer seems super simple.
double_field.services.yml
src/Normalizer/DoubleFieldItemNormalizer.php
Comment #13
chi commentedUnfortunately normalizers are not configurable and Double field has no appropriate settings to make use. Introducing additional configuration to field instance for such a specific case as renaming properties in JSON output does not seem a good idea.
Comment #14
dagmarWell, that's the main reason why this lives into a jsonapi_extra plugin. The plugin offers the configurations to store those values.
Comment #15
dagmarI created this issue that is related #2892193: Custom normalizers are not respected
Comment #16
e0ipsoThis could probably be a Field Enhancer in JSON API extras.
Comment #17
wim leersBut if it's implemented at the normalizer level, then that will only work in the JSON API module (or only in the REST module), but it will not work in https://www.drupal.org/project/graphql nor in https://www.drupal.org/project/openapi.
Ideally,
DoubleField::propertyDefinitions()would not usefirstandsecondas property names, but it'd use configurable, named properties. That's only possible if those property names were defined in the accompanying field storage definition (FieldStorageDefinitionInterface).If it's possible to store a "subfield type" in the field storage definition, then surely it's also possible to store a name for them? Then
could be modified to
(And for BC, you can generate the defaults to be
firstandsecond, so that existing Double Fields continue to work as they do today.)That would then make compatible with all API-First modules.
Comment #18
wim leersComment #19
chi commentedAnyone who has time to implement this as described in #17 feel free to reopen the issue.