こちらのサイトではDRUPAL10に関連する記事を掲載しています。
Drupalでは「モジュール」と呼ばれる、小さなソフトウェアを実装することで機能拡張を行います。本ウェビナーシリーズでは、Drupalのモジュール開発に関する基礎知識を解説します。Drupalのバックエンド周りについて体系的に学びたい方、自分でカスタムモジュールを作れるようになりたい方、アクイア認定デベロッパー試験を目指す方におすすめです。
Drupalを一通り操作したことがある方を対象としています。まだDrupalを触ったことが無い方は、初心者向けDrupalチュートリアル動画(日本語)を先に見ておくとより理解が深まります。
カスタムモジュールを作ろう
カスタムモジュールを作る方法は以下の方法がある
- フック
- プラグイン
- エンティティ
- サービス
- ルーティング
- イベント
今回はフックでモジュールを作成する
今回の作成手順
![](https://drupalfan.com/wp-content/uploads/image-141.png)
*.info.ymlファイル作成
モジュールのメタ情報を記載する
モジュール作成において唯一の必須ファイルになる
配置場所
![](https://drupalfan.com/wp-content/uploads/image-142.png)
設定内容
description以外は必須項目
下記以外にもオプション設定がある
![](https://drupalfan.com/wp-content/uploads/image-143.png)
有効化
YMLファイルだけで有効化のリストに表示されるようになる
![](https://drupalfan.com/wp-content/uploads/image-144-1024x312.png)
フックを実装
フックとは
Drupalの動作を変更する関数
Drupal7までは主にフックで実装されていた
既存のコードを変更することなく、Drupalコアやモジュールの動作を変更可能
フックにはhook_で始まる名前がある
フックには一連のパラメータと戻り値が用意されている
基本的にカスタムモジュールの*.moduleファイルでフックを実装する
カスタムモジュールでオリジナルのフックを実装することもできる
保存時のボタンの表示を変更する
Drupalのドキュメントに定義されている(300個くらいあ)
![](https://drupalfan.com/wp-content/uploads/image-145-1024x433.png)
手順
![](https://drupalfan.com/wp-content/uploads/image-146.png)
![](https://drupalfan.com/wp-content/uploads/image-147-1024x518.png)
![](https://drupalfan.com/wp-content/uploads/image-148-1024x318.png)
![](https://drupalfan.com/wp-content/uploads/image-149.png)
![](https://drupalfan.com/wp-content/uploads/image-150.png)
![](https://drupalfan.com/wp-content/uploads/image-151.png)
![](https://drupalfan.com/wp-content/uploads/image-152.png)
![](https://drupalfan.com/wp-content/uploads/image-153.png)
ルーティングとコントローラを追加
ページが呼ばれる仕組み
![](https://drupalfan.com/wp-content/uploads/image-154.png)
リクエストを受け付ける(INDEX.PHP)
DRUPALカーネルを実行(SYMFONYのHTTPKERNELをベースにDRUPALKERNELを実装)
対象のコントローラを特定、実行、テンプレートレンダリングをする(ROUTING)
結果をレスポンスできる形式にして返す
ルーティングシステム
![](https://drupalfan.com/wp-content/uploads/image-155.png)
既存Drupalサイトのルート一覧
Develモジュールで全ルート一覧を取得できる
![](https://drupalfan.com/wp-content/uploads/image-156.png)
YMLファイルで定義
![](https://drupalfan.com/wp-content/uploads/image-157.png)
コントローラの作成
![](https://drupalfan.com/wp-content/uploads/image-158.png)
maruchaパスをアクセスする
![](https://drupalfan.com/wp-content/uploads/image-159.png)
ルーティング一覧に表示される
![](https://drupalfan.com/wp-content/uploads/image-160.png)
フォームを作成
このようなフォームをカスタムモジュールでつくる
Drupal FormAPIを使う
![](https://drupalfan.com/wp-content/uploads/image-161.png)
DrupalのFormAPIとは
FormAPIとはHTMLフォームを定義するための抽象化レイヤー
フォームの定義、表示、検証、送信というライフサイクルを制御する
フォームが要求されるとレンダリング可能は配列変数として定義される
レンダリング過程でHTMLに変換、エンドユーザに表示される
フォルムの種類
FormBase
ConfigFormBase他
フォームクラスの作成
getFormID()
buildForm()
submitForm()
以上3つのメソッドが必要
![](https://drupalfan.com/wp-content/uploads/image-162.png)
![](https://drupalfan.com/wp-content/uploads/image-163.png)
![](https://drupalfan.com/wp-content/uploads/image-164.png)
YMLファイルにルーティング定義を追加
![](https://drupalfan.com/wp-content/uploads/image-165.png)
![](https://drupalfan.com/wp-content/uploads/image-166.png)
![](https://drupalfan.com/wp-content/uploads/image-167.png)
ブロックを作成
ブロックAPIとは
![](https://drupalfan.com/wp-content/uploads/image-168-1024x399.png)
ブロッククラスを作成する
![](https://drupalfan.com/wp-content/uploads/image-169-1024x517.png)
![](https://drupalfan.com/wp-content/uploads/image-170.png)
![](https://drupalfan.com/wp-content/uploads/image-171.png)
![](https://drupalfan.com/wp-content/uploads/image-172.png)
練習問題
![](https://blogdeoshiete.com/wp-content/uploads/advertise.jpg)
この記事またはDrupalに関するご質問がございましたら、お気軽にお問い合わせください。