A Drupal 8 theme template that works with SASS and Gulp and also comes with a self-update functionality without overwriting custom theme development.
A Drupal 8 theme template that works with SASS, Bower, Gulp and **any Drupal 8 base theme** you want to use. This template also has a self-update feature built in and you should never have to copy/paste anything when setting up a custom theme for a Drupal project in the future.
# Usage
## Installation
## Basic installation
Add the following parts to your root 'composer.json' in your Drupal project:
By default, this template uses Bartik as the base theme and you can start using it in an existing Drupal project by calling:
```bash
composer require lakedrops/theme-d8-sass
```
This will add this template as a dependency to your composer.json file and create a custom theme named `mytheme` with all the settings requiered to get started immediately.
## Advanced installation
### Define the theme name
To define a specific theme name you should simply add the following section to your composer.json:
```json
{
"repositories":[
{
"type":"composer",
"url":"https://packages.drupal.org/8"
},
{
"type":"composer",
"url":"https://asset-packagist.org"
"extra":{
"theme-d8-sass":{
"project_name":"NAME_YOUR_THEME"
}
],
"require":{
"bower-asset/bootstrap-sass":"~3.3.7",
"bower-asset/flat-ui-sass":"~2.1.3",
"bower-asset/fontawesome":"~4.7.0",
"bower-asset/animate-sass":"~0.6.0",
"composer/installers":"^1.0.20",
"drupal/bootstrap":"~3.2.0",
"lakedrops/theme-d8-sass":"~0"
},
}
}
```
It is best practice to add this configuration into the composer.json before you install the theme itself, otherwise you'll end up with two custom themes, one with the default name and a second one with your own theme name.
### Define and configure the base theme
To use a different base theme than Bartik you can add the following configuration to your composer.json:
```json
{
"extra":{
"installer-paths":{
"web/themes/custom/{$name}":[
"type:drupal-custom-theme"
]
},
"theme-d8-sass":{
"project_name":"NAME_YOUR_THEME",
"base_theme":{
"name":"bootstrap",
"package":"drupal/bootstrap",
"starterkit":"starterkits/sass",
"sasspath":"scss",
"import":["overrides"]
},
}
}
}
}
```
| Key | Default | Description |
| --- | --- | --- |
| name | bartik | The Drupal project name of the base theme. |
| package | | The composer package name with namespace and package name so that Composer is able to find the package in the current installation for additional resources like starterkit. This parameter is required if you want to use any of the following settings and you have to make sure that this package is included as a requirement for your project. |
| starterkit | | If the base theme comes with a starterkit you can specify the relative path within that project where to find it. |
| sasspath | | The created custom theme is always based on a subdirectory called `sass` for all the sass or scss code. If the starterkit of the base theme uses a different directory name, then specify that here so that we can copy the files from the correct place into our sass directory. |
| import | [] | If the starterkit comes with basic files that you should import, then you can provide a list of names that will then be imported automatically. |
Note: if you're using bootstrap as your base theme, you will also have to add the bower assets for the Bootstrap library which is explained below. The same may apply for other base themes, you should always find their requirements in their own documentation. Feel free to notify us about configurations on other base themes and we'll be adding that into the documentation here as well.
### Adding bower assets
Either a base theme or your own theme development style may require some libraries that are provided by [Bower](https://bower.io/search/). To add one or more Bower assets to your theme, simply add a section like this to your composer.json:
```json
{
"extra":{
"theme-d8-sass":{
"bower_assets":{
"bootstrap-sass":{
"name":"bootstrap",
"version":"~3.3.7",
"fonts":{
"src":"assets/fonts/bootstrap"
},
...
...
@@ -55,13 +82,122 @@ Add the following parts to your root 'composer.json' in your Drupal project:
"js":{
"src":"assets/javascripts/bootstrap"
}
},
}
}
}
}
}
```
In the above example `bootstrap-sass` in the package name from the bower package repository and it contains a dictionary of values with these options:
| Key | Description |
| --- | --- |
| name | The name how to identify this asset in your own theme. |
| version | This value is optional and defaults to the `latest` if not provided. Note: if you're using global bower assets (see below), this value here is ignored. |
| fonts | A dictionary containing the source directory where to find the fonts of that package. Those fonts will be copied over into your custom theme. |
| sass | A dictionary containing the source directory where to find the sass file of that package and a list of sass files that will be imported by the sass compiler when required. Those sass file will **not** be copied to your custom theme, because they will only be required for compilation but not to deliver those files to the browser of your website visitors. |
| js | A dictionary containing the source directory where to find the javascript files of that package. Those javascript file will be copied over into your custom theme and included into the library that adds them to the sites when required. |
A bower.json file will be created dynamically when composer is executed and bower will be executed automatically to install and update all required assets locally in your theme.
### Add bower assets globally
The previous chapter describes how to add bower assets to your custom theme and without doing anything else, they will be added locally to your theme by using bower to install and update them.
If you either want to avoid that overhead of using Bower or if any of these assets will be used by multiple projects, then you could install them globally in the vendor directory of your Drupal project. This will be done by composer if you add the following settings to your composer.json in addition to the settings in the previous chapter:
```json
{
"repositories":[
{
"type":"composer",
"url":"https://asset-packagist.org"
}
],
"require":{
"bower-asset/bootstrap-sass":"~3.3.7"
}
}
```
Your custom theme will then just use the assets from the vendor directory rather than keeping them in a `bower_components` subdirectory of the theme. But you still need the `bower_assets` settings in the extra section as described above.
#### Other Bower Assets
A short list of bower assets that might be interesting to add them to your theme:
##### Flat-UI
```json
{
"require":{
"bower-asset/flat-ui-sass":"~2.1.3"
},
"extra":{
"theme-d8-sass":{
"bower_assets":{
"flat-ui-sass":{
"name":"flat-ui",
"fonts":{
"src":"vendor.assets/fonts.flat-ui"
},
"sass":{
"src":"vendor.assets/stylesheets",
"import":["flat-ui"]
},
"js":{
"src":"vendor.assets/javascripts"
}
}
}
}
}
}
```
##### FontAwesome
```json
{
"require":{
"bower-asset/fontawesome":"~4.7.0"
},
"extra":{
"theme-d8-sass":{
"bower_assets":{
"fontawesome":{
"name":"fontawesome",
"fonts":{
"src":"fonts"
},
"sass":{
"src":"scss",
"import":["font-awesome"]
}
}
}
}
}
}
```
##### Animate
```json
{
"require":{
"bower-asset/animate-sass":"~0.6.0"
},
"extra":{
"theme-d8-sass":{
"bower_assets":{
"animate-sass":{
"name":"animate",
"sass":{
"src":"",
"modules":["bounceIn"],
"import":["animate"]
"import":["animate"],
"modules":["bounceIn"]
}
}
}
...
...
@@ -70,16 +206,62 @@ Add the following parts to your root 'composer.json' in your Drupal project:
}
```
Some of those parts will most likely already in that 'composer.json' so that you only should add those that are missing.
For the animate package you have an additional list called `modules` which contains a list of animation modules that you want to enable for your custom theme.
## Add optional scripts
The custom theme will be updated when ever you either run `composer install` or `composer update` on your project. However, sometime you may only want to update the theme without running composer on the full project and you can achieve that by adding some custom scripts to your composer.json file:
You don't need them all, just add those that make sense to your development workflow. To run any of those scripts simply call `composer drupal-theme-update` which will then run that script.
Then run `composer install` and a ready-to-go theme will be created for you.
The available scripts in this package are:
| Name | Purpose |
| --- | --- |
| init | Create or update the theme with all its files and settings. Do not overwrite any custom files and finally run `npm` and/or `bower` if required. |
| update | Same as `init` just that npm and bower will be called in update mode rather than install mode. |
| reset | Same as `init` with a prior reset which will remove fonts and javascript files from previously installed bower assets in the theme. |
| overwrite | Same as `init` but it will also overwrite custom files. |
## Updating
Simply call `composer update` in the root of your project and the latest version of this plugin will be updated and the changed files will then be updated in your custom theme. No worries, the files that you edited in your theme and that are not core to this plugin, won't be overwritten, so you keep your own work easily.
## Compiling your Sass files
At the end of the update, `gulp default` will be executed which will update your fonts, javascript files and compile your Sass files into css. This is useful for deployment of Drupal sites which will make sure that the theme assets are always up to date.
The sass compiler also knows a development and a production mode. It uses development mode by default and production mode when composer is run with the `--no-dev` command line argument, which is usually the case in production environments.
# Using Gulp
Go to your theme's directory and call `gulp` once to run all of the available pre-processing. This is the equivalent of calling `gulp default`.
If Gulp is not installed globally then you need to call `node_modules/.bin/gulp` in all examples of this chapter.
Gulp by default will run the tasks `fonts`, `js` and `css` which are described in detail below.
## Gulp tasks
### Fonts
`gulp fonts` will copy all fonts from all configured bower assets into the fonts subdirectory of your custom theme.
### Javascript
`gulp js` will copy all javascript files from all configured bower assets into the js subdirectory of your custom theme.
### CSS
Go to your theme's directory and call `node_modules/.bin/gulp` once to run all of the available pre-processing.
`gulp css` will compile your sass files into css once. When adding the argument `--env production` then the output will be optimized for production environments, otherwise the output will be optimized for development.
To watch the files and automatically re-compile your Sass files when something has changed, call `node_modules/.bin/gulp watch` and you can run and forget this task.
`gulp watch` wiwll start a process that monitors your sass directory and automatically calls the css compilation when ever one or more files have changed.