and I work in Chapter Three
as Front End Developer
Remotely
:)
Requirements:
I actually tried to refactor all the tamplates to twig and I failed miserably :(
I even tried to use many of the Drupal theme solutions...
and then just override with my stuff.... but...
So I started fresh.
some of them were too opinionated
composer create-project pattern-lab/edition-twig-standard badcamp-2018
// Watchers for everything:
gulp.task('watch', function () {
gulp.watch(config.js.src, ['legacy:js']);
gulp.watch(config.css.src, ['pl:css']);
gulp.watch(config.pattern_lab.src, ['generate:pl']);
gulp.watch(config.pattern_lab.javascript.src, ['generate:pl']);
});
// pl:php
gulp.task('pl:php', shell.task('php pattern-lab/core/console --generate'));
Example of PHP watcher:
// CSS
/dist/css/components.css
// JS
/dist/pl/js/components.js
so we can use our pattern library as a framework like bootstrap or zurb foundation etc...
I committed those files directly to the repo.
But... if you can avoid committing the compiled code and manage the releases with Tag or branches, please do it...
Editing Pattern Lab head file will include the previously mentioned files, so we can preview our pattern library in the pattern lab PHP server.
// File
/pattern-lab/source/_meta/_00-head.twig
// File
/pattern-lab/source/_meta/_00-head.twig
<!DOCTYPE html>
<html class="{{ htmlClass }}">
<head>
<title>{{ title }}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" href="../../css/pattern-scaffolding.css?{{ cacheBuster }}" media="all"/>
<link rel="stylesheet" href="../../css/components.css" media="all">
<script src="../../js/dist/pl/components.js"></script>
<!-- Begin Pattern Lab (Required for Pattern Lab to run properly) -->
{{ patternLabHead | raw }}
<!-- End Pattern Lab -->
</head>
<body class="{{ bodyClass }}">
Just create a standard Drupal theme.
The Component Libraries module was especially important because it allowed me to map the Pattern Lab components easily into my theme. Then I had to map my Pattern Lab components with the Drupal theme:
Once your Twig templates in your pattern library are working you might want to reference them with the component library and custom Twig namespaces.
// sfgovpl.info.yml
component-libraries:
protons:
paths:
- ../../../libraries/sfgov-pattern-lab/pattern-lab/source/_patterns/00-protons
atoms:
paths:
- ../../../libraries/sfgov-pattern-lab/pattern-lab/source/_patterns/01-atoms
molecules:
paths:
- ../../../libraries/sfgov-pattern-lab/pattern-lab/source/_patterns/02-molecules
organisms:
paths:
- ../../../libraries/sfgov-pattern-lab/pattern-lab/source/_patterns/03-organisms
templates:
paths:
- ../../../libraries/sfgov-pattern-lab/pattern-lab/source/_patterns/04-templates
pages:
paths:
- ../../../libraries/sfgov-pattern-lab/pattern-lab/source/_patterns/05-pages
libraries:
- sfgovpl/sfgov-pattern-lab
I'm using the ../../../ to reference the libraries forlder in the root of my Drupal install
// sfgovpl.libraries.yml
sfgov-pattern-lab:
css:
base:
'/libraries/sfgov-pattern-lab/dist/css/components.css': {}
js:
'/libraries/sfgov-pattern-lab/dist/pl/js/components.js': {}
// sfgov.info.yml
libraries:
- sfgovpl/sfgov-pattern-lab
- sfgovpl/sfgov-drupal-specific-styles
- sfgovpl/google-fonts
- sfgovpl/resize-disable
- sfgovpl/sfgov-drupal-search
- sfgovpl/fontawesome
This plugin allowed me to put the pattern library in a folder different than vendor
Then I added some configuration to the composer.json
I used the following composer plugin:
composer require oomphinc/composer-installers-extender
Under extra I specified where composer should install the repository of type github:
"extra": {
"installer-paths": {
"web/libraries/{$name}": ["type:github"],
}
Then under repositories I set the type:github
"repositories": {
"github": {
"type": "package",
"package": {
"name": "sf-digital-services/sfgov-pattern-lab",
"version": "master",
"type": "drupal-library",
"source": {
"url": "https://github.com/SFDigitalServices/sfgov-pattern-lab.git",
"type": "git",
"reference": "master"
}
}
}
}
and required the package under require: As you can see the name matches the name in the previously declared github repo:
A composer update should clone the github repo and place the Pattern Lab inside relative to the Drupal web folder:
/web/libraries/sfgov-pattern-lab
"require": {
"sf-digital-services/sfgov-pattern-lab": "dev-master",
}
// node--topic--search-index.html.twig
<div class="topic-search-result">
<div class="topic-search-result--container">
<div class="content-type">
<i class="sfgov-icon-filefilled"></i>
<span>{{ content_type }}</span>
</div>
<a class="title-url" href="{{ url }}"><h4>{{ title }}</h4></a>
<p class="body">{{ body|striptags('<a>')|raw }}</p>
</div>
</div>
{# Set variables to use in the component. #}
{% set url = path('entity.node.canonical', {'node': elements['#node'].id() }) %}
{% set description = node.get('field_description').getValue()[0]['value'] %}
{% set type = node.type.entity.label %} {# content type #}
{# Icluding the molecule in our Pattern Lab.#}
{% include "@molecules/08-search-results/03-topic-search-result.twig" with {
"content_type": type,
"url": url,
"title": elements['#node'].get('title').getString(),
"body": description
} %}
Drupal template:
The following example calls the Pattern lab molecule originally located at:
web/libraries/sfgov-pattern-lab/pattern-lab/source/_patterns/02-molecules/08-search-results/03-topic-search-result.twig
but Instead we use:
@molecules/08-search-results/03-topic-search-result.twig