Skip to content

Commit cc9a120

Browse files
authored
Merge pull request #1 from cybtachyon/feature/jassmith/RefactorTwigLib
Refactored to use twig from library folder
2 parents 473020c + a6bfc9c commit cc9a120

10 files changed

+480
-84
lines changed

README.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,52 @@ When pattern configurations are saved, the template is downloaded locally (to mi
99

1010
Rendered twigs may contain drupal tokens, which are then processed in context.
1111

12+
## Installation
13+
Install the patternkit module as usual, and review the important variables below to determine if you would like to change the defaults.
14+
15+
Install the Twig library into /sites/all/libraries/Twig
16+
```
17+
git clone git://github.com/twigphp/Twig.git -b 1.x /tmp/Twig
18+
mv /tmp/Twig/lib/Twig ${DRUPALDIR}/sites/all/libraries/
19+
rm -rf /tmp/Twig
20+
```
21+
22+
The patternkit module by itself only provides the glue for other modules to present components. Define one by implementing ```hook_patternkit_library```
23+
24+
An example implementation follows
25+
```
26+
/**
27+
* Implements hook_patternkit_library().
28+
*/
29+
function webrh_patternkit_library() {
30+
$libraries = array();
31+
32+
$namespaces = array(
33+
'Web RH Patterns' => 'webrh/src/library',
34+
);
35+
36+
$module_path = drupal_get_path('module', 'webrh');
37+
foreach ($namespaces as $namespace => $path) {
38+
$lib_path = $module_path . DIRECTORY_SEPARATOR . $path;
39+
$libraries[] = new PatternkitDrupalTwigLib($namespace, $lib_path);
40+
}
41+
42+
return $libraries;
43+
}
44+
```
45+
46+
There are two different plugins currently available,
47+
* PatternkitRESTLib
48+
* PatternkitDrupalTwigLib
49+
50+
Use the former for dynamic REST based components, and the latter for locally sourced.
51+
1252
## Important Variables
13-
* patternkit_cache_enabled - Whether or not the metadata and render cache are enabled. (Disable during development)
14-
* patternkit_pl_host - The scheme://hostname:port/ of the PatternLab library host.
15-
* patternkit_default_module_ttl - How long the rendered pattern should be cached.
16-
* patternkit_show_errors - Whether or not to display messages on the site.
17-
* patternkit_log_errors - Whether or not to log errors to php error log.
53+
* ```patternkit_cache_enabled``` - Whether or not the metadata and render cache are enabled. (Disable during development)
54+
* ```patternkit_pl_host``` - The scheme://hostname:port/ of the PatternLab library host.
55+
* ```patternkit_default_module_ttl``` - How long the rendered pattern should be cached.
56+
* ```patternkit_show_errors``` - Whether or not to display messages on the site.
57+
* ```patternkit_log_errors``` - Whether or not to log errors to php error log.
1858

1959
## TODOs
2060
* https://github.com/drupal-pattern-lab/roadmap/issues/8 Solve the problem of mapping Drupal fields to pattern Variables.

include/utility.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Utility functions for Patternkit.
66
*/
77

8-
98
/**
109
* Fetch and cache assets to render this pattern.
1110
*

lib/example.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"category": "atom",
4+
"title": "Example",
5+
"type": "object",
6+
"format": "grid",
7+
"properties": {
8+
"text": {
9+
"title": "Text",
10+
"type": "string",
11+
"options": {
12+
"grid_columns": 4
13+
}
14+
}
15+
}
16+
}

lib/example.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Test sample twig template.
2+
{{- string|striptags('')|raw -}}

patternkit.info

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
name="Patternkit"
2-
description="Adds Patternkit patterns to panels as content types."
3-
package="Presentation Framework"
4-
core=7.x
1+
name = "Patternkit"
2+
description = "Adds Patternkit patterns to panels as content types."
3+
package = "Presentation Framework"
4+
core = 7.x
5+
version = 7.x-1.1
6+
57
dependencies[] = ctools
68
dependencies[] = panels
9+
710
files[] = src/PatternkitDrupalCachedLib.php
811
files[] = src/PatternkitDrupalTwigLib.php
912
files[] = src/PatternkitEditorConfig.php
1013
files[] = src/PatternkitLibInterface.php
14+
files[] = src/PatternkitTwigWrapper.php
1115
files[] = src/PatternkitPattern.php
1216
files[] = src/PatternkitRESTLib.php

patternkit.module

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,41 @@ function patternkit_flush_metadata_cache() {
6969
* An array of Pattern Kit Library objects.
7070
*/
7171
function patternkit_pattern_libraries() {
72-
// @todo Add caching.
73-
return module_invoke_all('patternkit_library');
72+
static $libraries;
73+
74+
// If undefined, create the libraries metadata.
75+
if (is_null($libraries)) {
76+
if ($cache = cache_get('patternkit_libraries')) {
77+
$libraries = $cache->data;
78+
}
79+
else {
80+
$libraries = module_invoke_all('patternkit_library');
81+
82+
if (!empty($libraries)) {
83+
// Cache for one day.
84+
cache_set('patternkit_library', $libraries, time() + 60 * 60 * 24);
85+
}
86+
}
87+
}
88+
89+
// Triggers initialization of the Twig wrapper.
90+
PatternkitTwigWrapper::getInstance($libraries);
91+
92+
return $libraries;
7493
}
7594

7695
/**
7796
* Implements hook_patternkit_library().
7897
*/
7998
function patternkit_patternkit_library() {
80-
// @todo Replace with a Service that calls a Factory.
81-
$rest_lib = new PatternkitRESTLib();
82-
$libraries = array($rest_lib);
83-
/** @var object $theme */
99+
$libraries = array();
100+
101+
// Load patterns from this module for testing.
102+
$module_path = drupal_get_path('module', 'patternkit');
103+
$lib_path = $module_path . DIRECTORY_SEPARATOR . 'lib';
104+
$libraries[] = new PatternkitDrupalTwigLib('Example', $lib_path);
105+
106+
// Load patterns from themes exposing namespaces.
84107
foreach (list_themes() as $theme_name => $theme) {
85108
if ($theme->engine !== 'twig' || !isset($theme->info['namespaces'])) {
86109
continue;

src/PatternkitDrupalCachedLib.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ public function getCachedMetadata($subtype = NULL, $reset = FALSE) {
6565
// If we are requesting data for a specific module type, return just
6666
// that data.
6767
if ($subtype !== NULL && strtolower($subtype) !== 'none') {
68-
$lookup = substr($subtype, 3);
69-
if (!empty($cached_metadata[strtolower($lookup)])) {
70-
return $cached_metadata[strtolower($lookup)];
68+
if (substr($subtype, 0, 3) == 'pk_') {
69+
$subtype = substr($subtype, 3);
70+
}
71+
if (!empty($cached_metadata[strtolower($subtype)])) {
72+
return $cached_metadata[strtolower($subtype)];
7173
}
7274
_patternkit_show_error(
7375
'Patternkit module does not appear to exist (%module), verify module info/usage.',
74-
array('%module' => $lookup)
76+
array('%module' => $subtype)
7577
);
7678

7779
return NULL;

0 commit comments

Comments
 (0)