Problem/Motivation
Many sites are using the JSON:API Extras module to refine the API that JSON:API exposes by default. One such refinement is the ability to "alias" or disable resource type fields. To do that, JSON:API Extras is forced to depend on potentially unstable @internal
PHP code. Also, custom module developers aren't able to write logic that could automatically alias resource type fields according to rules that suit their application (like automatically alias field_foo_bar
to fooBar
).
Proposed resolution
Add the following methods to \Drupal\jsonapi\ResourceType\ResourceTypeBuildEvent
:
getFields() []ResourceTypeField
setPublicFieldName(ResourceTypeField $field, $public_field_name): void
disableField(ResourceTypeField $field): void
Remaining tasks
#3037039: Create a public API for indicating resource types should not be exposed
User interface changes
None.
API changes
There will be a new methods on an event.
Data model changes
None.
Release notes snippet
JSON:API resource type fields can now be aliased and disabled programmatically. See the change record (UPDATE THIS LINKED CR, IF POSSIBLE).
Comment | File | Size | Author |
---|---|---|---|
#8 | 3085035-8-combined.patch | 18.49 KB | gabesullice |
#8 | 3085035-8.patch | 11.56 KB | gabesullice |
#8 | interdiff.txt | 1.3 KB | gabesullice |
#4 | 3085035-4-combined.patch | 17.78 KB | gabesullice |
#4 | 3085035-4.patch | 10.98 KB | gabesullice |
Comments
Comment #2
gabesulliceComment #3
gabesulliceHere's my initial implementation. It needs tests and will not apply without #3037039: Create a public API for indicating resource types should not be exposed
Comment #4
gabesulliceComment #5
gabesulliceIf this lands with #3037039: Create a public API for indicating resource types should not be exposed then we should update that issue's change record rather than creating two CRs.
Comment #6
gabesulliceComment #7
Wim LeersLooking great!
🤓🤔 I think we can remove
array|
here, the second part is sufficient?👍
🤓 Missing
@return
.🤓 Docblock is wrong. (This is not about public field name, but about disabling.)
🤩 Woah, elegant, fascinating.
🤓 Übernit: I'm surprised these are arrays instead of strings.
Comment #8
gabesullicegetSubscribedEvents
. The idea is that you can do['methodName', $priority]
, but priority is optional, so it ends up['string']
.Note: this was rerolled on top of #3037039-58: Create a public API for indicating resource types should not be exposed
Comment #9
Wim LeersI think this is RTBC :) But blocked on that other issue landing first, so
it is for now.Good job!
Comment #10
gabesullice#3037039: Create a public API for indicating resource types should not be exposed landed!
Comment #11
Wim Leers3085035-8.patch
still applies cleanly, it just needed a re-test 😊Comment #12
e0ipsoThis looks great! Thanks for working on this. I can't wait to make use of this in JSON:API Extras.
Comment #13
jibranJust a couple of questions.
Do we need to update the old change record? https://www.drupal.org/node/3079797
Just saw #5Do we need to default $fields to empty array?
Comment #15
effulgentsia CreditAttribution: effulgentsia at Acquia commentedAdding reviewer credit
Comment #19
effulgentsia CreditAttribution: effulgentsia at Acquia commentedI don't think so. As this is a new class that hasn't been in a tagged release of Drupal yet, we don't need to do it for BC reasons. And I don't think we want to support an API where people don't pass this in explicitly. But if I'm wrong on that, please file a followup.
Meanwhile, pushed this to 8.8.x and above.
Setting to NW for updating the CR from #3037039: Create a public API for indicating resource types should not be exposed.
Also, tagging for both release notes and highlights, because #3037039: Create a public API for indicating resource types should not be exposed has both those tags. It could potentially be the same release note and/or highlight entry for both issues, so long as it's worded to encompass both.
Comment #20
gabesulliceI updated the CR some time ago, but forget to update this issue's metadata. Thanks go to @xjm for a gentle reminder :)