八月 13, 2017 | 後端和Drupal

Drupal 8 專用的CLI工具:Drupal Console

Drupal Console跟Drush比較不一樣,他是專門給D8來使用。而就官方的說法,這個CLI工具,可以來產生Sample Code,並且在開發的時候進行Debug來跟D8網站進行互動,大大的降低了開發D8模組的困難程度

在Drupal8裡面,由於跟Drupal7差異甚大,主要一個很大的差別就是將整個Drupal的底層改用symfony的架構來做開發。對於原本熟悉Drupal7開發的人來說,應該是蠻痛苦的。畢竟整個寫法都不一樣了。不過,Drupal Console同樣也因為Symfony架構的關係,為了D8應運而生。

Drupal Console跟Drush比較不一樣,他是專門給D8來使用。而就官方的說法,這個CLI工具,可以來產生Sample Code,並且在開發的時候進行Debug來跟D8網站進行互動,大大的降低了開發D8模組的困難程度。

安裝

安裝就照下面步驟囉,簡單易懂,對於使用蘋果的人來說,可能會有下載下來變成0KB的狀況。(我就是) 這個時候可能是curl無法使用,那就直接用瀏覽器下載也是OK的唷

$ curl https://drupalconsole.com/installer -L -o drupal.phar
$ mv drupal.phar /usr/local/bin/drupal
$ chmod +x /usr/local/bin/drupal

安裝完,可以直接通過 list的指令,可以看到全部

$ drupal list

簡易指令統整

以下是這篇文章要介紹的幾個常用的指令。如果要看完整的就還需要看看官方的文件囉

  1. 下載新的Drupal網站
  2. 安裝新的Drupal網站
  3. 建立MultiSite
  4. 開啟/關閉維護模式
  5. 切換開發模式/生產模式
  6. 產生亂數Node/terms/Users
  7. 建立一個模組
  8. 安裝一個模組
  9. 產生Controller
  10. 建立一個Sample Block

下載新的Drupal網站

下載新的Drupal8網站到特定目錄下,並且一次把所有Composer相關套件補足。

$ drupal site:new --directory ~/website/drupal8

安裝新的Drupal網站

安裝畫面也可以通過一句指令就一次解決,參數一目了然囉~

$ drupal site:install  standard  \
--langcode="en"  \
--db-type="mysql"  \
--db-host="127.0.0.1"  \
--db-name="drupal8"  \
--db-user="test"  \
--db-pass="test"  \
--db-port="3306"  \
--site-name="Drupal 8"  \
--site-mail="[email protected]"  \
--account-name="admin"  \
--account-mail="drupal"  \
--account-pass="drupal"

建立MultiSite

如果有在使用多網站的人,一樣一句指令可以解決

$ drupal multisite:new  vendor/newsite http://mysite.example.com

開啟/關閉維護模式

開啟維護模式

$ drupal site:maintenance on

關閉維護模式

$ drupal site:maintenance off

切換開發模式/生產模式

開發模式:關閉全部的Cache

$ drupal site:mode dev

生產模式

$ drupal site:mode  prod

產生亂數Node/terms/Users

在Drupal 7 本來需要安裝devel generate這個模組,到了D8只需要一句指令就好囉

  1. 產生Node測試資料
$ drupal create:nodes
  1. 產生Users測試資料
$ drupal create:users
  1. 產生Terms測試資料
$ drupal create:terms

建立一個Hello Santa 模組

如果你不熟悉如何開發Drupal模組,沒有關係,drupal Console可以快速幫助我們建立一個Sample模組,讓我們再繼續往下開發

$ drupal generate:module  // 只需要打這一行指令,就收工了

 // Welcome to the Drupal module generator

 Enter the new module name:
 > hellosanta

 Enter the module machine name [hellosanta]:
 >

 Enter the module Path [/modules/custom]:
 >

 Enter module description [My Awesome Module]:
 > My first Module.

 Enter package name [Custom]:
 > santa

 Enter Drupal Core version [8.x]:
 >

 Do you want to generate a .module file (yes/no) [yes]:
 >

 Define module as feature (yes/no) [no]:
 >

 Do you want to add a composer.json file to your module (yes/no) [yes]:
 >

 Would you like to add module dependencies (yes/no) [no]:
 >

 Do you want to generate a unit test class (yes/no) [yes]:
 >

 Do you want to generate a themeable template (yes/no) [yes]:
 >


 Do you confirm generation? (yes/no) [yes]:
 >

