Pattern Builder PHP Library
What is it?
- TLDR; It lets you take data from anywhere and sets it up to work with Twig rendering.
- The library is a Symfony based PHP library that contains classes for setting data in the appropriate object layout for consumption and rendering by the Twig layer.
- The Drupal Pattern Builder module uses this library to render the patterns while using Drupal to store the user entered data.
Where
Install
- Add the latest release of Pattern Builder library to your composer.json:
"require": {"pattern-builder/pattern-builder": "v1.1.0"}
- Run
composer install
- Drupal: The Drupal Pattern Builder module includes a composer.json with the library. The module is maintained to be compatible with library.
Rendering a Schema
The following details how to render an example JSON schema named "awesome".
The Drupal Pattern Builder module automatically does all this through Drupal field storage, field widgets, and field formatters.
Example JSON Schema: awesome
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Awesome",
"description": "An awesome pattern to highlight content.",
"type": "object",
"properties": {
"name": {
"type": "string",
"enum": ["awesome"],
"options": {
"hidden": true
}
},
"background": {
"title": "Background Options",
"type": "object",
"format": "grid",
"properties": {
"color": {
"type": "string",
"enum": ["white", "gray", "black", "rich-black", "red", "transparent"]
},
"isFixed" :{
"title": "Fixed Background Option",
"type": "boolean",
"default": true
}
}
},
"header": {
"title": "Header",
"type": "object",
"format": "grid",
"properties": {
"theme": {
"type": "string",
"enum": ["light", "dark"],
"default": "light"
},
"content": {
"type": "array",
"maxItems": 1,
"items": {
"title": "Header Item",
"oneOf": [
{"$ref": "awesome_header.json"}
]
}
}
}
},
"content": {
"title": "Content Items",
"type": "array",
"minItems": 1,
"items": {
"oneOf": [
{"$ref": "card.json"}
]
}
}
}
}
Create a Schema Factory
Drupal Pattern Builder Module
$schema_factory = patternbuilder_get();
Standalone Library
// Twig setup.
$twig_loader = new \Twig_Loader_Filesystem(array(
'/path/to/my/templates',
));
$twig = new \Twig_Environment($twig_loader);
// Logger: MyLogger extends \Psr\Log\AbstractLogger
$logger = new MyLogger();
// JSON Schema setup.
$retriever = new \JsonSchema\Uri\UriRetriever();
$resolver = new \JsonSchema\RefResolver($retriever);
// Pattern Builder.
$configuration = new \PatternBuilder\Configuration\Configuration($logger, $twig, $resolver);
$schema_factory = new \PatternBuilder\Schema\Schema($configuration);
Set Schema Values
// Load the awesome header schema and set values.
$awesome_header = $schema_factory->load('awesome_header')
->set('title', 'My Awesome Content')
->set('headline', 'New content, new markup, new everything!');
// Create a card schema and set values.
$card = $schema_factory->load('card')
->set('title', 'Best Content Ever')
->set('background', array('color' => 'white'))
->set('image', array('src' => 'http://example.com/cool-image.jpg'));
// Load the awesome pattern schema and set values.
$awesome = $schema_factory->load('awesome')
->set('background', array('color' => 'black'))
->set('header', array(
'theme' => 'dark',
'content' => $awesome_header
))
->set('content', $card);
Render the Schema
// Render the entire pattern - all the nested schemas and their TWIG templates.
$awesome_markup = $awesome->render();