Entity Prepopulate
Prepopulates entity field values with tokens and YAML-formatted data, working at the entity level for robust field prepopulation on entity forms.
epp
インストール
composer require 'drupal/epp:8.x-1.7'
概要
Entity Prepopulate は、Token を含む事前定義された値で Entity フィールドを自動的に入力できます。フォームレベルで動作する従来の Prepopulate モジュールとは異なり、このモジュールは Entity レベルで動作するため、より堅牢で安全なフィールド事前入力アプローチを提供します。
このモジュールは YAML 構文をサポートしており、複数値フィールドや複数プロパティフィールド(geofield など)を含む複雑な設定が可能です。すべての事前入力はフィールド設定を通じて明示的に行われるため、セキュリティが向上します。値はすべての Token が正常に置換された場合にのみ適用され、フィールドバリデーションにより無効な値が設定されることはありません。
設定は標準の Drupal Field UI を通じてフィールドごとに行います。フィールド設定フォームに「Entity Prepopulate」フィールドセットが追加されます。事前入力値(Token サポート付き)を指定し、新規 Entity 作成時のみ適用するか、既存 Entity の更新時にも適用するかを選択できます。
Features
- Token を含む設定可能な値で任意の Entity フィールドを事前入力
- より堅牢な動作のためにフォームレベルではなく Entity レベルで動作
- 複数値フィールドや複数プロパティフィールド(lat/lng を持つ geofield など)用の YAML 構文をサポート
- すべての Token が正常に置換された場合にのみ値が適用され、不完全なデータを防止
- フィールドバリデーションにより無効な値の設定を防止
- 既存 Entity の編集時にも事前入力を適用するオプションの「更新時も適用」設定
- Token モジュールと統合し、拡張 Token サポートと Token ブラウザを提供
- 設定フォームで利用可能なフィールドプロパティをヘルプリファレンスとして表示
- フィールドごとの明示的な設定により安全な事前入力を確保
- 任意のフィールド設定可能な Entity タイプ(Node、User、Taxonomy Term など)で動作
Use Cases
Prepopulate a text field with the site name
Configure a text field to automatically display the site name when creating new content. Set the Value field to '[site:name]' in the field's EPP settings. When users create new content, the field will be pre-filled with the actual site name.
Set default author reference to current user
Configure an entity reference field (e.g., 'Author') to prepopulate with the currently logged-in user. Use the token '[current-user:uid]' as the value to automatically set the current user as the default selection.
Prepopulate multiple values in a multi-value field
For fields with cardinality greater than 1, use YAML list syntax to prepopulate multiple values. For example: - first [site:name] - second value - third value This will fill the first three field items with the respective values.
Prepopulate a geofield with specific coordinates
For multi-property fields like geofield, use YAML mapping syntax to target specific properties: lat: 51.5074 lng: -0.1278 This sets the latitude and longitude properties separately.
Auto-fill status field on update
Configure a status field with 'Also on update' enabled to ensure a specific value is always applied when editing entities. This is useful for workflow states or tracking fields that should be reset on each edit.
Dynamic date prepopulation
Use date tokens like '[current-date:custom:Y-m-d]' to prepopulate date fields with the current date when creating new content. Useful for tracking creation dates or setting default deadlines.
Conditional prepopulation with token safety
Since values are only applied when ALL tokens are replaced successfully, you can safely use tokens that may not always be available. If a token cannot be replaced (e.g., context-dependent tokens), the field simply won't be prepopulated, preventing partial or broken data.
Tips
- Use the 'Available field properties' expandable section to see which properties you can target with YAML syntax for complex field types
- Install the Token module to get a token browser and access to many additional tokens beyond core
- Values are only applied if ALL tokens are replaced - this is a feature, not a bug, preventing partial data
- The 'Also on update' option is useful for fields that should be reset or recalculated on every edit
- For multi-value fields, use YAML list syntax with dash markers (- item1)
- For multi-property fields (like address or geofield), use YAML mapping syntax (property: value)
- Check Drupal logs (admin/reports/dblog) for EPP-related messages when debugging prepopulation issues
- This module works at the entity level, so prepopulated values will be visible in any form display for the entity
Technical Details
Admin Pages 1
/admin/structure/types/manage/[content-type]/fields/[entity_type].[bundle].[field_name]
Entity Prepopulate adds a fieldset to existing field configuration forms (both regular field config and base field overrides) where you can configure prepopulation values. This appears on any field configuration page in Drupal.
Hooks 2
hook_entity_prepare_form
Implements the core prepopulation logic. When an entity form is being prepared, this hook iterates through all fields, checks for EPP third-party settings, replaces tokens, parses YAML, validates values, and sets the field values on the entity.
hook_form_alter
Alters field_config_edit_form and base_field_override_edit_form to add the EPP configuration fieldset with Value textarea, token browser, field properties help, and Also on update checkbox.
Troubleshooting 5
Check the Drupal logs for EPP-related messages. Common causes include: 1) Not all tokens were replaced (if using tokens that require specific context), 2) The value failed field validation (e.g., setting text in a number field), 3) The user doesn't have edit access to the field, or 4) 'Also on update' is not checked when editing existing entities.
YAML parse errors are logged. Ensure proper YAML formatting: use consistent indentation (spaces, not tabs), proper list markers (- item), and valid mapping syntax (key: value). Test your YAML in a validator before using it.
Install the Token module for more tokens and the token browser. Verify token syntax is correct with the browser. Remember that some tokens require specific context (e.g., [node:title] requires an existing node) which may not be available on entity create forms.
The module validates prepopulated values against field constraints. If validation fails, the previous value is restored and a notice is logged. Ensure your prepopulation value matches the expected field format (numbers for integer fields, valid entity IDs for references, etc.).
If both the Value and 'Also on update' settings are empty/unchecked and you save the field configuration, EPP third-party settings are automatically removed to prevent unnecessary module dependencies.
Security Notes 5
- All prepopulation is explicit through configuration - no URL parameter injection like the original Prepopulate module
- Field edit access is checked before applying prepopulation values
- Values are validated against field constraints before being set
- Invalid values are logged but not applied, preventing data integrity issues
- This module has security coverage from the Drupal Security Team