Corresponding Entity References
CER keeps entity reference fields in sync by automatically creating and maintaining bidirectional references between entities.
cer
概要
Corresponding Entity References(CER)は、エンティティ参照フィールドを使用するエンティティ間の双方向関係を自動的に維持します。エンティティAliceがエンティティBobを参照すると、CERは自動的にBobからAliceへの逆参照を作成します。この同期は、どちらかのエンティティが作成、更新、または削除されるたびに継続されます。
CERは「プリセット」を通じて動作します。プリセットは参照タイプフィールド間の関係を定義する設定エンティティです。どの2つのフィールドが相互に対応すべきか、どのエンティティバンドルがその関係に参加するかを指定します。このモジュールはコアのEntity Referenceフィールドとすぐに連携できます。
このモジュールはCorresponding Node References(CNR)モジュールのDrupal 8/9/10/11後継版であり、ノードだけでなくフィールドを持つあらゆるエンティティタイプで動作するようにゼロから再構築されました。
Features
- エンティティ参照フィールドの自動双方向同期 - エンティティAがエンティティBを参照すると、エンティティBは自動的にエンティティAを参照
- どのフィールドが相互に対応すべきかを定義する設定エンティティベースのプリセット
- エンティティ参照フィールドを持つあらゆるエンティティタイプをサポート(Node、Taxonomy Term、User、Commerce製品など)
- 特定のエンティティバンドルに同期を制限するバンドルフィルタリング
- エンティティタイプのすべてのバンドルを含めるワイルドカードバンドルサポート(entity_type:*)
- 追加方向の制御 - 新しい参照を既存の参照の先頭に追加(prepend)するか末尾に追加(append)するかを選択
- 各対応参照プリセットの有効/無効切り替え
- 管理UIを通じた既存参照の手動同期機能
- 自動クリーンアップ - エンティティが削除されると、対応するエンティティからの参照も自動的に削除
- 他のモジュールが同期動作を変更できるHook API(hook_cer_differences_alter)
- 複数値エンティティ参照フィールドに対応 - 個々の参照の追加と削除を正しく処理
- エンティティ/フィールド権限を尊重 - 同期は通常のセキュリティコンテキストで動作
Use Cases
Bidirectional node relationships
Create two-way relationships between content types. For example, if you have 'Article' and 'Related Articles' where articles reference other articles, CER ensures that when Article A references Article B, Article B automatically shows Article A in its related articles field.
Author and authored content relationship
Link users to their content bidirectionally. If you have a custom 'Authored By' field on nodes and a 'Published Works' field on user profiles, CER keeps these synchronized automatically.
Product and category relationships
In e-commerce sites, maintain bidirectional relationships between products and categories. When a product is added to a category, the category automatically lists that product.
Event and speaker management
For event websites, link events to speakers and speakers to events. When a speaker is added to an event, that event automatically appears on the speaker's profile.
Organizational hierarchies
Create bidirectional parent-child or related-entity relationships within the same content type. Useful for organizational structures, location hierarchies, or any self-referencing relationships.
Cross-entity-type relationships
Link different entity types like nodes, taxonomy terms, and commerce products. For example, link blog posts to taxonomy terms and have terms automatically reference the posts tagged with them.
Tips
- CER does not store any additional information about referenced entities, making upgrades from older versions straightforward - simply configure CER after upgrading and existing relationships continue working
- Use the wildcard bundle option (entity_type:*) when you want all bundles of an entity type to participate in synchronization
- The same field can be used for both First field and Second field to create self-referencing bidirectional relationships
- Consider field cardinality when setting up presets - if a field only allows one reference, only the most recent reference will be kept
- Use hook_cer_differences_alter() to add custom logic, such as only synchronizing published entities
- Test corresponding references with a small set of content before applying to large content sets
- Remember that CER respects entity and field permissions - test with appropriate user roles
Technical Details
Admin Pages 5
/admin/config/content/cer
Main administration page displaying a list of all configured corresponding reference presets. Shows each preset's label, machine name, corresponding fields, add direction (append/prepend), and enabled status. Provides operations links for editing, deleting, and synchronizing each preset.
/admin/config/content/cer/add
Form to create a new corresponding reference preset that defines a bidirectional relationship between two entity reference fields.
/admin/config/content/cer/{corresponding_reference}
Form to modify an existing corresponding reference preset. All settings except the machine name can be changed.
/admin/config/content/cer/{corresponding_reference}/delete
Confirmation form to permanently delete a corresponding reference preset. This does not remove any existing references from entities - it only removes the synchronization configuration.
/admin/config/content/cer/{corresponding_reference}/sync
Confirmation form to manually trigger synchronization of all entities for this preset. Useful for bringing existing content into sync after creating a new preset or enabling synchronization.
権限 1
Hooks 1
hook_cer_differences_alter
Allows modules to alter the differences calculated by CER before they are applied. Called during entity synchronization to modify which references will be added or removed.
Troubleshooting 5
Ensure the corresponding reference preset is enabled. Check that both entities being referenced belong to bundles configured in the preset's Bundles field. Verify that both entities have the configured reference fields.
CER operates within normal security context. Ensure the current user has permission to edit the referenced entities. Check field-level permissions if using Field Permissions module.
Use the Synchronize operation on the preset to process existing entities. Navigate to the preset's edit page and use the Synchronize task tab.
CER supports self-referencing relationships. Ensure both First field and Second field are set to the same field name, and that the bundles are correctly configured.
Check the Add Direction setting on your preset. Use 'Prepend' to add new references at the top or 'Append' to add them at the bottom of the reference list.
Security Notes 4
- CER operates within normal Drupal security context - it cannot reference entities the current user cannot access
- Users need the 'administer cer' permission to manage corresponding reference configurations
- Synchronization operations respect entity access controls - unpermitted references will not be created
- Be mindful of field-level permissions when using modules like Field Permissions alongside CER