View Source Beacon.Content.PageField behaviour (Beacon v0.2.2)
Custom page fields for pages.
Each Beacon.Content.Page
have a default set of fields that
fits most cases for building pages for your sites,
but in some cases you need custom data to either help manage
those page in Beacon Admin or to display such data.
For example, you can add a field to store each blog post illustration, or a field to include tags, and so on.
Each page field will be:
- stored in the
page.extra
map field - displayed in Beacon LiveAdmin
- validated when the pages is saved or published
Example
defmodule MyApp.TagsField do
use Phoenix.Component
import Beacon.Web.CoreComponents
import Ecto.Changeset
@behaviour Beacon.Content.PageField
@impl true
def name, do: :tags
@impl true
def type, do: :string
@impl true
def render(assigns) do
~H"""
<.input type="text" label="Tags" field={@field} />
"""
end
@impl true
def changeset(data, attrs, _metadata) do
data
|> cast(attrs, [:tags])
|> validate_required([:tags])
end
end
Summary
Callbacks
Changeset used to validate and save data.
Default value for field. Defaults to nil
.
Field identifier. Must be unique per site.
Template to render the field on LiveAdmin.
Field type. Can be any value supported by Ecto Schema.
Callbacks
@callback changeset( data :: {Ecto.Changeset.data(), Ecto.Changeset.types()}, attrs :: %{required(String.t()) => any()}, metadata :: %{page_changeset: Ecto.Changeset.t()} ) :: Ecto.Changeset.t()
Changeset used to validate and save data.
@callback default() :: any()
Default value for field. Defaults to nil
.
@callback name() :: atom()
Field identifier. Must be unique per site.
@callback render(assigns :: Phoenix.LiveView.Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
Template to render the field on LiveAdmin.
@callback type() :: any()
Field type. Can be any value supported by Ecto Schema.