You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/5.x/reference/field-types/assets.md
+80-22Lines changed: 80 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -101,38 +101,54 @@ You can choose which custom fields should be available for assets in each volume
101
101
102
102
## Development
103
103
104
-
### Querying Elements with Assets Fields
104
+
Asset fields have similar features to other [relational fields](../../system/relations.md).
105
105
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
107
107
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:
109
109
110
110
| Value | Fetches elements…
111
111
| - | -
112
112
| `':empty:'` | that don’t have any related assets.
113
113
| `':notempty:'` | that have at least one related asset.
114
114
| `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.
117
117
| `['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.
119
119
| an [AssetQuery](craft5:craft\elements\db\AssetQuery) object | that are related to any of the resulting assets.
120
120
121
-
::: code
121
+
In this example, we’re querying for other “product” entries that reference the same document:
122
+
122
123
```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)
126
129
.all() %}
127
130
```
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!
134
134
:::
135
135
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
+
136
152
### Working with Assets Field Data
137
153
138
154
If you have an element with an Assets field in your template, you can access its related assets using your Assets field’s handle:
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
-
232
242
### Saving Assets Fields
233
243
234
244
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
317
327
}) }}
318
328
```
319
329
330
+
### GraphQL
331
+
332
+
<Seepath="../../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:
0 commit comments