Flag

Create customized boolean toggle fields (flags) that users can attach to any entity type, enabling features like bookmarks, favorites, spam reports, or any user-driven content markers.

flag
30,754 sites
184
drupal.org

インストール

Drupal 11, 10 v5.0.3
composer require 'drupal/flag:^5.0'

概要

Flagモジュールを使用すると、サイト管理者はNode、Comment、User、またはカスタムEntityタイプを含む任意のDrupal Entityタイプに付与できる柔軟なブーリアントグルフィールド(「フラグ」と呼ばれる)を定義できます。適切な権限を持つユーザーはコンテンツにフラグを付けたり外したりでき、ユーザー主導のコンテンツ整理とインタラクションの豊かなエコシステムを構築できます。

デフォルトでは、フラグはユーザーごとに動作し、認証済みの各ユーザーが独自のフラグ付きコンテンツセットを維持します。これにより、ブックマーク、お気に入り、ウォッチリストなどの個人的な機能が可能になります。さらに、フラグは「グローバル」として設定することもでき、その場合フラグの状態はすべてのユーザー間で共有されます。これはコンテンツを注目または不適切としてマークする機能に便利です。

このモジュールは、フラグ付きコンテンツの表示とフィルタリングのための包括的なViews統合、拡張性のためのプラグインベースのアーキテクチャ、フラグ操作へのカスタム対応のためのイベント駆動フック、セッショントラッキングによる匿名ユーザーのフラグ付けサポートを提供します。FlagにはDrupal 6および7からの移行サポートも含まれており、既存のフラグ設定を簡単にアップグレードできます。

Features

  • 任意のEntityタイプ(Node、Comment、User、カスタムEntity)に対して、設定可能なラベル、テキスト、動作を持つ無制限のカスタムフラグを作成
  • ユーザーごとのフラグ付け(個人のブックマーク、お気に入り)またはグローバルフラグ付け(編集機能用にすべてのユーザー間で共有)
  • 複数のリンクタイプ:AJAX駆動の即時トグル、確認フォーム、カスタムフィールド付きフィールド入力フォーム、またはページリロード
  • フラグをEntityビューモードの疑似フィールドとして、Entity編集フォームのチェックボックスとして、またはコンテキストメニューに表示
  • フラグ付きコンテンツのリストを構築するためのリレーションシップ、フィルター、ソート、フィールドを備えた完全なViews統合
  • 各フラグに対してフラグ/フラグ解除権限を生成する動的権限システム(オプションで所有者ベースの権限も)
  • カスタム統合のためにEntityがフラグ付け/フラグ解除されたときにイベントをディスパッチするイベント駆動アーキテクチャ
  • カスタムテンプレートでフラグリンクとカウントをレンダリングするためのTwigテンプレート関数(flagcount、flaglink)
  • セッションベースのトラッキングによる匿名ユーザーのフラグ付けサポート
  • Views Bulk Operationsによる一括フラグ/フラグ解除操作のためのActionプラグイン統合
  • Rules、自動メール、その他のToken対応モジュールで使用するための包括的なTokenサポート
  • Drupal 6および7のFlagモジュールからの移行サポート

Use Cases

Personal Bookmarks

Allow users to bookmark content for later reading. Create a per-user flag called 'bookmark' for nodes, use AJAX link type for instant feedback. Build a View filtered by the current user's bookmarks to create a 'My Bookmarks' page.

Content Favorites

Enable a favorites system where users can mark their favorite articles, products, or media. Use the flag count submodule to display popularity indicators showing how many users have favorited each item.

Spam/Abuse Reporting

Create a 'Report as spam' flag allowing users to flag inappropriate content. Configure with confirmation form link type to prevent accidental reports. Use Views to create an admin report showing flagged content sorted by flag count for moderation.

Editorial Featured Content

Use a global flag called 'featured' to mark content for homepage features. Since it's global, any editor can set it and the state is shared. Filter Views by this flag to dynamically populate featured content sections.

User Following System

Enable the flag_follower submodule or create a custom user flag for social following. Users can follow other users, and Views can display 'Followers' and 'Following' lists on user profiles.

Wishlist Functionality

For e-commerce sites, create a 'wishlist' flag for product content types. Use field entry link type to allow users to add notes or quantities when adding to wishlist. Display wishlist via Views with edit/remove capabilities.

Content Subscriptions

Create a 'subscribe' flag to allow users to subscribe to content for notifications. Hook into the flag.entity_flagged event to trigger email notifications when subscribed content is updated.

Voting/Rating Placeholder

While Flag itself is boolean, it can serve as the foundation for simple voting systems. Create 'upvote' and 'downvote' flags and use flag counts to calculate scores in Views or custom code.

