useCwaResource
Low-level composable that fetches resource data and wires into the admin manager. For display components, prefer useCwaComponent — it wraps this composable and adds a plugin system for collections, images, and custom behaviour.
import { toRef } from 'vue'
import type { IriProp } from '#cwa/composables/cwa-resource'
import { useCwaResource } from '#imports'
const props = defineProps<IriProp>()
const { getResource, exposeMeta } = useCwaResource(toRef(props, 'iri'))
const resource = getResource()
defineExpose(exposeMeta)
Signature
useCwaResource(
iri: Ref<string>,
options?: {
name?: string // a label for the admin
manager?: {
disabled?: boolean // skip admin manager integration
}
}
)
Parameters
| Parameter | Type | Description |
|---|---|---|
iri | Ref<string> | The component's IRI as a ref. Use toRef(props, 'iri') to convert the iri prop |
options.name | string | A label passed to the admin in exposeMeta. The UI select names a variant by the name in its defineCwaConfig, not by this option |
options.manager.disabled | boolean | Skip the admin manager integration |
Styles aren't an option here. They are declared with defineCwaConfig; see useCwaComponent › Style Variants.
Return Values
const { getResource, exposeMeta, $cwa, styleKeys, uiClassNames } = useCwaResource(iriRef)
| Return | Type | Purpose |
|---|---|---|
getResource | () => ComputedRef<CwaCurrentResourceInterface | undefined> | Call it once in setup to get the resource ref |
exposeMeta | object | Pass directly to defineExpose |
$cwa | CwaComposable | Access to auth, resources, UI state |
styleKeys | ComputedRef<string[]> | The keys of the styles an admin picked, in stored order. See useCwaComponent › Style Variants |
uiClassNames | ComputedRef<string[] | undefined> | What the resource stores: one style:<key> entry for each picked style |
const resource = getResource() — call getResource() once to get the reactive resource ref. Never call it in a computed or template — call it once in setup().
resource.value?.data?.title — access your PHP entity fields on .data.
resource.value states:
undefined— the store has no entry for this IRI yet (show skeleton)- object without
data— the fetch is in progress or failed (apiState.statussays which) - object with
data— data available
defineExpose(exposeMeta) is required — without it the admin manager cannot find and select this component.
Example
<template>
<h1>{{ resource?.data?.title }}</h1>
</template>
<script setup lang="ts">
import { toRef } from 'vue'
import type { IriProp } from '#cwa/composables/cwa-resource'
import { useCwaResource } from '#imports'
const props = defineProps<IriProp>()
const { getResource, exposeMeta } = useCwaResource(toRef(props, 'iri'))
const resource = getResource()
defineExpose(exposeMeta)
</script>
UI Variant Files
A file in the component's ui/ folder is a UI variant. Its label in the admin's UI select comes from its own defineCwaConfig:
<!-- app/app/cwa/components/HtmlContent/ui/AltHtmlContent.vue -->
<script setup lang="ts">
defineCwaConfig({
name: 'Alternative',
})
const props = defineProps<IriProp>()
const { getResource, exposeMeta, styleKeys } = useCwaResource(toRef(props, 'iri'))
defineExpose(exposeMeta)
</script>
Without a name, the select shows the component's registered name. See Alternative UI Variants for the full pattern.
Component (recommended)
The recommended entry point for all CWA display components — fetch resource data, expose to the admin manager, and compose plugins for collections, uploaded files, or custom behaviour.
File Field
Resolve a single uploadable file field on a resource — content URL, display media, load state, and Imagine filter variants — named at the call site.