Skip to content

Commit 473020c

Browse files
committed
Add twig pattern tfd7 render support
feat(patternkit) Add support for twig patterns to be rendered with the Twig for Drupal 7 theme engine.
1 parent 6cfcab3 commit 473020c

File tree

8 files changed

+161
-33
lines changed

8 files changed

+161
-33
lines changed

include/utility.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ function _patternkit_merge_js(array $js1, array $js2) {
524524
* @return array|\PatternkitPattern
525525
* Array of metadata objects found or object if specific module requested.
526526
*
527-
* @todo Add support for multiple library implementation of a pattern.
527+
* @todo Add support for multiple library implementations of a pattern.
528+
* AKA namespacing.
528529
*/
529530
function _patternkit_get_metadata($subtype = NULL, $reset = FALSE) {
530531
$metadata = array();

plugins/content_types/patternkit.inc

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -420,24 +420,19 @@ function _patternkit_render_block(
420420
$module,
421421
$instance_id,
422422
array $config,
423-
array $context = NULL
424-
) {
425-
if (isset($module->subtype)) {
426-
$subtype = $module->subtype;
427-
}
428-
else {
429-
return 'MISSING/BROKEN';
430-
}
423+
array $context = NULL) {
431424

432-
if ($config['presentation_style'] === 'webcomponent') {
433-
$subtype = substr($subtype, 3);
434-
$body = "<$subtype-pattern></$subtype-pattern>";
425+
$body = 'MISSING/BROKEN';
426+
if (!isset($module->subtype)) {
427+
return $body;
435428
}
436-
else {
437-
$filename = "public://patternkit/$subtype/{$config['instance_id']}/body.html";
438-
$body = file_get_contents($filename);
429+
$subtype = $module->subtype;
430+
$pattern = _patternkit_get_metadata($subtype);
431+
if (!$pattern) {
432+
return $body;
439433
}
440-
return $body;
434+
// @todo Handle context variable replacements.
435+
return $pattern->getRenderedMarkup(new PatternkitEditorConfig($config));
441436
}
442437

443438
/**

src/PatternkitDrupalCachedLib.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ abstract class PatternkitDrupalCachedLib implements PatternkitLibInterface {
1313
*/
1414
abstract protected function getRawMetadata();
1515

16+
/**
17+
* Creates a new pattern in this library.
18+
*
19+
* @param object|array $schema
20+
* Optional JSON Schema to preset the pattern.
21+
*
22+
* @return \PatternkitPattern
23+
* The new pattern.
24+
*/
25+
public function createPattern($schema = array()) {
26+
return new PatternkitPattern($this, $schema);
27+
}
28+
1629
/**
1730
* Utility function to get specified Patternkit module metadata.
1831
*

src/PatternkitDrupalTwigLib.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function __construct($title, $path) {
3838
*/
3939
public function fetchPatternAssets(PatternkitPattern $pattern,
4040
\PatternkitEditorConfig $config) {
41+
// @todo Add support for twig lib attachments such as JS and images.
42+
$pattern->attachments = array();
4143
return $pattern;
4244
}
4345

@@ -145,6 +147,7 @@ protected function getRawMetadata() {
145147
$dirs = explode(DIRECTORY_SEPARATOR, $file_path);
146148
// All JSON schema must be in an 'api' folder at this time.
147149
// @todo Add support for custom setups.
150+
// @todo Look at a standard for JSON Schema + JSON Sample data.
148151
$num_dirs = count($dirs);
149152
if ($num_dirs < 2
150153
|| array_pop($dirs) !== 'api') {
@@ -155,16 +158,16 @@ protected function getRawMetadata() {
155158
continue;
156159
}
157160
if ($file_contents = file_get_contents($file)) {
158-
$pattern = new PatternkitPattern(json_decode($file_contents));
159-
$pattern->library = &$this;
161+
$pattern = $this->createPattern(json_decode($file_contents));
160162
$file_basename = $file->getBasename('.json');
161163
$subtype = "pk_$file_basename";
162164
$pattern->subtype = $subtype;
163165
$pattern->url = url("patternkit/ajax/$id/$subtype/schema");
164166
$twig_file = $file_path
165167
. DIRECTORY_SEPARATOR . $file_basename . '.twig';
166168
if (file_exists($twig_file)) {
167-
$pattern->twig = file_get_contents($twig_file);
169+
$pattern->filename = $twig_file;
170+
$pattern->template = file_get_contents($twig_file);
168171
}
169172
$metadata[$file_basename] = $pattern;
170173
}
@@ -181,4 +184,25 @@ protected function getRawMetadata() {
181184
return $metadata;
182185
}
183186

187+
/**
188+
* Returns rendered markup for a provided pattern.
189+
*
190+
* @param \PatternkitPattern $pattern
191+
* The pattern to render.
192+
* @param \PatternkitEditorConfig $config
193+
* The editor configuration for the pattern.
194+
*
195+
* @return string
196+
* The rendered pattern HTML.
197+
*/
198+
public function getRenderedPatternMarkup(PatternkitPattern $pattern,
199+
PatternkitEditorConfig $config) {
200+
if (empty($pattern->filename) || empty($config->fields)) {
201+
return '';
202+
}
203+
$template = $pattern->filename;
204+
$variables = $config->fields;
205+
return twig_render_template($template, $variables);
206+
}
207+
184208
}

src/PatternkitEditorConfig.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ class PatternkitEditorConfig extends stdClass {
1313
*/
1414
public function __construct($config = array()) {
1515
foreach ($config as $property => $value) {
16-
if (property_exists($this, $property)) {
16+
if (property_exists($this, (string) $property)) {
1717
$this->{$property} = $value;
1818
unset($config->{$property});
1919
}
2020
}
21-
$this->fields->misc_data = !empty($config) ? $config : '';
2221
}
2322

2423
/**

src/PatternkitLibInterface.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
*/
66
interface PatternkitLibInterface {
77

8+
/**
9+
* Creates a new pattern in this library.
10+
*
11+
* @param object|array $schema
12+
* Optional JSON Schema to preset the pattern.
13+
*
14+
* @return \PatternkitPattern
15+
* The new pattern.
16+
*/
17+
public function createPattern($schema = array());
18+
819
/**
920
* Fetches all assets for a pattern.
1021
*
@@ -19,6 +30,20 @@ interface PatternkitLibInterface {
1930
public function fetchPatternAssets(PatternkitPattern $pattern,
2031
\PatternkitEditorConfig $config);
2132

33+
/**
34+
* Returns rendered markup for a provided pattern.
35+
*
36+
* @param \PatternkitPattern $pattern
37+
* The pattern to render.
38+
* @param \PatternkitEditorConfig $config
39+
* The editor configuration for the pattern.
40+
*
41+
* @return string
42+
* The rendered pattern HTML.
43+
*/
44+
public function getRenderedPatternMarkup(PatternkitPattern $pattern,
45+
PatternkitEditorConfig $config);
46+
2247
/**
2348
* Returns the id of the Pattern Library.
2449
*

src/PatternkitPattern.php

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,44 @@ class PatternkitPattern {
88
/**
99
* PatternkitPattern constructor.
1010
*
11+
* @param \PatternkitLibInterface $library
12+
* The library the pattern belongs to.
13+
*
1114
* @param object|array $schema
1215
* An optional JSON Schema object to use.
1316
*/
14-
public function __construct($schema = array()) {
17+
public function __construct(PatternkitLibInterface $library, $schema = array()) {
1518
$this->subtype = NULL;
1619
$this->title = NULL;
1720
$this->html = NULL;
18-
$this->library = NULL;
1921
$this->version = NULL;
2022
$this->attachments = NULL;
2123
$this->schema = $schema;
2224
if (empty($schema)) {
2325
return;
2426
}
2527
foreach ($schema as $key => $value) {
26-
if ($key !== 'schema' && property_exists($this, $key)) {
28+
if ($key !== 'schema' && property_exists($this, (string) $key)) {
2729
$this->{$key} = $value;
2830
}
2931
}
32+
$this->library = $library;
3033
}
3134

35+
/**
36+
* The required minimum data for a pattern.
37+
*
38+
* @ingroup required
39+
* @{
40+
*/
41+
42+
/**
43+
* The Patternkit Library that loaded the pattern.
44+
*
45+
* @var \PatternkitLibInterface
46+
*/
47+
public $library;
48+
3249
/**
3350
* The subtype for the pattern. Typically "pk_$pattern".
3451
*
@@ -46,11 +63,8 @@ public function __construct($schema = array()) {
4663
public $title;
4764

4865
/**
49-
* An override TTL in ms for the pattern.
50-
*
51-
* @var int
66+
* @}
5267
*/
53-
public $ttl;
5468

5569
/**
5670
* Optional array of attached assets.
@@ -83,18 +97,18 @@ public function __construct($schema = array()) {
8397
public $body;
8498

8599
/**
86-
* Pre-rendered HTML using the configuration object, if available.
100+
* The pattern template file.
87101
*
88102
* @var string
89103
*/
90-
public $html;
104+
public $filename;
91105

92106
/**
93-
* The Patternkit Library that loaded the pattern.
107+
* Pre-rendered HTML using the configuration object, if available.
94108
*
95-
* @var \PatternkitLibInterface
109+
* @var string
96110
*/
97-
public $library;
111+
public $html;
98112

99113
/**
100114
* The JSON Schema for the pattern.
@@ -105,6 +119,22 @@ public function __construct($schema = array()) {
105119
*/
106120
public $schema;
107121

122+
/**
123+
* Optional pattern template contents.
124+
*
125+
* Typically Twig or Nunjucks, others include Handlebars and Jinja.
126+
*
127+
* @var string
128+
*/
129+
public $template;
130+
131+
/**
132+
* An override TTL in ms for the pattern.
133+
*
134+
* @var int
135+
*/
136+
public $ttl;
137+
108138
/**
109139
* The API URL for the pattern.
110140
*
@@ -132,4 +162,17 @@ public function fetchAssets(PatternkitEditorConfig $config) {
132162
return $this;
133163
}
134164

165+
/**
166+
* Renders the pattern and returns the generated markup.
167+
*
168+
* @param \PatternkitEditorConfig $config
169+
* The editor configuration for the pattern.
170+
*
171+
* @return string
172+
* The rendered HTML pattern markup.
173+
*/
174+
public function getRenderedMarkup(PatternkitEditorConfig $config) {
175+
return $this->library->getRenderedPatternMarkup($this, $config);
176+
}
177+
135178
}

src/PatternkitRESTLib.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ public function getId() {
142142
return 'patternkit_rest_api';
143143
}
144144

145+
/**
146+
* Returns rendered markup for a provided pattern.
147+
*
148+
* @param \PatternkitPattern $pattern
149+
* The pattern to render.
150+
* @param \PatternkitEditorConfig $config
151+
* The editor configuration for the pattern.
152+
*
153+
* @return string
154+
* The rendered pattern HTML.
155+
*/
156+
public function getRenderedPatternMarkup(PatternkitPattern $pattern,
157+
PatternkitEditorConfig $config) {
158+
$subtype = $pattern->subtype;
159+
if (empty($config['presentation_style']) || empty($config['instance_id'])) {
160+
return '';
161+
}
162+
if ($config['presentation_style'] === 'webcomponent') {
163+
$subtype = substr($subtype, 3);
164+
$body = "<$subtype-pattern></$subtype-pattern>";
165+
}
166+
else {
167+
$filename = "public://patternkit/$subtype/{$config['instance_id']}/body.html";
168+
$body = file_get_contents($filename);
169+
}
170+
return $body;
171+
}
172+
145173
/**
146174
* Returns the title of the Pattern Library.
147175
*

0 commit comments

Comments
 (0)