フックとイベント
XOOPSはコアの機能と相互作用し、直接的な依存関係なくモジュール間で相互作用するための拡張ポイントとしてフックとイベントを提供します。
フック対イベント
Section titled “フック対イベント”| 側面 | フック | イベント |
|---|---|---|
| 目的 | 動作/データの変更 | 出現に対応 |
| リターン | 変更されたデータを返すことができる | 通常はvoid |
| タイミング | アクション前/中 | アクション後 |
| パターン | フィルターチェーン | オブザーバー/pub-sub |
フックシステム
Section titled “フックシステム”フックを登録
Section titled “フックを登録”// xoops_version.php でフックを登録$modversion['hooks'][] = [ 'name' => 'user.profile.display', 'callback' => 'mymodule_hook_user_profile', 'priority' => 10,];フックコールバック
Section titled “フックコールバック”function mymodule_hook_user_profile(array $data): array{ $userId = $data['user_id'];
// カスタムプロフィールフィールドを追加 $data['fields']['reputation'] = mymodule_get_user_reputation($userId); $data['fields']['badges'] = mymodule_get_user_badges($userId);
return $data;}利用可能なコアフック
Section titled “利用可能なコアフック”| フック名 | データ | 説明 |
|---|---|---|
user.profile.display | ユーザーデータ配列 | プロフィール表示を変更 |
content.render | コンテンツHTML | コンテンツ出力をフィルター |
form.submit | フォームデータ | フォームデータを検証/変更 |
search.results | 結果配列 | 検索結果をフィルター |
menu.main | メニュー項目 | メインメニューを変更 |
イベントシステム
Section titled “イベントシステム”イベントをディスパッチ
Section titled “イベントをディスパッチ”// モジュールコード内$eventHandler = xoops_getHandler('event');
$eventHandler->trigger('mymodule.article.created', [ 'article_id' => $article->id(), 'author_id' => $article->authorId(), 'title' => $article->title(),]);イベントをリッスン
Section titled “イベントをリッスン”class MyModulePreload extends \Xmf\Module\Helper\AbstractHelper{ public function eventMymoduleArticleCreated(array $args): void { $articleId = $args['article_id'];
// サブスクライバーに通知 $this->notifyNewArticle($articleId); }
public function eventUserLogin(array $args): void { $userId = $args['userid'];
// モジュール用にユーザーアクティビティを更新 $this->updateUserActivity($userId); }}プリロードイベント リファレンス
Section titled “プリロードイベント リファレンス”コアイベント
Section titled “コアイベント”// ヘッダー/フッターpublic function eventCoreHeaderStart(array $args): void {}public function eventCoreHeaderEnd(array $args): void {}public function eventCoreFooterStart(array $args): void {}public function eventCoreFooterEnd(array $args): void {}
// インクルードpublic function eventCoreIncludeCommonStart(array $args): void {}public function eventCoreIncludeCommonEnd(array $args): void {}
// 例外public function eventCoreException(array $args): void {}ユーザーイベント
Section titled “ユーザーイベント”public function eventUserLogin(array $args): void {}public function eventUserLogout(array $args): void {}public function eventUserRegister(array $args): void {}public function eventUserActivate(array $args): void {}public function eventUserDelete(array $args): void {}モジュールイベント
Section titled “モジュールイベント”public function eventSystemModuleInstall(array $args): void {}public function eventSystemModuleUninstall(array $args): void {}public function eventSystemModuleUpdate(array $args): void {}public function eventSystemModuleActivate(array $args): void {}public function eventSystemModuleDeactivate(array $args): void {}カスタムモジュールイベント
Section titled “カスタムモジュールイベント”イベントを定義
Section titled “イベントを定義”// イベント定数を定義class ArticleEvents{ public const CREATED = 'mymodule.article.created'; public const UPDATED = 'mymodule.article.updated'; public const DELETED = 'mymodule.article.deleted'; public const PUBLISHED = 'mymodule.article.published';}イベントをトリガー
Section titled “イベントをトリガー”class ArticleService{ public function publish(Article $article): void { $article->publish(); $this->repository->save($article);
// イベントをトリガー $GLOBALS['xoopsPreload']->triggerEvent( ArticleEvents::PUBLISHED, ['article' => $article] ); }}モジュールイベントをリッスン
Section titled “モジュールイベントをリッスン”// 別のモジュールのPreload.php内
public function eventMymoduleArticlePublished(array $args): void{ $article = $args['article'];
// 検索用にインデックス $this->searchIndexer->index($article);
// サイトマップを更新 $this->sitemapGenerator->addUrl($article->url());}ベストプラクティス
Section titled “ベストプラクティス”- 特定の名前を使用 -
module.entity.action形式 - 最小限のデータを渡す - リスナーが必要とするもののみ
- イベントを文書化 - モジュールドキュメントにイベントをリスト
- 副作用を避ける - リスナーを焦点を当てた状態に保つ
- エラーを処理 - リスナーエラーがフローを中断しないようにする
関連ドキュメント
Section titled “関連ドキュメント”- Event-System - 詳細なイベントドキュメント
- ../03-Module-Development/Module-Development - モジュール開発
- ../07-XOOPS-4.0/Implementation-Guides/Event-System-Guide - PSR-14イベント