Custom Field
A flexible multi-value field type that stores multiple sub-fields in a single database table, providing a high-performance alternative to Paragraphs and entity references.
custom_field
インストール
composer require 'drupal/custom_field:^4.0'
概要
Custom Fieldは、豊富なWidgetおよびFormatterプラグインをサポートする動的な複数値フィールドを作成できるDrupalモジュールです。Entityリファレンスやparagraphsを必要とする従来のアプローチとは異なり、Custom Fieldはすべてのデータを単一のデータベーステーブルに格納するため、不要なJOINを排除し、設定の複雑さを軽減することで大幅なパフォーマンス向上を実現します。
このモジュールはField Type、Widget、Formatterの包括的なプラグインシステムを提供し、開発者が機能を簡単に拡張できるようにします。各カスタムフィールドには異なるタイプの複数のカラム(サブフィールド)を含めることができ、すべて1つのテーブルに効率的に格納されます。このアプローチは、別個のEntityのオーバーヘッドなしに構造化された複数値データが必要なユースケースに最適です。
Custom FieldはFeeds、Search API、GraphQL Compose、JSON:API、Linkit、Media Library、Entity Browser、AIモジュールなど多くの人気Drupalモジュールと統合されており、複雑なコンテンツアーキテクチャに対応する多用途な選択肢となっています。
Features
- 最適なパフォーマンスのため、Entityリファレンスなしで単一のデータベーステーブルに格納される複数値フィールド
- string、text、integer、float、decimal、boolean、email、telephone、URI、link、datetime、daterange、time、time range、duration、file、image、color、map(キー値)、entity reference、UUID、viewfieldを含む26種類の組み込みサブフィールドタイプ
- text、textarea、select、radios、checkboxes、autocomplete、日付ピッカー、カラーボックス、ファイルアップロード、Media Library統合を含む39種類のサブフィールド用Widgetプラグイン
- 6種類のメインField Formatter:Default(カスタムテーマフック)、Inline、HTML List、Table、Flipped Table、Token対応のCustom Template
- レスポンシブブレークポイントを備えたインラインWidget配置のためのCSS Flexboxベースのレイアウトシステム
- 任意のEntityタイプの既存カスタムフィールドからフィールド設定をクローン
- Drushコマンドまたはプログラムによる既存データを持つフィールドへのカラムの追加・削除
- コンテンツ翻訳統合によるサブフィールドレベルの翻訳サポート
- カスタムフィルター、ソート、引数ハンドラーを備えた完全なViews統合
- 基本および高度なToken置換オプションを備えたすべてのサブフィールドのTokenサポート
- カスタムフィールドへのデータインポートのためのFeeds統合
- リンク、時間値、その他のフィールドタイプの包括的なバリデーション制約
Use Cases
Product Specifications
Create a custom field with sub-fields for weight, dimensions, material, color, and price. Display as a table formatter for product specification sheets. Eliminates the need for separate fields or Paragraphs for each specification.
Event Information
Use daterange sub-field with timezone support, combined with location (string), ticket URL (link), and price (decimal) sub-fields. The flexbox widget arranges these inline, while the table formatter creates a clean event listing.
Contact Information Cards
Combine name (string), email (email), phone (telephone), website (link), and photo (image) sub-fields. Use the inline formatter for compact display or the default formatter with custom theming for styled contact cards.
Address Components
Store street, city, state, postal code, and country as separate sub-fields with appropriate validation. Clone settings across content types for consistency. More efficient than Address module for simple use cases.
Media Gallery Metadata
Attach metadata to media items using image sub-field with caption (string_long), photographer (entity_reference to users), date taken (datetime), and location (map for coordinates).
Replacing Paragraphs for Simple Structures
For repeated structured data like team member profiles, FAQ items, or feature lists, custom fields provide better performance than Paragraphs by eliminating entity overhead and simplifying queries.
API Data Storage
Store structured API response data using map sub-fields, combined with timestamp, status, and reference fields. The JSON:API submodule ensures proper serialization for headless implementations.
Tips
- Use the 'Clone settings from' feature when creating new custom fields to copy configuration from existing fields, saving significant setup time.
- For best performance with large datasets, prefer custom fields over Paragraphs when you don't need the full entity features like revisions or moderation.
- The Custom Template formatter with token support provides Views-like rewrite functionality without the overhead of a View.
- Use the Drush commands (cf-add-column, cf-remove-column) to safely modify field structure on fields with existing data - the commands preserve data automatically.
- Enable 'Check empty' on sub-fields that should trigger row removal when empty, preventing partially filled multi-value items.
- The flexbox widget with column settings allows creating form layouts similar to inline entity form without additional modules.
- For entity references to media, enable the custom_field_media submodule to get full Media Library integration.
- Use the hierarchical select widget and formatter for taxonomy terms to display and select terms with their full hierarchy path.
Technical Details
Hooks 2
hook_custom_field_link_attributes_alter
Allows modules to alter available link attributes
hook_custom_field_tags_alter
Allows modules to alter available HTML wrapper tags
Drush Commands 2
drush custom_field:add-column
Interactively add a new column (sub-field) to an existing custom field with data preservation
drush custom_field:remove-column
Interactively remove a column (sub-field) from an existing custom field