Drupalに入門する
next-drupal.org というサイトを見かけた。これをベースに、Drupalでのheadless CMSを試してみることにする。
まずはcomposerでDrupalのプロジェクトを作る必要がある。
現在のマシンに入っている(というか入れた)のはこれら
$ composer --version
Composer version 2.8.9 2025-05-13 14:01:37
PHP version 8.4.7 (/opt/homebrew/Cellar/php/8.4.7/bin/php)
Run the "diagnose" command to get more detailed diagnostics output.
$ php -v
PHP 8.4.7 (cli) (built: May 6 2025 12:31:58) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.4.7, Copyright (c) Zend Technologies
with Zend OPcache v8.4.7, Copyright (c), by Zend Technologies
$ composer --version
Composer version 2.8.9 2025-05-13 14:01:37
PHP version 8.4.7 (/opt/homebrew/Cellar/php/8.4.7/bin/php)
Run the "diagnose" command to get more detailed diagnostics output.
$ composer create-project drupal/recommended-project drupal-site
# (略)
$ ls
drupal-site
$ ls drupal-site
composer.json composer.lock recipes vendor web
$ ls drupal-site/web
autoload.php example.gitignore INSTALL.txt profiles robots.txt themes
core index.php modules README.md sites update.php
$ ls drupal-site/recipes
README.txt
そのままgit addするとはちゃめちゃに重い。example.gitignoreがあるので、とりあえずそれをそのままリネームして使う
$ cd drupal-site
$ git init
$ git commit -m "Initial Commit" --allow-empty
$ echo '/vendor/**/*' > .gitignore
$ mv web/example.gitignore web/.gitignore
$ git add .
$ git commit -m 'create drupal project'
drupal/next
というパッケージを入れてみる、これは雰囲気的にdrupalのAPIクライアントが入ったNext.jsプロジェクトを組み立てるという感じのものかな、中身はまだ見てない
$ composer require drupal/next
# (略)
4 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
45 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Scaffolding files for drupal/core:
- Copy [web-root]/example.gitignore from assets/scaffold/files/example.gitignore
No security vulnerability advisories found.
Using version ^2.0 for drupal/next
$ mv web/example.gitignore web/.gitignore
$ git status | wc -l
720
$ git commit -m 'add drupal/next'
$ vim composer.json
$ g s
M composer.json
$ g d
composer.json
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
────┐
95: │
────┘
" * Remove the plugin that prints this message:",
" composer remove drupal/core-project-message"
]
},
"patches": {
"drupal/decoupled_router": {
"Unable to resolve path on node in other language than default": "https://www.drupal.org/files/issues/2024-08-05/decouple_router-3111456-resolve-language-issue-63--get-translation-re-rolled-and-good-redirect.patch"
}
}
}
}
$ composer require cweagans/composer-patches
./composer.json has been updated
Running composer update cweagans/composer-patches
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking cweagans/composer-patches (1.7.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading cweagans/composer-patches (1.7.3)
cweagans/composer-patches contains a Composer plugin which is currently not in your allow-plugins config. See https://getcomposer.org/allow-plugins
Do you trust "cweagans/composer-patches" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?] y
- Installing cweagans/composer-patches (1.7.3): Extracting archive
Generating autoload files
45 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Scaffolding files for drupal/core:
- Copy [web-root]/example.gitignore from assets/scaffold/files/example.gitignore
$ mv web/example.gitignore web/.gitignore
$ git commit -m 'patch drupal/decoupled_router'
以下のように書いてあったので、この手順は飛ばしても良かったかも
If you're not using multilingual features such as translation, you can skip this step.
- Visit /admin/modules.
- Enable the following modules: Next.js and Next.js JSON:API.
とりあえずDrupalを立ち上げるといいかな
ドキュメントルートっぽいところでPHPのbuiltin web serverを立ち上げてみる
$ cd web
$ php -S localhost:8910
そうしてブラウザで http://localhost:8910
を開いてみると、次のようなページを見ることができた。
ひとまずは適当に進めていく。DBはローカル環境でとりあえず動かしたいのでSQLiteで、メールは届かんだろうから適当に。
CSSが崩れちゃっているの、404が出ていてあれあれ〜という感じ
そんなところにファイルはない
$ ls sites/default/files/
config_1-6tuH5zt5VJu4K3sZmHWE8VtpbdNnaefbbcG1X4_3v0FvXM3c6zBwBx58C3jEMGGGhe3X7bwQ
languages
php
styles
translations
とりあえず、初期設定を進めてもGit管理下のファイルは増えていなそうだった
CSSの問題は後で見るとして、とりあえずチュートリアルに従って、 http://localhost:8910/admin/modules
にアクセスしたのち、 Next.js
と Next.js JSON:API
というモジュールを有効化する。それっぽいのを見つけて、チェックを入れたのち、画面最下部の「インストール」を押した。
「上記の設定で継続しますか?」と聞かれるので、承認して先に進む。
Visit /admin/modules.
Enable the following modules: Next.js and Next.js JSON:API.