LoginSignup
12
11

More than 5 years have passed since last update.

Drupal Consoleを使った開発環境構築

Last updated at Posted at 2016-12-08

Drupal Advent Calendar 2016の12/8担当です。
Drupal Consoleを使った開発環境構築について書きたいと思います。

Drupal Consoleとは

Drupal Consoleとはざっくりいうと、コマンドラインでDrupalを操作するツールです。WordPressでいうところのwp-cliといったところでしょうか。
Drupalのインストールやモジュールの開発、ファイルの生成などをコマンドラインで行うことができます。
日本語ドキュメントも用意されているので導入しやすいと思います。

How to install Drupal Console

インストールはComposerを使えば簡単です。以下の通りです。

composer global require drupal/console:@stable

Generate Project

drupal init

ファイルがたくさん生成されたと思います。とりあえず事前準備は完了です。
続いてプロジェクトを作っていきましょう。対話式でプロジェクトを作っていきます。

drupal site:new

 Enter the directory name where to install Drupal:
 > {{ディレクトリ名}}

 Getting releases for Drupal

 Select a core release:
  [0 ] 8.2.3
  [1 ] 8.2.2
  [2 ] 8.2.1
  [3 ] 8.2.0
  [4 ] 8.2.0-rc2
  [5 ] 8.2.0-rc1
  [6 ] 8.2.0-beta3
  [7 ] 8.2.0-beta2
  [8 ] 8.2.0-beta1
  [9 ] 8.1.10
  [10] 8.1.9
  [11] 8.1.8
  [12] 8.1.7
  [13] 8.1.6
  [14] 8.1.5
 > {{バージョン}}

これで、指定のディレクトリに指定のバージョンのDrupalがダウンロードされました。引き続きインストールしていきましょう。こちらも対話式です。

cd <DOWNLOADED-DIRECTORY>
drupal site:install
 Select Drupal profile to be installed:
  [0] Minimal
  [1] Standard
 > {{どちらか}}

 Select language for your Drupal installation [English]:
 > {{言語、日本語であればJapanese}}
 Drupal Database type:
  [0] MySQL, MariaDB, Percona Server, or equivalent
  [1] SQLite
 > {{どちらか、今回はSQLite}}

 Database File [sites/default/files/.ht.sqlite]:
 > {{DBファイル保存先}}

 Database Prefix [ ]:
 > {{DBのプレフィックス}}


 Provide your Drupal site name [Drupal 8 Site Install]:
 > {{サイト名}}

 Provide your Drupal site mail [admin@example.com]:
 > {{メールアドレス}}

 Provide your Drupal administrator account name [admin]:
 > {{Administratorのアカウント名}}

 Provide your Drupal administrator account mail [admin@example.com]:
 > {{Administratorのメールアドレス}}

 Provide your Drupal administrator account password:
 > {{Administratorのパスワード}}

 Starting Drupal 8 install process


 [OK] Your Drupal 8 installation was completed successfully                     

インストール完了です。ではサーバ起動してみましょう。

drupal server

起動したらhttp://localhot:8088でアクセスできます。

起動後の画面

おめでとうございます。開発環境の構築ができました。と言いたいですが、めんどくさいですよね。
このダウンロード〜サーバ起動までの流れ対話せずに実行できます。
chainコマンドというものを使って、指定したymlファイルの内容を一括実行します。まずは、ymlファイルの中身から確認しましょう。

drupal8.yml
commands:
  - command: site:new
    arguments:
      directory: drupal8-yml.dev
      version: 8.2.3

  - command: site:install
    options:
      langcode: ja
      db-type: sqlite
      db-file: sites/default/files/.ht.sqlite
      site-name: 'Drupal 8 using yml'
      site-mail: admin@example.com
      account-name: admin
      account-mail: admin@example.com
      account-pass: admin
      generate-inline: true
    arguments:
      profile: standard

  - command: server

1つ目のコマンドでDrupalのインストールを行い、2つ目のコマンドでインストール、3つ目のコマンドでサーバを起動します。実行コマンドは

drupal chain --file=./drupal8.yml

です。では実行すると、

 site:new
 Downloading drupal 8.2.3


 [OK] Drupal 8.2.3 was downloaded in directory                                  
      /path/to/project/drupal8-yml.dev  


 site:install
 Starting Drupal 8 install process


 [OK] Your Drupal 8 installation was completed successfully                     


 server


 /usr/local/Cellar/php56/5.6.27_4/bin/php -S 127.0.0.1:8088                     
 /Users/user/.console/router.php                                               



 [OK] Executing php from /usr/local/Cellar/php56/5.6.27_4/bin/php.              


PHP 5.6.27 Development Server started at Wed Nov 30 11:55:27 2016
Listening on http://127.0.0.1:8088
Document root is /path/to/project/drupal8-yml.dev

先ほどと同じ開発環境ができました。

なにがよいのか?

ymlを用意することで、チーム内の全員が、全く同様の環境を構築することができます。またCIツールと連携して継続的に自動テストを行うことができます。

今回は環境構築まででしたが、モジュール・プラグイン作成やDrupal Console自体のオリジナルコマンドもDrupal Consoleで作成できます。サクッと開発環境構築以外にも色々と使い道はあると思いますので一度触ってみてはどうでしょうか?

12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11