Tips

  • Use the flag_bookmark submodule as a reference implementation when creating your own flags
  • For high-traffic sites, consider caching implications - contextual link display disables entity cache for users with flag access
  • Add custom fields to flaggings via Field UI at /admin/structure/flags/manage/[flag]/fields for collecting additional data like notes or ratings
  • Use the flagcount() and flaglink() Twig functions in custom templates for flexible flag display
  • Subscribe to flag.entity_flagged and flag.entity_unflagged events for custom integrations like notifications or statistics
  • Test anonymous flagging thoroughly as it relies on PHP session handling which varies by hosting environment
  • Use template suggestions (flag--[flag_id].html.twig) to customize the appearance of specific flags

Technical Details

Admin Pages 7
Flags /admin/structure/flags

Lists all configured flags on the site. From here administrators can view, edit, enable, disable, reset, or delete flags. The list shows the flag name, flag type (entity type it applies to), bundles it applies to, global status, and available operations.

Add flag /admin/structure/flags/add

First step of flag creation - select the entity type that this flag will be attached to. After selecting, continue to the full flag configuration form.

Add flag /admin/structure/flags/add/{entity_type}

Complete flag configuration form for creating a new flag. Configure all aspects of the flag including identification, scope, messages, access control, and display options.

Edit Flag /admin/structure/flags/manage/{flag}

Modify an existing flag's configuration. All settings from the add form are available for editing.

Reset Flag /admin/structure/flags/manage/{flag}/reset

Remove all flaggings for this flag. This permanently deletes all user flagging data associated with this flag.

Enable Flag /admin/structure/flags/manage/{flag}/enable

Re-enable a previously disabled flag, making it active on the site again.

Disable Flag /admin/structure/flags/manage/{flag}/disable

Temporarily disable a flag without deleting it. Disabled flags are not shown to users.

権限 8
Administer Flags

Define and manage Flags and Flag settings. Restricted access permission.

Administer Flaggings

Delete flaggings for all users. Restricted access permission.

Flag [flag_name]

Permission to flag content with this specific flag. Dynamically generated for each flag.

Unflag [flag_name]

Permission to unflag content with this specific flag. Dynamically generated for each flag.

Flag [flag_name] own items

Permission to flag content owned by the current user (optional, requires extra permissions enabled).

Unflag [flag_name] own items

Permission to unflag content owned by the current user (optional, requires extra permissions enabled).

Flag [flag_name] others' items

Permission to flag content owned by other users (optional, requires extra permissions enabled).

Unflag [flag_name] others' items

Permission to unflag content owned by other users (optional, requires extra permissions enabled).

Hooks 4
hook_flag_type_info_alter

Alter flag type plugin definitions provided by other modules. May be placed in a MODULE.flag.inc file.

hook_flag_options_alter

Alter a flag's default options. Use to add custom options that will be saved with the flag configuration.

hook_flag_link_type_info_alter

Alter action link type plugin definitions. May be placed in a MODULE.flag.inc file.

hook_flag_action_access

Perform custom access checks when a flag action is attempted. Return AccessResult objects to allow, deny, or remain neutral.

Troubleshooting 6
Flag links not appearing on content

Verify the flag is enabled, the user has 'flag [flag_id]' permission, the flag applies to the content's bundle, and display options (show as field, show in links) are configured. Check Manage Display settings if using field display.

AJAX flag links not working

Ensure JavaScript is enabled, check for JS errors in browser console, verify the flag/flag.link_ajax library is loading. Try clearing Drupal cache and browser cache.

Anonymous users cannot flag content

Anonymous flagging requires session-based tracking. Ensure PHP sessions are working correctly. Check that anonymous role has the flag permission. Note that page caching may interfere with anonymous flagging.

Flag counts not updating

Flag counts are maintained by the FlagCountManager event subscriber. Clear caches and verify the event system is working. Check database table 'flag_counts' for data integrity.

Views relationship not showing flags

Ensure you've added the flag relationship to your View and configured it to use the correct flag. Check that 'Include only flagged content' is set appropriately for your use case.

Confirmation form appears in wrong location

For Confirm Form or Field Entry link types, check the 'Form behavior' setting. Choose between 'New page', 'Dialog', or 'Modal dialog' based on your needs.

Security Notes 5
  • The 'administer flags' and 'administer flaggings' permissions are marked as restricted access - grant only to trusted administrators
  • Flag permissions are generated dynamically - review permissions after creating new flags to ensure appropriate access
  • Anonymous flagging uses session IDs which can be manipulated - do not use for security-critical features
  • CSRF protection is applied to all flag/unflag routes
  • When using contextual links display, entity caching is disabled for users with flag access to prevent cached flag states from being shown