Drupal 8 Starter Tutorial

Drupal 8 开发起步

开发环境

Composer 安装 Drupal 环境

# 安装 PHP 包管理工具
brew install composer

# 使用 Composer 国内镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com

# 安装 Drupal 环境到当前目录的 docs 目录下
composer create-project drupal-composer/drupal-project:8.x-dev --stability dev --no-interaction docs

通过 Composer 安装的 Drupal 8 自带新版 Drush 支持,全局安装 Drush 8.x 版本后,在安装目录下,运行 Drush 命令时会自动调用 9.x 版本。

通过 Composer 安装的 Drupal 8 还自带 Drupal Console 支持,参考以下方法为 Drupal Console 添加全局支持:

# 全局安装 drupal 命令
php -r "readfile('https://drupalconsole.com/installer');" > /usr/local/bin/drupal
chmod +x /usr/local/bin/drupal

Composer 安装常用 Drupal 8 扩展:

# Themes
composer require 'drupal/drupal8_zymphonies_theme:^1.3'

composer require --dev 'drupal/devel:^1.2'

Drush 安装 Drupal 环境

访问 Drush 主页,安装 Drush 命令行 8.14+ 版本到 /usr/local/bin 目录。Drush 安装的 Drupal 8 仍可使用 Drush 升级。

# 添加最新 Drush 支持
drush dl drupal

升级 Drupal 环境

使用 Composer 安装的 Drupal 8 使用以下命令升级:

# 检查补丁
composer outdated drupal/*

# 应用补丁
composer update drupal/core webflo/drupal-core-require-dev --with-dependencies
composer update drupal/* --with-dependencies

Drush 安装的 Drupal 环境同样使用 Drush 部署升级布丁。

常用设置

Drupal 8 初始化

Drupal 环境安装后需要访问 install.php 去初始化 Drupal 8 站点,也可以用如下命令完成:

# drupal 命令初始化 Drupal 8 环境
drupal site:install standard  --langcode en --db-type mysql --db-port 3306 --db-user root --db-pass root --db-host 127.0.0.1

开发环境设置

开发环境的设置保存在 setttings.local.php 文件中,覆盖 settings.php 中默认或生产环境的设置:

<?php

/**
 * DB Configurations.
 * 
 * Below configuration is referring MAMP MySQL server.
 */
$databases['default']['default'] = array (
  'database' => '<DB Name>',
  'username' => 'root',
  'password' => 'root',
  'prefix' => '',
  'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

/**
 * Export configurations to a non web directory.
 */
$config_directories['sync'] = '../config/sync';


/**
 * Enable local development services.
 */
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

/**
 * Disable CSS and JS aggregation.
 */
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

/**
 * Site Information.
 */
// Site Name - Basing on core/modules/system/config/schema/system.schema.yml.
$config['system.site']['name'] = 'Composer (Dev)';

/**
 * Suppress status warning.
 */
$settings['trusted_host_patterns'] = [
  '.*',
];

/**
 * Google Analytics Overrides.
 */
$config['google_analytics.settings']['account'] = "UA-99999999-99";

常用命令

Drush

# 配置管理
drush cex            # 导出配置 Drush ^8.14
drush config:export  # 导出配置 Drush 9+
# 对于 Drupal 8.6+ 的版本,导出的配置部署到不同的环境后,访问 /admin/config/development/configuration 即可确认并导入配置

常见问题

Doctrine Annotation Type Error

 

Composer 在 PHP 7.2 环境下安装的网站代码,在 PHP 7.0 的环境下会出现 Doctrine Annotation 相关 Exception 问题。

Additional uncaught exception thrown while handling exception.
Original
TypeError: Return value of Doctrine\Common\Annotations\AnnotationRegistry::reset() must be an instance of Doctrine\Common\Annotations\void, none returned in Doctrine\Common\Annotations\AnnotationRegistry::reset() (line 55 of /Users/moha/Documents/content/lightning/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php).

Doctrine\Common\Annotations\AnnotationRegistry::reset() (Line: 113)
Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery->getDefinitions() (Line: 86)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 109)
Drupal\Core\Render\ElementInfoManager->buildInfo('seven') (Line: 76)
Drupal\Core\Render\ElementInfoManager->getInfo('form') (Line: 806)
Drupal\Core\Form\FormBuilder->prepareForm('install_select_language_form', Array, Object) (Line: 272)
Drupal\Core\Form\FormBuilder->buildForm('install_select_language_form', Object) (Line: 905)
install_get_form('Drupal\Core\Installer\Form\SelectLanguageForm', Array) (Line: 1330)
install_select_language(Array) (Line: 677)
install_run_task(Array, Array) (Line: 555)
install_run_tasks(Array) (Line: 117)
install_drupal(Object) (Line: 44)
Additional
TypeError: Return value of Doctrine\Common\Annotations\AnnotationRegistry::reset() must be an instance of Doctrine\Common\Annotations\void, none returned in Doctrine\Common\Annotations\AnnotationRegistry::reset() (line 55 of /Users/moha/Documents/content/lightning/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php).

Doctrine\Common\Annotations\AnnotationRegistry::reset() (Line: 113)
Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery->getDefinitions() (Line: 86)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 109)
Drupal\Core\Render\ElementInfoManager->buildInfo('seven') (Line: 76)
Drupal\Core\Render\ElementInfoManager->getInfo('html') (Line: 298)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 139)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 140)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 66)
Drupal\Core\Render\BareHtmlPageRenderer->renderBarePage(Array, 'Error', 'install_page', Array) (Line: 76)
Drupal\Core\ProxyClass\Render\BareHtmlPageRenderer->renderBarePage(Array, 'Error', 'install_page', Array) (Line: 1011)
install_display_output(Array, Array, Array) (Line: 260)
_drupal_log_error(Array, 1) (Line: 600)
_drupal_exception_handler(Object)
 

执行 composer -vvv about,确保运行 Composer 的 PHP 的版本和网站运行的 PHP 版本一致,即 php-cli 和 php-fmp 的版本需要一致可以解决这类问题。Mac OS 下,用下面的命令选择 PHP 版本来安装。

brew install php@7.0
brew link --overwrite php@7.0 --force # Update link under /usr/local/bin & /usr/local/sbin
Author: njun
njun's picture
Updated: 2018/12/04