feat(16.2-01): add the pact plugin field type API
- pact.HasFormFieldTypes and pact.FormFieldType: a plugin registers admin form field types under a short alias, with element, assets, options and CheckOptions/Decode/Load/ListValue/Upload funcs - FormFieldValue, FormFieldDecoded (value plus notices), FormFieldNotice, FormFieldUpload, FormFieldUploadResult and FormFieldError - ListValue takes the request context: func(ctx, value) string
This commit is contained in:
@@ -3,8 +3,10 @@ package pact
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
@@ -368,6 +370,126 @@ type AdminPartialData interface {
|
||||
PartialData(ctx context.Context, name string, record any) (any, error)
|
||||
}
|
||||
|
||||
// FormFieldType is one admin form field type a plugin registers under a
|
||||
// short global alias, used as `type:` in any controller's fields.yaml. The
|
||||
// admin framework owns the HTTP routes, the CSRF check, authentication,
|
||||
// permissions, record scope, locale storage, notice localization and the
|
||||
// skip of a value the administrator did not change; the funcs only carry the
|
||||
// type's own logic.
|
||||
//
|
||||
// Name is the alias: lowercase ASCII letters and digits starting with a
|
||||
// letter, unique across plugins and never a built-in field type. Element is
|
||||
// the custom-element tag the admin renders for the field; it must start with
|
||||
// the owning plugin's prefix (vendor.plugin -> "vendor-plugin-") and is
|
||||
// defined by one of Scripts. Translatable makes the value a map of locale to
|
||||
// string, stored through the framework's translation writer: the default
|
||||
// locale in the host column, the others as translations. Scripts (.js, .mjs)
|
||||
// and Styles (.css) are paths under assets/ in the plugin's AdminFS; the admin
|
||||
// loads them once on every admin page. Options are the type's own fields.yaml
|
||||
// keys, next to the general field keys.
|
||||
//
|
||||
// CheckOptions, when set, checks a field's options at boot; an error fails
|
||||
// boot naming the field. Decode is required: it turns a submitted value into
|
||||
// the value to store and may report what it changed as notices; a
|
||||
// *FormFieldError refuses a value the type cannot accept at all. Load, when
|
||||
// set, turns a stored value into the value the element receives. ListValue,
|
||||
// when set, returns the text a list column of this type shows. Upload, when
|
||||
// set, stores a file the element uploads and returns its URL.
|
||||
type FormFieldType struct {
|
||||
Name string
|
||||
Element string
|
||||
Translatable bool
|
||||
Scripts []string
|
||||
Styles []string
|
||||
Options []string
|
||||
CheckOptions func(options map[string]any) error `json:"-"`
|
||||
Decode func(ctx context.Context, in FormFieldValue) (FormFieldDecoded, error) `json:"-"`
|
||||
Load func(ctx context.Context, in FormFieldValue) (any, error) `json:"-"`
|
||||
ListValue func(ctx context.Context, value any) string `json:"-"`
|
||||
Upload func(ctx context.Context, in FormFieldUpload) (FormFieldUploadResult, error) `json:"-"`
|
||||
}
|
||||
|
||||
// FormFieldValue is what the framework hands Decode and Load. DB is the save
|
||||
// transaction (Decode) or the read transaction (Load). Record is the loaded
|
||||
// record on update and the new model on create. Options are the field's
|
||||
// options from fields.yaml. Locale is the content locale of the value for a
|
||||
// translatable type and empty otherwise; Default is true when the value lives
|
||||
// in the host column (always true for a type that is not translatable). Value
|
||||
// is the submitted value (Decode) or the stored value (Load); it is a string
|
||||
// for a translatable type and any JSON value otherwise. Translated, when set,
|
||||
// reads another attribute of Record in Locale exactly, without fallback; it is
|
||||
// nil for a type that is not translatable and when no translation writer is
|
||||
// published.
|
||||
type FormFieldValue struct {
|
||||
DB *gorm.DB
|
||||
Record any
|
||||
Field string
|
||||
Options map[string]any
|
||||
Locale string
|
||||
Default bool
|
||||
Value any
|
||||
Translated func(attribute string) (string, bool, error)
|
||||
}
|
||||
|
||||
// FormFieldDecoded is Decode's answer. Value is stored: a string for a
|
||||
// translatable type. Notices are non-blocking messages about the accepted
|
||||
// value, such as what a sanitizer removed; a save whose Decode calls return
|
||||
// no notice answers exactly as a save without plugin field types.
|
||||
type FormFieldDecoded struct {
|
||||
Value any
|
||||
Notices []FormFieldNotice
|
||||
}
|
||||
|
||||
// FormFieldNotice is one message for the administrator about a saved value.
|
||||
// The framework localizes Message (a phrase key or text) in the request
|
||||
// locale with Params; a Count above zero selects the plural form and sets
|
||||
// :count.
|
||||
type FormFieldNotice struct {
|
||||
Message string
|
||||
Params map[string]string
|
||||
Count int
|
||||
}
|
||||
|
||||
// FormFieldUpload is what the framework hands Upload. DB is the upload's
|
||||
// transaction. Record is the record loaded through the controller's
|
||||
// FormExtendQuery scope, nil on the create form. Options are the field's
|
||||
// options. Body is the file's content, already capped by the upload body
|
||||
// limit.
|
||||
type FormFieldUpload struct {
|
||||
DB *gorm.DB
|
||||
Record any
|
||||
Field string
|
||||
Options map[string]any
|
||||
Filename string
|
||||
Body io.Reader
|
||||
}
|
||||
|
||||
// FormFieldUploadResult is Upload's answer: the URL the element shows.
|
||||
type FormFieldUploadResult struct {
|
||||
URL string
|
||||
}
|
||||
|
||||
// FormFieldError is a value a field type refuses. Messages are phrase keys
|
||||
// or text, localized by the framework and shown on the field; the save or
|
||||
// upload writes nothing.
|
||||
type FormFieldError struct {
|
||||
Messages []string
|
||||
}
|
||||
|
||||
// Error joins the messages with "; ".
|
||||
func (e *FormFieldError) Error() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(e.Messages, "; ")
|
||||
}
|
||||
|
||||
// HasFormFieldTypes is implemented by plugins (not admin controllers) that
|
||||
// register admin form field types, like HasAdminControllers.
|
||||
type HasFormFieldTypes interface {
|
||||
FormFieldTypes() []FormFieldType
|
||||
}
|
||||
|
||||
// Permission is one registerPermissions() entry.
|
||||
type Permission struct {
|
||||
Code string
|
||||
|
||||
Reference in New Issue
Block a user