PhoenixKitWeb.AdminEditHelper (phoenix_kit v2.22.2)

Copy Markdown View Source

Public API for an "Edit" link from a public page into its matching admin page.

A host viewing a public page (a host LiveView, or a module's own public controller/LiveView) calls assign_admin_edit/3 to declare where that page's admin counterpart lives. The visitor gets nothing unless they can reach the admin area at all, and — when the caller names a :permission module key — unless they also hold access to that specific module, so an admin scoped to Publishing never sees an Edit link into Ecommerce.

Renders via PhoenixKitWeb.Components.Core.AdminEditLink.admin_edit_link/1, which reads the :admin_edit_url/:admin_edit_label assigns this module sets and renders nothing when no URL was assigned.

The path passed to assign_admin_edit/3 is the final URL — when it targets a PhoenixKit-routed admin page (as both examples below do), run it through PhoenixKit.Utils.Routes.path/1 first so it still resolves under a mount prefix, a locale segment, or a renamed admin_path segment.

Host LiveView example

def handle_params(%{"slug" => slug}, _uri, socket) do
  post = Blog.get_post_by_slug!(slug)

  socket =
    AdminEditHelper.assign_admin_edit(
      socket,
      PhoenixKit.Utils.Routes.path("/admin/posts/#{post.id}/edit")
    )

  {:noreply, assign(socket, :post, post)}
end

Module controller example

A module's own public controller can declare the same edit target, guarding the call the way phoenix_kit_publishing guards every optional core API it depends on — so it still compiles and runs against a core release that predates this helper:

if Code.ensure_loaded?(PhoenixKitWeb.AdminEditHelper) and
     function_exported?(PhoenixKitWeb.AdminEditHelper, :assign_admin_edit, 3) do
  conn =
    PhoenixKitWeb.AdminEditHelper.assign_admin_edit(
      conn,
      PhoenixKit.Utils.Routes.path("/admin/publishing/posts/#{post.id}/edit"),
      permission: "publishing"
    )
end

Summary

Functions

Assigns :admin_edit_url and :admin_edit_label when the current scope may see the edit link; otherwise returns the conn/socket unchanged (the assigns are absent, not nil).

Functions

assign_admin_edit(conn_or_socket, path, label_or_opts \\ "Edit")

Assigns :admin_edit_url and :admin_edit_label when the current scope may see the edit link; otherwise returns the conn/socket unchanged (the assigns are absent, not nil).

label_or_opts (default "Edit", gettext-translated) is either:

  • a plain string — used as the label as-is; every existing caller keeps working unchanged.
  • a keyword list:
    • :label — the link's label (default: gettext "Edit").
    • :permission — a module permission key (the same string used as a module's permission_metadata/0 :key or a PhoenixKit.Dashboard.Tab :permission). When given, the scope must also hold Scope.has_module_access?/2 for that key.

In all cases the scope must be non-nil and Scope.can_access_admin_area?/1 must be true.