View Source Beacon.MediaLibrary.AssetField behaviour (Beacon v0.2.1)

Custom asset fields for assets.

Each Beacon.MediaLibrary.Asset has a default set of fields that fits most cases for assets for your sites, but in some cases you need custom data to either help manage those assets in Beacon Admin or to display such data.

Each asset field will be:

  • stored in the asset.extra map field
  • displayed in Beacon Admin
  • validated when the assets are saved or published

Example

defmodule MyApp.AltTextField do
  use Phoenix.Component
  import Beacon.Web.CoreComponents
  import Ecto.Changeset

  @behaviour Beacon.MediaLibrary.AssetField

  @impl true
  def name, do: :alt

  @impl true
  def type, do: :string

  @impl true
  def render_input(assigns) do
    ~H"""
    <.input type="text" label="Alt Text" field={@field} />
    """
  end

  @impl true
  def render_show(assigns) do
    ~H"""
    <.input type="text" label="Alt Text" value={@value} />
    """
  end

  @impl true
  def changeset(data, attrs) do
    data
    |> cast(attrs, [:alt])
    |> validate_required([:alt])
  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 form field on Admin.

Template to render the show field on Admin.

Field type. Can be any value supported by Ecto Schema.

Callbacks

Link to this callback

changeset(data, attrs, metadata)

View Source
@callback changeset(
  data :: {Ecto.Changeset.data(), Ecto.Changeset.types()},
  attrs :: %{required(String.t()) => any()},
  metadata :: %{asset_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_input(assigns :: Phoenix.LiveView.Socket.assigns()) ::
  Phoenix.LiveView.Rendered.t()

Template to render the form field on Admin.

@callback render_show(assigns :: Phoenix.LiveView.Socket.assigns()) ::
  Phoenix.LiveView.Rendered.t()

Template to render the show field on Admin.

@callback type() :: any()

Field type. Can be any value supported by Ecto Schema.