Guide to authoring and managing structured Content Fragments in AEM.
Content Fragments
1. Content Fragment Là Gì
Content Fragment (CF) là structured content tách biệt hoàn toàn khỏi presentation. Khác với page component (trộn content + layout), CF cho phép tái sử dụng cùng nội dung trên nhiều kênh: website, SPA, mobile app, email... thông qua Java API, REST, hoặc GraphQL.
CF được lưu dưới dạng dam:Asset trong /content/dam/.
2. Content Fragment Models
Model định nghĩa schema (fields + types) cho fragment. Được lưu trong configuration space /conf.
Bật CF Models cho một site
- Tools > General > Configuration Browser
- Chọn site config (VD:
myproject) - Tick Content Fragment Models
- Models được lưu tại:
/conf/myproject/settings/dam/cfm/models/
Các kiểu field
Cấu trúc JCR của Model
Validation trên Model
3. Cấu Trúc JCR Của Content Fragment
Data nằm tại jcr:content/data. Tên element trùng với tên field trong model. Fragment reference lưu dưới dạng String[] chứa path.
Variations
Variation là phiên bản thay thế của cùng nội dung (VD: bản tóm tắt, bản social media). Mỗi variation lưu riêng bộ field values:
Variation kế thừa từ master. Chỉ các field được set tường minh trên variation mới được lưu riêng.
4. Đọc Content Fragment — Java API
Adapt Resource sang ContentFragment
Package chính: com.adobe.cq.dam.cfm.ContentFragment
ContentFragment API
ContentElement API
Đọc typed values
Đọc Variations
5. Tạo Content Fragment Programmatically
Tạo một fragment
Set giá trị các kiểu field
Sử dụng:
Set Fragment References
Ví dụ hoàn chỉnh: tạo article + link references
6. Cập Nhật Fragment Đã Tồn Tại
Update field values
Tạo và chỉnh sửa Variations
Xóa fragment
7. Render Content Fragment Trong HTL
Core Content Fragment Component
AEM Core Components cung cấp sẵn Content Fragment component. Thêm vào template policy, cấu hình qua dialog.
Custom Sling Model + HTL
Sling Model:
HTL template:
Rich text cần context='html' để không bị escape:
8. Query Content Fragments Bằng QueryBuilder
Tìm tất cả CF thuộc model "article", field featured = true, sắp xếp theo publishDate:
Lưu ý query CF:
- Filter theo model:
jcr:content/data/cq:model - Field values nằm tại:
jcr:content/data/master/<fieldName> - Luôn set
p.limithợp lý, tránhp.limit=-1 - Tạo Oak index cho
cq:modelvà các custom properties hay query
9. Assets HTTP API (REST)
AEM 6.5 hỗ trợ REST API để CRUD fragment mà không cần Java code. Dùng cho external systems hoặc migration scripts.
10. Folder Structure Chuẩn
Tạo folder programmatically
11. Bulk Operations
Groovy Console — update field trên tất cả fragments cùng model
Bulk export to JSON
12. Best Practices
Model design
- Một model cho một content type (Article, Author, FAQ). Tránh model "catch-all" nhiều chục field.
- Dùng Fragment Reference để link data liên quan, không duplicate.
- Đặt tên field nhất quán: camelCase, match API contract.
- Thêm description cho field — hiển thị dưới dạng help text trong editor.
- Thêm field an toàn. Rename/remove field sẽ break fragments hiện có.
Authoring
- Tổ chức fragments theo folder: type, year, hoặc locale.
- Dùng Variations cho channel-specific content (web, email, social) thay vì tạo fragment riêng.
- Tag fragments để hỗ trợ tìm kiếm cross-model.
- Dùng Associated Content để link DAM assets thuộc về fragment.
Performance
- Query luôn filter theo
jcr:content/data/cq:modeltrước. - Tạo Oak index cho các fragment properties hay query.
- Cache fragment data trong Sling Model bằng
@PostConstruct— không đọc lại JCR mỗi lần gọi getter.
13. Lỗi Thường Gặp
Tham Khảo
- Content Fragment Models (AEM 6.5) — Adobe Experience League
- Content Fragments API (Javadoc)
- Assets HTTP API — Adobe Experience League