customentity

package
v1.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

README

Custom Entity Admin Controllers

This package provides admin UI controllers for managing custom entities in the CMS.

Features

  • List View: Paginated table view with search and filtering
  • Create Form: Modal form for creating new entities
  • Edit Form: Modal form for editing existing entities
  • Delete Confirmation: Safe deletion with confirmation dialog
  • HTMX Integration: Seamless modal interactions without page reloads
  • Bootstrap 5 UI: Consistent styling with the rest of the admin

Controllers

EntityListController

Displays a paginated list of entities with actions for create, edit, and delete.

EntityCreateController

Handles entity creation with a modal form.

EntityEditController

Handles entity editing with a pre-populated modal form.

EntityDeleteController

Handles entity deletion with a confirmation dialog.

Usage

1. Define Your Custom Entity
productDef := cmsstore.CustomEntityDefinition{
    Type:      "product",
    TypeLabel: "Product",
    Group:     "Shop",
    Attributes: []cmsstore.CustomAttributeDefinition{
        {Name: "title", Type: "string", Label: "Title", Required: true},
        {Name: "price", Type: "float", Label: "Price", Required: true},
        {Name: "stock", Type: "int", Label: "Stock Quantity"},
        {Name: "description", Type: "string", Label: "Description"},
    },
}
2. Initialize the Store with Custom Entities
store, err := cmsstore.NewStore(cmsstore.NewStoreOptions{
    DB:                    db,
    BlockTableName:        "cms_block",
    PageTableName:         "cms_page",
    SiteTableName:         "cms_site",
    TemplateTableName:     "cms_template",
    AutomigrateEnabled:    true,
    CustomEntitiesEnabled: true,
    CustomEntityDefinitions: []cmsstore.CustomEntityDefinition{productDef},
})
3. Create Controllers
import "github.com/dracory/cmsstore/admin/customentity"

// Create UI interface implementation
type AdminUI struct {
    store  cmsstore.StoreInterface
    layout hb.TagInterface
    logger any
}

func (ui *AdminUI) Store() cmsstore.StoreInterface { return ui.store }
func (ui *AdminUI) Layout() hb.TagInterface { return ui.layout }
func (ui *AdminUI) Logger() any { return ui.logger }

// Initialize controllers
ui := &AdminUI{store: store, layout: myLayout, logger: myLogger}

listController := customentity.NewEntityListController(ui, productDef)
createController := customentity.NewEntityCreateController(ui, productDef)
editController := customentity.NewEntityEditController(ui, productDef)
deleteController := customentity.NewEntityDeleteController(ui, productDef)
4. Register Routes
// List/Index
http.HandleFunc("/admin/custom-entity/product", listController.Handler)

// Create
http.HandleFunc("/admin/custom-entity/product/create", createController.Handler)

// Edit
http.HandleFunc("/admin/custom-entity/product/edit", editController.Handler)

// Delete
http.HandleFunc("/admin/custom-entity/product/delete", deleteController.Handler)

Add a link to your admin navigation:

<a href="/admin/custom-entity/product" class="nav-link">
    <i class="bi bi-box"></i> Products
</a>

Route Pattern

For each custom entity type, the following routes are created:

  • GET /admin/custom-entity/{type} - List view
  • GET /admin/custom-entity/{type}/create - Create form (modal)
  • POST /admin/custom-entity/{type}/create - Process creation
  • GET /admin/custom-entity/{type}/edit?id={id} - Edit form (modal)
  • POST /admin/custom-entity/{type}/edit?id={id} - Process update
  • GET /admin/custom-entity/{type}/delete?id={id} - Delete confirmation (modal)
  • POST /admin/custom-entity/{type}/delete?id={id} - Process deletion

Attribute Types

Supported attribute types and their form field mappings:

Type Form Field Description
string Text input Single-line text
int Number input Integer values
float Number input (with decimals) Decimal values
bool Select dropdown True/false values
json Textarea JSON data

Customization

Custom Field Types

You can extend the getFieldType() method in the controllers to support additional field types.

Custom Validation

Add validation logic in the handleSubmit() methods before calling Create() or Update().

Custom Actions

Add additional buttons in the table by modifying the buildActions() method in EntityListController.

Example: Complete Setup

See example_admin_setup.go for a complete working example of setting up custom entity admin controllers.

Requirements

  • Bootstrap 5 (for styling)
  • HTMX (for modal interactions)
  • Bootstrap Icons (for icons)

These should be included in your admin layout template.

Notes

  • All modals use HTMX for seamless interactions
  • Forms use POST method for submissions
  • Entities are soft-deleted (moved to trash)
  • Pagination shows up to 20 entities per page
  • Entity IDs are displayed as code for easy copying

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleAdminSetup

func ExampleAdminSetup()

ExampleAdminSetup demonstrates how to set up custom entity admin controllers

func ExampleMultipleEntityTypes

func ExampleMultipleEntityTypes()

ExampleMultipleEntityTypes shows how to set up multiple custom entity types

Types

type EntityCreateController

type EntityCreateController struct {
	// contains filtered or unexported fields
}

EntityCreateController handles entity creation

func NewEntityCreateController

func NewEntityCreateController(ui UiInterface, definition cmsstore.CustomEntityDefinition) *EntityCreateController

NewEntityCreateController creates a new create controller

func (*EntityCreateController) Handler

Handler handles both GET (show form) and POST (process form)

type EntityDeleteController

type EntityDeleteController struct {
	// contains filtered or unexported fields
}

EntityDeleteController handles entity deletion

func NewEntityDeleteController

func NewEntityDeleteController(ui UiInterface, definition cmsstore.CustomEntityDefinition) *EntityDeleteController

NewEntityDeleteController creates a new delete controller

func (*EntityDeleteController) Handler

Handler handles both GET (show confirmation) and POST (process deletion)

type EntityEditController

type EntityEditController struct {
	// contains filtered or unexported fields
}

EntityEditController handles entity editing

func NewEntityEditController

func NewEntityEditController(ui UiInterface, definition cmsstore.CustomEntityDefinition) *EntityEditController

NewEntityEditController creates a new edit controller

func (*EntityEditController) Handler

Handler handles both GET (show form) and POST (process form)

type EntityListController

type EntityListController struct {
	// contains filtered or unexported fields
}

EntityListController handles the list view for a custom entity type

func NewEntityListController

func NewEntityListController(ui UiInterface, definition cmsstore.CustomEntityDefinition) *EntityListController

NewEntityListController creates a new list controller

func (*EntityListController) Handler

Handler renders the entity list page

type ExampleUI

type ExampleUI struct {
	// contains filtered or unexported fields
}

ExampleUI implements the UiInterface for custom entity controllers

func (*ExampleUI) Layout

func (ui *ExampleUI) Layout() hb.TagInterface

func (*ExampleUI) Logger

func (ui *ExampleUI) Logger() any

func (*ExampleUI) Store

func (ui *ExampleUI) Store() cmsstore.StoreInterface

type UiInterface

type UiInterface interface {
	Store() cmsstore.StoreInterface
	Layout() hb.TagInterface
	Logger() any
}

UiInterface defines the interface for custom entity admin UI

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL