Skip to content

Add support for computed values#6179

Merged
jasonvarga merged 86 commits into3.3from
feature/computed-field-visibility
Oct 19, 2022
Merged

Add support for computed values#6179
jasonvarga merged 86 commits into3.3from
feature/computed-field-visibility

Conversation

@jesseleite
Copy link
Copy Markdown
Contributor

@jesseleite jesseleite commented Jun 8, 2022

The idea of this PR was brought about by a support email convo. It adds support for computed values, with ability to show these as virtual fields on listings and publish forms, while preventing these values from being saved to data. This allows the user to display data (ie. from external database or API) that exists outside of Statamic (ie. a User's balance from a 3rd party service).

Usage:

User::computed('balance', function ($user, $value) {
    return (new SomeThirdPartyApi($user))->balance();
});

Collection::computed('articles', 'word_count', function ($entry, $value) {
    return word_count($entry);
});

And $value would give you the raw attribute from the yaml/database data, otherwise it'll return null just like Laravel's accessors do.

Todo:

  • Add new visibility: computed field config.
  • Ensure computed fields are read-only on publish forms.
  • Filter out computed fields when calling $fields->values() or $fields->process()->values().
  • Do not filter out computed fields when calling $fields->preProcess()->values() for create/edit forms.
  • Ensure computed fields are still viewable on index/listings via $field->preProcessIndex().
  • Ability to set computed field logic in service provider.
  • Add $entry->computedData() (an array of all the callbacks evaluated).
  • Ensure you can get computed values via $entry->value() and $entry->values().
  • Merge computedData() in EntriesController@extractFromFields() around line 408?
  • Test coverage.
  • Documentation PR.

Closes #6177
Closes #2226

@jesseleite jesseleite changed the base branch from 3.3 to feature/configurable-field-visibility June 8, 2022 19:21
@jesseleite jesseleite marked this pull request as draft June 8, 2022 19:22
@jesseleite jesseleite changed the title Computed field visibility Computed fields Jun 9, 2022
@jesseleite jesseleite changed the title Computed fields Add support for computed field values Jul 27, 2022
@jesseleite jesseleite marked this pull request as ready for review July 27, 2022 23:20
@jesseleite jesseleite changed the title Add support for computed field values Add support for computed values Jul 28, 2022
@jasonvarga jasonvarga merged commit 69312d8 into 3.3 Oct 19, 2022
@jasonvarga jasonvarga deleted the feature/computed-field-visibility branch October 19, 2022 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add virtual column handling to fieldtype config? Support View Model data outside of the view in a collection loop

2 participants