Config Pages
Provides fieldable configuration entities that can be placed anywhere in the menu system, with context-awareness support for language, domain, or custom contexts.
config_pages
インストール
composer require 'drupal/config_pages:8.x-2.19'
概要
Config Pagesは、DrupalのField APIを使用してカスタム設定ページを作成する強力な方法を提供します。Form APIでカスタムフォームを書く代わりに、ファイルアップロード、エンティティ参照、複数値のドラッグ&ドロップ、AJAXインタラクションなど、エンティティフィールドの完全な機能を活用できます。
このモジュールは設定値を保存するフィールド付きコンテンツエンティティを作成し、その構造はconfig entity bundleによって定義されます。各config pageタイプはメニューシステムの任意のパスにマウントでき、コンテキスト認識が可能です。つまり、現在の言語、ドメイン、または任意のカスタムコンテキストプラグインに基づいて異なる値を保存できます。
このモジュールは、ホームページ、サイト全体の設定、複雑なカスタム設定フォームの置き換えなど、シングルトンページの作成に最適です。Token連携、Views連携をサポートし、プログラムで値を管理するためのDrushコマンドを提供します。
Features
- DrupalのField APIを使用したフィールド付きエンティティ - テキスト、画像、エンティティ参照、Paragraphsなど、あらゆるフィールドタイプを追加可能
- config pageを任意のメニューパス(例:/admin/config/mysettings)にマウントし、適切なパンくずリストとメニュー階層を設定
- 現在の言語やドメインに基づいて異なる設定値を持てるコンテキスト認識システム
- 言語やドメイン以外のカスタムコンテキストタイプを作成するための拡張可能なコンテキストプラグインシステム
- コンテキスト固有の設定が存在しない場合にデフォルト値を使用するコンテキストフォールバック機構
- config pageのフィールド値を置換トークンとして公開するToken連携
- config pageのコンテンツをサイトの任意の場所に表示するためのBlockプラグイン
- config pageのフィールド値に基づいてBlock/要素の表示を制御するVisibility conditionプラグイン
- コンテキスト対応クエリのためのargument defaultプラグインを含むViews連携
- テンプレートで直接config pageフィールドをレンダリングするためのTwig関数
- フィールド値をプログラムで取得・設定するためのDrushコマンド
- config page値を管理するためのDrupal Consoleコマンド
- 異なるコンテキスト間でフィールド値をコピーするインポート機能
- すべてのフィールドをデフォルトにリセットする値クリア機能
- config pageタイプごとの表示・編集アクセスのきめ細かいパーミッション
- 表示されたBlockからの素早い編集のためのContextual links
- フィールド、表示モード、フォーム設定を管理するための完全なField UI連携
Use Cases
Homepage Configuration
Create a config page type with fields for homepage hero banner, featured content references, promotional text, and call-to-action buttons. Content editors can update homepage elements without developer intervention, using familiar field widgets for images, WYSIWYG text, and entity references.
Global Site Settings
Replace custom settings forms with a config page containing fields for social media links, company contact information, footer copyright text, and analytics tracking codes. Mount it at /admin/config/site-settings for easy access and enable token exposure to use values throughout the site.
Multilingual Configuration
Enable language context to store different promotional content, legal text, or regional information per language. The system automatically loads the appropriate config page based on the current site language, with fallback to a default language when no translation exists.
Featured Content Curation
Replace Nodequeue with a config page using entity reference fields with Views-powered autocomplete. Editors can curate featured articles, products, or media items using drag-and-drop ordering, with all the power of Views filtering for the selection interface.
Conditional Block Display
Use the ConfigPage field value visibility condition to show or hide blocks based on config page settings. For example, display a promotional banner only when a 'show_promo' boolean field is enabled, allowing content managers to toggle site-wide features.
Template Integration
Use the config_pages_field() Twig function to render config page fields directly in theme templates. For example, {{ config_pages_field('footer_settings', 'field_copyright_text') }} renders the copyright text in footer.html.twig without needing a block.
Tips
- Use the config_pages_config() helper function in custom code to easily load a config page by type: $config = config_pages_config('my_settings');
- For programmatic value retrieval, use the config_pages.loader service: \Drupal::service('config_pages.loader')->getValue('my_settings', 'field_name', 0, 'value');
- Custom context plugins can be created by extending ConfigPagesContextBase and placing them in Plugin/ConfigPagesContext directory
- Config page types and their field configurations are exportable via Drupal's configuration management, but the actual content (field values) is stored as content entities
- When using entity reference fields on config pages, enable the autocomplete widget with Views selection for powerful filtering capabilities
- The Twig function config_pages_field() automatically handles caching and returns an empty render array with appropriate cache tags when no config page exists
Technical Details
Admin Pages 4
/admin/structure/config_pages
Overview page listing all config page types with their context settings and token exposure status. From here, you can quickly access any config page for editing.
/admin/structure/config_pages/types
Administrative page for managing config page type definitions. Lists all available types with their context settings and provides access to edit, delete, and manage fields.
/admin/structure/config_pages/types/add
Form for creating a new config page type with menu path, context settings, and token configuration.
/admin/structure/config_pages/{config_pages_type}/edit
The actual config page form where users enter configuration values. Shows all configured fields and allows saving, clearing values, and importing from other contexts.
権限 9
Hooks 2
hook_config_pages_contexts_info_alter
Allows modules to alter the list of available context plugins
hook_config_pages_functions_alter
Allows modules and themes to add custom Twig functions to the config_pages extension
Drush Commands 2
drush config:pages-set-field-value
Set a value for a field of a config page
drush config:pages-get-field-value
Get the value of a field from a config page
Troubleshooting 4
The specified menu path conflicts with an existing route. Choose a different path or remove/modify the conflicting route.
Verify that context plugins are properly enabled on the config page type. Check if a config page entity exists for the current context. Review fallback settings if values should fall back to a default.
Ensure the 'Expose this ConfigPage values as tokens' checkbox is enabled on the config page type form. Clear caches after enabling.
Verify a config page entity exists (has been saved at least once). Check that the view mode has the fields configured in 'Manage display'. Ensure the user has 'view' permission for the config page type.
Security Notes 4
- Permissions are granular - assign 'edit [type] config page entity' for specific types rather than 'edit config_pages entity' which grants access to all types
- The 'administer config_pages types' permission grants significant power including the ability to create new config page types and modify field configurations - assign carefully
- Field values are not automatically sanitized for output - use appropriate field formatters and follow Drupal's render system best practices
- The 'access config_pages clear values option' permission allows users to reset all field values - this could result in data loss if misused