Generated or updated files

 1 - /Users/yangzonghan/website/drupal8/web/modules/custom/hellosanta/hellosanta.info.yml
 2 - /Users/yangzonghan/website/drupal8/web/modules/custom/hellosanta/hellosanta.module
 3 - /Users/yangzonghan/website/drupal8/web/modules/custom/hellosanta/composer.json
 4 - /Users/yangzonghan/website/drupal8/web/modules/custom/hellosanta/tests/src/Functional/LoadTest.php
 5 - /Users/yangzonghan/website/drupal8/web/modules/custom/hellosanta/hellosanta.module
 6 - /Users/yangzonghan/website/drupal8/web/modules/custom/hellosanta/templates/hellosanta.html.twig

安裝模組

$ drupal module:install hellosanta

產生Controller

這個方法可以快速幫我們針對特定模組產生對應的頁面,同樣一句指令就處理完畢惹 :)

這裡的範例是建立一個路徑為/hellosanta/example的頁面。

$ drupal generate:controller

 // Welcome to the Drupal Controller generator
 Enter the module name [hellosanta]:
 >

 Enter the Controller class name [DefaultController]:
 >

 Enter the Controller method title (to stop adding more methods, leave this empty) [ ]:
 > Hello Santa Awesome

 Enter the action method name [hello]:
 > HelloSanta

 Enter the route path [/hellosanta/HelloSanta]:
 > /hellosanta/example

 Enter the Controller method title (to stop adding more methods, leave this empty) [ ]:
 >

 Do you want to generate a unit test class (yes/no) [yes]:
 >

 Do you want to load services from the container (yes/no) [no]:
 >


 Do you confirm generation? (yes/no) [yes]:
 >

上面都做完了以後,直接就可以看到頁面了,超級快速吧~ 不用會寫程式:)

建立一個客製化區塊

這裡建立一個區塊,裡面有一個姓名Textfield還有一個Email的Field,一樣用一句指令解決

$ drupal generate:plugin:block //只需要打這一行,其他跟著問題走

 // Welcome to the Drupal Plugin Block generator
 Enter the module name [hellosanta]:
 > hellosanta

 Enter the plugin class name [DefaultBlock]:
 > santablock

 Enter the plugin label [Santablock]:
 >

 Enter the plugin id [santablock]:
 > hellosanta-block-1

 Enter the theme region to render the Plugin Block. [ ]:
 >

 Do you want to load services from the container (yes/no) [no]:
 >


You can add input fields to create special configurations in the block.
This is optional, press enter to continue
 Do you want to generate a form structure? (yes/no) [yes]:
 >

 New field type (press <return> to stop adding fields) [ ]:
 > textfield

 Input label:
 > Name

 Input machine name [name]:
 >

 Maximum amount of characters [64]:
 > 10

 Width of the textfield (in characters) [64]:
 > 30

 Description [ ]:
 > Please leave your name

 Default value [ ]:
 > Victor Yang

 Weight for input item [0]:
 >

 New field type (press <return> to stop adding fields) [ ]:
 > email

 Input label:
 > Email

 Input machine name [email]:
 >

 Description [ ]:
 > Please leave your Email

 Default value [ ]:
 > [email protected]

 Weight for input item [0]:
 > 1

 New field type (press <return> to stop adding fields) [ ]:
 >

 Do you confirm generation? (yes/no) [yes]:
 >

輸入完畢就可以看到這個區塊囉

畫面呈現

小記

Drupal Console是一個很好用的指令工具,另外還可以客製化指令,是個不錯的工具,至於跟Drush的指令比較起來,我個人覺得是不相衝突,反而是相輔相成的,尤其在開發上面,提供了非常大的幫忙與協助,是個值得花時間學習的工具,由於指令提供的太多,所以上面就簡單介紹幾個常用的跟大家分享囉

使用我們的服務即表示您同意Cookie政策。了解更多