Field Config Cardinality

Allows setting different cardinality (number of values) limits on individual field instances, overriding the field storage cardinality on a per-bundle basis.

field_config_cardinality
3,460 sites
39
drupal.org

インストール

Drupal 11, 10 v4.0.1
composer require 'drupal/field_config_cardinality:^4.0'
Drupal 9 v3.0.1
composer require 'drupal/field_config_cardinality:^3.0'
Drupal 8 v8.x-1.2
composer require 'drupal/field_config_cardinality:8.x-1.2'

概要

Field Config Cardinalityは、異なるBundle(コンテンツタイプ、Entityタイプなど)間で同じFieldに対して異なるカーディナリティ制限を設定する機能を提供します。Drupal Coreでは、Fieldのカーディナリティはfield storageレベルで定義されるため、Fieldのすべてのインスタンスが同じカーディナリティ設定を共有します。このモジュールは、管理者がBundle単位で異なる値の制限を設定できるインスタンスレベルのカーディナリティオーバーライドを追加します。

例えば、storageレベルで無制限のカーディナリティを持つ「タグ」エンティティ参照フィールドがある場合、このモジュールを使用して記事は5つのタグに制限し、ページは無制限のタグを許可することができます。モジュールは、Field設定エンティティのサードパーティ設定としてカーディナリティ設定を保存することでこれを実現します。

このモジュールは、Options Select、Entity Reference Autocomplete、Media Libraryウィジェットの修正版を含む、インスタンスカーディナリティ設定を尊重する拡張Fieldウィジェットも提供します。さらに、カーディナリティと必須フィールドの状態に基づいて、セレクトウィジェットの空ラベルテキストのカスタマイズをサポートしています。

Features

  • Bundle単位でfield storageのカーディナリティをオーバーライドするインスタンスレベルのカーディナリティを設定
  • 特定のBundleで無制限カーディナリティのFieldを特定の数に制限するサポート
  • エンティティ参照フィールド、リストフィールド(integer、float、string)、ファイル/画像フィールド、Media LibraryフィールドおよびMedia Libraryフィールドとの互換性
  • インスタンスカーディナリティを適切に強制するカスタムウィジェット:カーディナリティ対応セレクトリスト、カーディナリティ対応Entity Reference Autocomplete、カーディナリティ対応Media Library
  • 無制限/制限および必須/任意の状態に対応した異なるラベルを持つセレクトウィジェットの空ラベルテキストのカスタマイズ
  • インスタンスカーディナリティ制限に達した際の「さらに追加」ボタンの自動非表示
  • storageレベルで定義された最大カーディナリティを尊重(storageカーディナリティを超えることはできません)

Use Cases

Limiting Tags on Different Content Types

You have a 'Tags' taxonomy reference field shared across multiple content types with unlimited cardinality at storage level. Using this module, you can limit Articles to 5 tags, Blog posts to 10 tags, and leave Pages with unlimited tags. This provides flexibility in content governance while using a single shared field.

Restricting Media Items Per Bundle

A 'Gallery Images' media reference field is configured as unlimited in storage. For Product content type, you want to allow only 4 images, while for Portfolio items you want unlimited. This module enables this per-bundle restriction using the Media Library widget.

Single Value Override for Entity Reference

An 'Author' entity reference field references User entities and has unlimited cardinality at storage. For News articles, you want to allow multiple authors, but for Simple Pages, you want exactly one author. By setting instance cardinality to 1 for Simple Page, the autocomplete widget displays a single field instead of a multi-value interface.

Custom Empty Labels for Better UX

You have a 'Priority' list field that needs different placeholder text based on context. On required fields you want '- Select Priority -' as the placeholder, while on optional fields you want '- No Priority -'. This module's empty label customization allows tailoring the user experience per field instance.

File Upload Limits Per Content Type

A 'Documents' file field is set to unlimited at storage level. For User profiles, you want to limit document uploads to 3 files, while Admin pages can have unlimited. The module enforces this by hiding additional upload slots beyond the instance cardinality limit.

Tips

  • Instance cardinality cannot exceed the field storage cardinality - ensure your storage cardinality is set high enough or to unlimited
  • When using the 'Unlimited' option for instance cardinality, the field storage must also be configured as unlimited
  • The module modifies core widgets through hook_field_widget_info_alter, which may conflict with other modules that modify the same widgets
  • Third-party settings are included in configuration exports, so cardinality settings will be exported when using drush config:export
  • For select widgets, use the CardinalityOptionsSelectWidget explicitly to get full support for custom empty labels
  • When setting instance cardinality to 1 on a multi-value field, the widget behavior changes to single-value mode, hiding the 'Add another' button

Technical Details

Admin Pages 1
Field Settings (with Instance Cardinality) /admin/structure/types/manage/{type}/fields/{field}/edit

The module extends the standard field configuration edit form with additional cardinality settings. These settings appear as a new fieldset on the existing field edit page, allowing administrators to set a different cardinality limit for this specific field instance.

Hooks 4
hook_field_widget_info_alter

Replaces the default media_library_widget and entity_reference_autocomplete widget classes with custom classes that support instance cardinality.

hook_form_alter (field_config_edit_form)

Adds the 'Cardinality Instance' fieldset to the field configuration edit form, allowing administrators to set instance-level cardinality and empty label options.

hook_field_widget_complete_form_alter

Applies instance cardinality settings to field widgets during form rendering. Handles managed_file fields by hiding items beyond the cardinality limit, and converts multi-select to single-select when cardinality is 1.

hook_preprocess_field_multiple_value_form

Modifies the rendering of multiple value fields to limit visible items based on instance cardinality. Hides the 'Add more' button when the cardinality limit is reached.

Security Notes 2
  • The module adds form elements to admin-only field configuration pages, which are protected by existing Drupal field permissions
  • Instance cardinality is enforced at the form level; additional server-side validation may be needed for API submissions