JSON:API support with enhancers

Created on 3 January 2024, 6 months ago
Updated 3 February 2024, 5 months ago

Problem/Motivation

Using JSON:API I found myself stumbling on similar issue as #3026813: How output TimeField? β†’ , seeing the raw data in seconds being returned while a formatted value is desired.

Proposed resolution

Create JSON:API fieldenhancers to provide the same formatting options as the formatters for both the time and time range field.

✨ Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium andreasderijcke Antwerpen / Gent

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

Merge Requests

Comments & Activities

  • Issue created by @andreasderijcke
  • Issue was unassigned.
  • Status changed to Needs review 6 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 6 months ago
    11 pass
  • πŸ‡§πŸ‡ͺBelgium andreasderijcke Antwerpen / Gent

    Patch attached corresponding with the current state in the issue fork.
    This adds JSON:API enhancers for both the Time and Time Range field, reusing the existing formatting logic to format the output.

    To prevent increase of, and reduce already existing code redundancy, I've added a trait to contain the actual formatting logic and the format settings field description. Perhaps more shared stuff like setting default values can be moved here in time.

    Because of this trait, I'm not sure if this needs additional tests, at least not for the enhancers in the current version.

    I've not added the reverse processing, as it is not required for my case, my spare time is also limited and it opens a new box of questions of what input to support. If supporting this is desired at all. If not, then current version might be good to go.

    Just to give an idea what it looks like right now,

    1. Regular field formatting vs the enhancers:

    2. The enhancer settings:

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 5 months ago
    11 pass
  • Pipeline finished with Success
    5 months ago
    Total: 140s
    #87119
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 5 months ago
    11 pass
  • πŸ‡§πŸ‡ͺBelgium andreasderijcke Antwerpen / Gent

    Added option to include the raw value as well, as my case needs it and it seems useful for client applications anyway.
    The updated enhancer settings for timerang (similar for time):

    This return format of the enhancers is changed from string to keyed array though, having keys 'formatted' and 'raw'.
    If inclusion of the raw values is disable, the value of 'raw' is NULL.

    Patch time_field-3411980-3.patch corresponds with these changes and the current MR.

  • πŸ‡§πŸ‡ͺBelgium andreasderijcke Antwerpen / Gent
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 5 months ago
    11 pass
  • πŸ‡§πŸ‡ͺBelgium andreasderijcke Antwerpen / Gent

    Patch time_field-3411980-3.patch had missing empty line at the end, causing composer to fail to apply the patch.
    Use time_field-3411980-4.patch

Production build 0.69.0 2024