Skip to content

Commit 26f3919

Browse files
committed
GraphQL: Asset fields, examples
1 parent f1c8a96 commit 26f3919

File tree

1 file changed

+80
-22
lines changed

1 file changed

+80
-22
lines changed

docs/5.x/reference/field-types/assets.md

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,38 +101,54 @@ You can choose which custom fields should be available for assets in each volume
101101

102102
## Development
103103

104-
### Querying Elements with Assets Fields
104+
Asset fields have similar features to other [relational fields](../../system/relations.md).
105105

106-
When [querying for elements](../../development/element-queries.md) that have an Assets field, you can filter the results based on the Assets field data using a query param named after your field’s handle.
106+
### Querying Elements with Assets Fields
107107

108-
Possible values include:
108+
When [querying for elements](../../development/element-queries.md) that may have an assets field, you can use values compatible with [relational query params](../../system/relations.md) to narrow the results:
109109

110110
| Value | Fetches elements…
111111
| - | -
112112
| `':empty:'` | that don’t have any related assets.
113113
| `':notempty:'` | that have at least one related asset.
114114
| `100` | that are related to the asset with an ID of 100.
115-
| `[100, 200]` | that are related to an asset with an ID of 100 or 200.
116-
| `[':empty:', 100, 200]` | with no related assets, or are related to an asset with an ID of 100 or 200.
115+
| `[100, 200]` | that are related to an asset with an ID of 100 _or_ 200.
116+
| `[':empty:', 100, 200]` | with no related assets, or are related to an asset with an ID of 100 _or_ 200.
117117
| `['and', 100, 200]` | that are related to the assets with IDs of 100 and 200.
118-
| an [Asset](craft5:craft\elements\Asset) object | that are related to the asset.
118+
| one or more [Asset](craft5:craft\elements\Asset) elements | that are related to the asset/assets.
119119
| an [AssetQuery](craft5:craft\elements\db\AssetQuery) object | that are related to any of the resulting assets.
120120

121-
::: code
121+
In this example, we’re querying for other “product” entries that reference the same document:
122+
122123
```twig
123-
{# Fetch entries with a related asset #}
124-
{% set entries = craft.entries()
125-
.myFieldHandle(':notempty:')
124+
{% set document = entry.materialDisclosures.one() %}
125+
126+
{% set similarProducts = craft.entries()
127+
.section('products')
128+
.materialDisclosures(document)
126129
.all() %}
127130
```
128-
```php
129-
// Fetch entries with a related asset
130-
$entries = \craft\elements\Entry::find()
131-
->myFieldHandle(':notempty:')
132-
->all();
133-
```
131+
132+
::: tip
133+
Exclude the current entry by setting `.id(['not', entry.id])` to the query!
134134
:::
135135

136+
You might also want to search for elements that have (or _don’t_ have) assets attached to a specific field:
137+
138+
```twig
139+
{# Products for which documentation of hazardous materials is provided: #}
140+
{% set potentiallyDangerousProducts = craft.entries()
141+
.section('products')
142+
.materialDisclosures(':notempty:')
143+
.all() %}
144+
145+
{# Products that have no disclosures: #}
146+
{% set probablySafeProducts = craft.entries()
147+
.section('products')
148+
.materialDisclosures(':empty:')
149+
.all() %}
150+
```
151+
136152
### Working with Assets Field Data
137153

138154
If you have an element with an Assets field in your template, you can access its related assets using your Assets field’s handle:
@@ -223,12 +239,6 @@ $relatedAssets = $entry->myFieldHandle
223239
```
224240
:::
225241

226-
::: tip
227-
<Todo notes="Extract this into a snippet." />
228-
229-
In Craft 3, we recommended cloning these query objects using the [`clone` keyword](https://www.php.net/manual/en/language.oop5.cloning.php) or [`clone()`](../twig/functions.md#clone) Twig function before applying params. **This is no longer required in Craft 4**, because a new copy of the query is returned each time you access the field property.
230-
:::
231-
232242
### Saving Assets Fields
233243

234244
If you have an element form, such as an [entry form](kb:entry-form), that needs to contain an Assets field, you will need to submit your field value as a list of asset IDs in the order you want them to be related.
@@ -317,6 +327,54 @@ You can do this by passing each of the related asset IDs in the field data array
317327
}) }}
318328
```
319329

330+
### GraphQL
331+
332+
<See path="../../development/graphql.md" hash="custom-fields" label="Custom Fields in GraphQL" description="Get familiar with using custom fields in GraphQL queries." />
333+
334+
Assets related via an assets field can be queried by via the field’s handle. You must make one or more sub-selections:
335+
336+
```graphql{4-8}
337+
query News {
338+
entries(section: "blog") {
339+
... on post_Entry {
340+
featureImage {
341+
id
342+
filename
343+
url
344+
}
345+
}
346+
}
347+
}
348+
```
349+
350+
The special [`@transform` directive](../../development/graphql.md#the-transform-directive) allows you to apply [image transforms](../../development/image-transforms.md#graphql) to assets, on-the-fly. The directive can be used on the entire asset field, or many subfields thereof:
351+
352+
```graphql{4,16}
353+
query HomepageContent {
354+
hero: entries(section: "blog", limit: 1) {
355+
... on post_Entry {
356+
featureImage@transform(handle: "hero") {
357+
url
358+
filename
359+
width
360+
height
361+
}
362+
}
363+
}
364+
365+
feed: entries(section: "blog", offset: 1) {
366+
... on post_Entry {
367+
featureImage {
368+
url@transform(handle: "thumbnail")
369+
filename
370+
width
371+
height
372+
}
373+
}
374+
}
375+
}
376+
```
377+
320378
## See Also
321379

322380
- [Asset Queries](../element-types/assets.md#querying-assets)

0 commit comments

Comments
 (0)