Admin Pages
An extension can add pages to the admin, such as a list of orders with a dialog for each one. They sit under /_cwa beside the built-in pages, in the same layout, behind the same sign-in check. This page adds an orders list and an order dialog, built from the parts the built-in lists use.
Registering an Admin Page
Each entry in adminPages is one page:
// cwa-shop/module.ts, in register({ … })
adminPages: [
{
path: 'shop-orders', file: resolve('./admin/orders.vue'), item: resolve('./admin/order.vue'), menu: { label: 'Orders' }, },
{ path: 'shop-reports', file: resolve('./admin/reports.vue'), role: 'ROLE_SHOP_ACCOUNTS', menu: { label: 'Reports' } },
],
| Option | Required | What it does |
|---|---|---|
path | Yes | The path under /_cwa: shop-orders is /_cwa/shop-orders. It can have more than one segment |
file | Yes | An absolute path to the page's Vue file |
item | No | An absolute path to a dialog for one item, opened over the list at /_cwa/<path>/<iri>. See The Item Dialog |
role | No | The role a user needs to open the page. Default ROLE_ADMIN, which a content admin with only ROLE_CMS_ADMIN doesn't have |
menu | No | { label } adds the page to the admin menu, under Extensions |
Each page's route is named _cwa- followed by its path with / turned into -: shop-orders is _cwa-shop-orders, and its item dialog is _cwa-shop-orders-iri.
users, pages, layouts, routes, data, settings and orphaned are taken, so /_cwa/users/shop fails the build. Starting the path with the extension's own name, as shop-orders does, keeps clear of them and of other extensions.Like every page under /_cwa, an extension's admin pages render only in the browser.
Who Can Open It
A signed-out visitor is sent to /login and back after signing in. A signed-in user without the page's role is sent to /. The menu entry is shown only to users with the role.
role must start with ROLE_ and can't be ROLE_USER, which every signed-in user has. A role the user reaches through the role hierarchy counts, because /me lists every role a user reaches. See Admin Roles › The Role Hierarchy.
A role other than ROLE_ADMIN appears in the admin's user dialog as a toggle, named after the menu labels of the pages that need it, so a user admin can grant it (see CWA Layer › Admin Panel › Admin Routes).
The List Page
<CwaAdminList> is the built-in lists' page: a heading, the search and sort row, the list with its loading, empty and error states, pages of 20, and the outlet the item dialog opens in. You give it the endpoint and render each row:
<!-- cwa-shop/admin/orders.vue -->
<template>
<CwaAdminList
title="Orders"
fetch-url="/shop/orders"
noun="orders"
:columns="columns"
:order-options="orderOptions"
search-label="Search orders"
>
<template #item="{ data, link }"> <CwaAdminListRow :to="link" :title="data.number" :sub="data.customerEmail"> <CwaAdminListCell>
<CwaUiBadge :tone="data.state === 'paid' ? 'success' : 'neutral'">
{{ data.state }}
</CwaUiBadge>
</CwaAdminListCell>
<CwaAdminListCell align="end">
{{ data.total }}
</CwaAdminListCell>
</CwaAdminListRow>
</template>
</CwaAdminList>
</template>
<script setup lang="ts">
const columns = [
{ label: 'Order' },
{ label: 'State', width: '8rem' },
{ label: 'Total', width: '7rem', align: 'end' as const },
]
const orderOptions = [
{ label: 'Newest first', value: { createdAt: 'desc' } },
{ label: 'Highest total', value: { total: 'desc' } },
]
</script>
The extension names the routes for you: the list page is _cwa- plus its path (_cwa-shop-orders), and its item dialog is that name plus -iri. The list builds each row's link from them, and gives the item dialog its route key, so an extension's admin pages need no definePageMeta for routing.
| Prop | Required | What it does |
|---|---|---|
title | Yes | The page heading and the browser tab's title |
fetch-url | Yes | The collection endpoint to list |
noun | Yes | The plural used in the count and the empty and error messages, such as "No orders match these filters." |
columns | Yes | One { label, width?, align?, labelHidden? } per column. The first takes the remaining width; align: 'end' lines up numbers on the right |
order-options | Yes | The sort choices. Each value is sent as order[<field>]=<direction>; the first is the default |
search-label | Yes | The search field's label. What's typed is sent as search= |
create-noun | No | Adds a create button (and one on the empty list) that opens the item dialog at add. Leave it out for records that aren't created in the admin, such as orders |
The list sends the page's query to fetch-url, so the API's collection needs filters for search, the order fields and anything else you add. The slots are:
| Slot | What goes in it |
|---|---|
item | One row, given data (the resource), rawData and link (the item dialog's route) |
filters | More filters beside the search, bound to the page's query |
notice | A notice above the list |
empty | What an empty list shows, in place of the create button |
Filters
A filter is a field bound to the page's query with useQueryBoundModel. Every query parameter except page, perPage and order is sent to the API and counts as a filter, so the list offers to clear it when nothing matches:
<template #filters>
<CwaUiMultiSelect
v-model="states"
label="State"
label-hidden
placeholder="Any state"
search-placeholder="Search states"
:options="stateOptions"
/>
</template>
<script setup lang="ts">
const stateOptions = ['awaiting_payment', 'paid', 'cancelled', 'refunded'].map(state => ({ label: state, value: state }))
const { model: stateModel } = useQueryBoundModel('state[]', { defaultValue: [] })
const states = computed({
get: () => (stateModel.value ?? []) as string[],
set: (value: string[]) => { stateModel.value = value },
})
</script>
Rows
| Component | Props |
|---|---|
<CwaAdminListRow> | title (required), to (the row's link, usually link), sub (a second line), dim. With actions, it shows an edit button, and a view button when view is set; name labels them. The default slot holds its <CwaAdminListCell>s |
<CwaAdminListCell> | One cell after the title. align="end" for numbers |
The row's title takes the first column, so give columns one entry more than the row has cells.
The Item Dialog
The item file opens over the list when a row is clicked, at /_cwa/shop-orders/<iri>. useCwaAdminItem loads the resource from the route, as the built-in dialogs do, and <CwaUiDialog> lays it out:
<!-- cwa-shop/admin/order.vue -->
<template>
<CwaUiDialog
v-if="localResourceData || isLoading || loadFailed"
placement="right"
:title="localResourceData?.number ?? 'Order'"
:tabs="tabs"
:loading="isLoading"
:load-failed="loadFailed"
noun="order"
@close="$emit('close')"
@retry="loadResource()"
>
<template #details>
<CwaUiFormSection title="Order">
<CwaUiDetailList :items="summary" />
</CwaUiFormSection>
<CwaUiFormSection title="Lines">
<CwaUiDataTable label="Order lines" :columns="lineColumns" :rows="localResourceData?.lines ?? []" />
</CwaUiFormSection>
<CwaUiFormSection v-if="localResourceData?.state === 'paid'" title="Refund">
<CwaUiButton label="Refund" tone="danger" :loading="actionRunning" @click="refund" />
</CwaUiFormSection>
</template>
<template #info>
<CwaAdminResourceId :iri="localResourceData?.['@id']" />
</template>
</CwaUiDialog>
</template>
<script setup lang="ts">
const emit = defineEmits<{ close: [], reload: [] }>()
const { localResourceData, isLoading, loadFailed, loadResource, runAction, actionRunning } = useCwaAdminItem({ createEndpoint: '/shop/orders', emit, resourceType: 'Order', defaultResource: () => ({}),})
const tabs = [{ key: 'details', label: 'Details' }, { key: 'info', label: 'Info' }]
const lineColumns = [
{ key: 'title', label: 'Item' },
{ key: 'quantity', label: 'Qty', align: 'end' as const },
{ key: 'total', label: 'Total', align: 'end' as const },
]
const summary = computed(() => [
{ label: 'State', value: localResourceData.value?.state },
{ label: 'Email', value: localResourceData.value?.customerEmail },
{ label: 'Total', value: localResourceData.value?.total },
])
function refund() {
const iri = localResourceData.value?.['@id']
return iri && runAction({
endpoint: `${iri}/refund`,
confirm: { title: 'Refund this order?', go: 'Refund', tone: 'danger' },
success: 'Order refunded',
})
}
</script>
The dialog emits close to go back to the list and reload to refresh it. Each entry in tabs names a slot.
useCwaAdminItem
It takes the options of the built-in dialogs' item composable:
| Option | Required | What it does |
|---|---|---|
createEndpoint | Yes | Where a new item is posted, when the dialog is opened at add |
emit | Yes | The dialog's emit, with close and reload |
defaultResource | Yes | The fields a new item starts with |
resourceType | No | The resource's type |
It returns the loaded resource and its state: localResourceData (the dialog's copy), resource (the stored one), isLoading, loadFailed, loadResource() to load it again, saveResource() and deleteResource() for an editable item, plus runAction and actionRunning.
Running an Action
An action is a request that does something to the item, such as a refund. runAction() sends it, and only then reloads the item and the list:
| Field | What it does |
|---|---|
endpoint | Where the request goes |
method | POST (default) or PATCH |
body | The request body, if any |
confirm | Ask first, with the admin's confirm dialog: title, go (the button), tone (danger or accent), and optionally description |
success | A notice shown when the request succeeds |
It returns true when the request succeeded (an empty 204 counts), and false when the admin cancelled the confirm or the request failed. A failure shows no success notice and reloads nothing; the API's error appears as an error notice in the admin header, which stays until it's dismissed. The header shows only for content and user admins, so a user whose only admin role is the extension's own sees no notice. actionRunning is true while an action runs, and a second action started meanwhile is ignored, so bind it to the button's loading.
Read-Only Details and Tables
Two kit components show data an admin reads but doesn't edit:
<CwaUiDetailList> shows labelled values, in two columns where there's room. items is a list of { label, value }; an empty value shows a dash, read as "Not set". The value slot, given item and index, formats a value.
<CwaUiDataTable> shows rows of data. columns is a list of { key, label, align? }, rows a list of objects read by each column's key, and label names the table for screen readers. empty is what it says with no rows (default "None"). A wide table scrolls sideways inside the dialog. The cell slot, given row, column and value, formats a cell:
<CwaUiDataTable label="Order lines" :columns="lineColumns" :rows="lines">
<template #cell="{ column, value }">
{{ column.key === 'total' ? formatMoney(value) : value }}
</template>
</CwaUiDataTable>
<CwaAdminResourceId :iri> shows a resource's ID with a Copy ID button, as on the built-in Info tabs.
The other kit components (<CwaUiDialog>, <CwaUiFormSection>, <CwaUiButton>, <CwaUiBadge> and the fields) are listed in The CWA Layer › Reusable UI Components.
Next: Sign-In and Sign-Out Events.
Registering an Extension
The cwa:extend hook, the extension's prefix and cwa/ folder, how a site replaces an extension's file, page cache rules, and the build errors.
Sign-In and Sign-Out Events
The cwa:auth:signed-in and cwa:auth:signed-out hooks, when each one is called, and what happens when a handler throws.