Problem/Motivation

Computed base field definitions are not picked up by Json API.

Steps to reproduce:

1. Add a BaseFieldDefinition to nodes with hook_entity_base_field_info_alter with setComputed(TRUE) and setClass(computedField::class) and let the class return something computed. Follow the example of ComputedTestFieldItemList.
2. Create a node of type article.
3. Go to jsonapi/node/article.

The field is there but it returns null.

Proposed resolution

-

CommentFileSizeAuthor
#3 computed_field_values-2912116-3.patch1.82 KBnuez

Comments

nuez created an issue. See original summary.

e0ipso’s picture

You can see an example of a computed field working in the jsonapi module itself. It's the URL download field for file entities.

Maybe that'll help find the disconnect.

nuez’s picture

Thank you for the tip:

The FileDownloadUrl class actually works, although not entirely: because of the 'vfs' scheme, the field_create_url() returns FALSE, and the computed URL stays empty in the test.

In the patch I've added the test for the computed field, and changed the FileDownloadUrl class so it returns the $uri when the scheme is 'vfs'. I guess this should actually be fixed in the field_create_url() method in core, but that might not be a viable road.

The reason why the ComputedTestFieldItemList fails is because the isEmpty() returns TRUE and the computation never happens.

Conclusion: documentation should be improved about how to create computed fields. Basically the isEmpty() method should always return FALSE when computed to force the computation to be executed.

I've added the related issue.

nuez’s picture

Status: Active » Needs review
e0ipso’s picture

Title: Computed field values are not picked up » Support for VFS files
Status: Needs review » Fixed

I made some adjustments on commit. See the actual commit for more details.

  • e0ipso committed aa3b260 on 8.x-1.x authored by nuez
    fix(File): Support for VFS files (#2912116 by e0ipso, nuez)
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.