Skip to content

Commit b690598

Browse files
committed
Clarify handling of asset subpath object templates
1 parent f6ae7b4 commit b690598

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,35 @@ On multi-site installs, the following settings will also be available (under **A
4848

4949
### Dynamic Subfolder Paths
5050

51-
Subfolder paths defined by the **Upload Location** and **Default Upload Location** settings can optionally contain Twig tags (e.g. `news/{{ slug }}`).
51+
Subfolder paths defined by the **Upload Location** and **Default Upload Location** settings are [object templates](../../system/object-templates.md). Any properties supported by the source element (the element that has the assets field) can be used here.
5252

53-
Any properties supported by the source element (the element that has the Assets field) can be used here.
54-
55-
::: tip
56-
If you want to include the entry’s ID or UID in a dynamic subfolder path, use `{canonicalId}` or `{canonicalUid}` rather than `{id}` or `{uid}`, so the source entry’s ID or UID is used rather than the those of a draft or revision.
53+
::: warning
54+
Be aware that _fields are reusable_, and can be added to [nested](#nested-elements) _and_ non-nested elements’ field layouts—and on different element types! Constructing a subfolder template that works in all contexts (and is readable and reliable) can be challenging—consider maintaining separate fields to avoid this complexity.
5755
:::
5856

59-
::: tip
60-
If an Assets field is used on an [entry type](../element-types/entries.md#entry-types) within a [Matrix field](matrix.md), the source element is going to be the nested entry, _not_ the element that the entry belongs to.
57+
#### IDs and UIDs
6158

62-
In this case, if you wanted to incorporate the main element’s ID in a dynamic subfolder path, you would use `owner.id` instead of just `id`. Combined with the above, this would mean `owner.canonicalId`.
63-
:::
59+
If you want to include an element’s ID or UID in a dynamic subfolder path, use `{canonicalId}` or `{canonicalUid}` rather than `{id}` or `{uid}`, so the source element’s ID or UID is used rather than the those of a draft or revision.
6460

65-
::: warning
66-
If the rendered subfolder path ends up blank, contains a leading or trailing forward slash (e.g. `foo/`), or an empty segment (e.g. `foo//bar`), Craft will interpret that as a sign that a variable in the subfolder template couldn’t be resolved successfully, and the path will be considered invalid. If you are intentionally outputting a blank segment, output `:ignore:`. For example, if you want to output the first selected category, or nothing if there isn’t one, do this:
61+
#### Nested Elements
62+
63+
If an Assets field is used by an [entry type](../element-types/entries.md#entry-types) within a [Matrix field](matrix.md), the source element will be the nested entry, _not_ the element that the entry belongs to. This means that assets attached to different nested entries may end up in different folders!
64+
65+
In these situations, you should use `owner.someElementProperty` instead of just `someElementProperty`; the previous example would become `owner.canonicalId` or `owner.canonicalUid`.
66+
67+
#### Conditional Path Segments
68+
69+
If a rendered subfolder path ends up blank or contains a leading, trailing, or double forward slash (e.g. `/bar/baz`, `foo/`, or `foo//bar`), Craft considers it invalid. When you suspect a value it depends on may not be available, output `:ignore:`. Suppose you want to output the first selected category, or nothing (when one isn’t selected):
6770

6871
```twig
69-
{{ categoriesFieldHandle.one().slug ?? ':ignore:' }}
72+
blog/{{ categoriesFieldHandle.one().slug ?? ':ignore:' }}/{canonicalId}
73+
```
74+
75+
This is equivalent to conditionally outputting the slashes as part of a value:
76+
77+
```twig
78+
blog/{{ categoriesFieldHandle.exists() ? "#{categoriesFieldHandle.one().slug}/" : null }}{canonicalId}
7079
```
71-
:::
7280

7381
## The Field
7482

0 commit comments

Comments
 (0)