Skip to content

Commit 964840c

Browse files
committed
Refactor Patternkit libs and add Drupal Twig
feat(patternkit) Refactor the Patternkit libraries into a separate REST library, and add Drupal Twig as a supported library. Also adds a Drupal hook API for adding Patternkit libraries, and adds specific classes for handling patterns and configuration.
1 parent 66fb4a8 commit 964840c

10 files changed

+534
-125
lines changed

patternkit.api.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Hooks provided by Patternkit.
6+
*/
7+
8+
/**
9+
* @addtogroup hooks
10+
* @{
11+
*/
12+
13+
/**
14+
* Add a Drupal-cached Patternkit Library.
15+
*
16+
* @return PatternkitDrupalCachedLib[]
17+
* An array of pattern libraries.
18+
*/
19+
function hook_patternkit_library() {
20+
$rest_lib = new PatternkitRESTLib();
21+
return array($rest_lib);
22+
}
23+
24+
/**
25+
* @} End of "addtogroup hooks".
26+
*/
27+
28+
// @todo Add JSON Schema spec.

patternkit.info

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
name="Patternkit plugin - Red Hat Base"
2-
description="Adds patternkit patterns to panels as content types."
1+
name="Patternkit"
2+
description="Adds Patternkit patterns to panels as content types."
33
package="Presentation Framework"
44
core=7.x
5-
version=7.x-1.1
5+
dependencies[] = ctools
6+
dependencies[] = panels
7+
files[] = src/PatternkitDrupalCachedLib.php
8+
files[] = src/PatternkitEditorConfig.php
9+
files[] = src/PatternkitLibInterface.php
10+
files[] = src/PatternkitPattern.php
11+
files[] = src/PatternkitRESTLib.php
12+
files[] = src/PatternkitDrupalTwigLib.php

patternkit.module

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* Patternkit wrapper/abstraction for panels.
66
*/
77

8+
// Default TTL is 30 * 24 * 60 * 60, 30 minutes.
9+
const PATTERNKIT_DEFAULT_TTL = 2592000;
10+
811
/**
912
* Implements hook_ctools_plugin_directory().
1013
*/
@@ -48,7 +51,44 @@ function patternkit_cache_clear_all() {
4851
* Flush the patternkit metadata cache.
4952
*/
5053
function patternkit_flush_metadata_cache() {
51-
cache_clear_all('patternkit_pk_metadata', 'cache', FALSE);
54+
$caches = array();
55+
foreach (patternkit_pattern_libraries() as $library) {
56+
$caches[] = 'patternkit_pk_metadata_' . $library->getId();
57+
}
58+
cache_clear_all($caches, 'cache', FALSE);
59+
}
60+
61+
/**
62+
* Returns all registered pattern libraries.
63+
*
64+
* @return \PatternkitDrupalCachedLib[]
65+
* An array of Pattern Kit Library objects.
66+
*/
67+
function patternkit_pattern_libraries() {
68+
// @todo Add caching.
69+
return module_invoke_all('patternkit_library');
70+
}
71+
72+
/**
73+
* Implements hook_patternkit_library().
74+
*/
75+
function patternkit_patternkit_library() {
76+
// @todo Replace with a Service that calls a Factory.
77+
$rest_lib = new PatternkitRESTLib();
78+
$libraries = array($rest_lib);
79+
/** @var object $theme */
80+
foreach (list_themes() as $theme_name => $theme) {
81+
if ($theme->engine !== 'twig' || !isset($theme->info['namespaces'])) {
82+
continue;
83+
}
84+
foreach ($theme->info['namespaces'] as $namespace => $path) {
85+
$theme_path = dirname($theme->filename);
86+
$lib_path = $theme_path . DIRECTORY_SEPARATOR . $path;
87+
$libraries[] = new PatternkitDrupalTwigLib($namespace, $lib_path);
88+
}
89+
}
90+
91+
return $libraries;
5292
}
5393

5494
/**
@@ -59,69 +99,25 @@ function patternkit_flush_metadata_cache() {
5999
* @param bool $reset
60100
* Optionally force the meta data to be reloaded.
61101
*
62-
* @return array|object
102+
* @return array|\PatternkitPattern
63103
* Array of metadata objects found or object if specific module requested.
104+
*
105+
* @todo Add support for multiple library implementation of a pattern.
64106
*/
65107
function _patternkit_get_metadata($subtype = NULL, $reset = FALSE) {
66-
$cached_metadata = &drupal_static(__FUNCTION__);
67-
68-
// If the static cache doesn't exist, or we've called with reset, rebuild.
69-
if (!isset($cached_metadata) || $reset) {
70-
$patternkit_cache_enabled = variable_get('patternkit_cache_enabled', TRUE);
71-
// If cache is enabled, attempt to load from cache.
72-
if ($patternkit_cache_enabled && ($cache = cache_get('patternkit_pk_metadata'))) {
73-
$cached_metadata = $cache->data;
74-
}
75-
else {
76-
$patternkit_host = variable_get('patternkit_pl_host', 'http://localhost:9001');
77-
78-
$patterns = drupal_http_request(
79-
$patternkit_host . '/api/patterns',
80-
array(
81-
'headers' => array('Content-Type' => 'application/json'),
82-
'timeout' => 10,
83-
)
84-
);
85-
if (!empty($patterns) && empty($patterns->error) && $patterns->code == 200) {
86-
$cached_metadata = (array) json_decode($patterns->data);
87-
88-
// Cache the data so that we don't have to build it again.
89-
// (if cache enabled, otherwise just a slow, redundant memcache set).
90-
if ($patternkit_cache_enabled == TRUE) {
91-
cache_set('patternkit_pk_metadata', $cached_metadata, 'cache', CACHE_PERMANENT);
92-
}
93-
}
94-
else {
95-
_patternkit_show_error(
96-
'Patternkit failed to load metadata from service (%service_uri): %error',
97-
array(
98-
'%service_uri' => $patternkit_host . '/api/patterns',
99-
'%error' => !empty($patterns->error) ? $patterns->error : $patterns->code,
100-
)
101-
);
102-
return NULL;
103-
}
104-
}
105-
}
106-
107-
// If we are requesting data for a specific module type, return just
108-
// that data.
109-
if (!is_null($subtype) && strtolower($subtype) != 'none') {
110-
$lookup = substr($subtype, 3);
111-
if (!empty($cached_metadata[strtolower($lookup)])) {
112-
return $cached_metadata[strtolower($lookup)];
108+
$module = NULL;
109+
$metadata = array();
110+
foreach (patternkit_pattern_libraries() as $lib) {
111+
if ($subtype === NULL) {
112+
$metadata += $lib->getCachedMetadata(NULL, $reset);
113+
continue;
113114
}
114-
else {
115-
_patternkit_show_error(
116-
'Patternkit module does not appear to exist (%module), verify module info/usage.',
117-
array('%module' => $lookup)
118-
);
119-
120-
return NULL;
115+
$module = $lib->getCachedMetadata($subtype, $reset);
116+
if ($module !== NULL) {
117+
return $module;
121118
}
122119
}
123-
124-
return $cached_metadata;
120+
return $subtype === NULL ? $metadata : $module;
125121
}
126122

127123
/**

plugins/content_types/patternkit.inc

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function patternkit_patternkit_content_type_admin_info(
136136

137137
if (empty($module->title)) {
138138
$block->title = "BROKEN/MISSING MODULE ($subtype)";
139-
$block->content = "BROKEN/MISSING MODULE";
139+
$block->content = 'BROKEN/MISSING MODULE';
140140

141141
return $block;
142142
}
@@ -165,19 +165,19 @@ function patternkit_patternkit_content_type_edit_form($form, &$form_state) {
165165
$module = _patternkit_get_metadata($subtype);
166166

167167
// Remove the title override fields.
168-
unset($form['override_title_markup']);
169-
unset($form['override_title']);
170-
unset($form['override_title_text']);
171-
unset($form['override_title_heading']);
168+
unset($form['override_title_markup'],
169+
$form['override_title'],
170+
$form['override_title_text'],
171+
$form['override_title_heading']);
172172

173173
// @TODO: Re-enable the other formats.
174174
$form['presentation_style'] = array(
175175
'#type' => 'select',
176176
'#title' => 'Presentation style',
177177
'#options' => array(
178-
// 'webcomponent' => 'Web component',
178+
// 'webcomponent' => 'Web component',
179179
'html' => 'HTML inline',
180-
// 'json' => 'JSON (with js/css)',
180+
// 'json' => 'JSON (with js/css)',
181181
),
182182
'#default_value' => isset($conf['presentation_style']) ? $conf['presentation_style'] : '',
183183
);
@@ -210,51 +210,14 @@ function patternkit_patternkit_content_type_edit_form($form, &$form_state) {
210210
'#default_value' => isset($conf['instance_config']) ? $conf['instance_config'] : '',
211211
);
212212

213-
$patternkit_host = variable_get(
214-
'patternkit_pl_host',
215-
'http://localhost:9001'
216-
);
217-
$url = $patternkit_host . '/schema/editor/' . substr($subtype, 3);
218-
219-
if (!empty($conf['instance_config'])) {
220-
$config = json_decode($conf['instance_config']);
221-
$url .= !empty($config->lzstring) ? "?data=" . $config->lzstring : '';
222-
}
223-
224-
$markup = <<< HTML
225-
<iframe id='schema-editor-iframe' width='100%' height='1000px' src='$url'></iframe>
226-
<script>
227-
// Enlarge the ctools modal to make it easier to work with the iframe.
228-
jQuery('.ctools-modal-content').animate({width:'100%', height:'100%'});
229-
jQuery('#modalContent').animate({'width': '100%', 'left':'0px', 'top':'0px'});
230-
jQuery('#modal-content').animate({'width': '100%', 'height': '100%'});
231-
232-
var schemaDataSaved = false;
233-
// Respond to data events.
234-
window.addEventListener('message', function(event) {
235-
if (event.data.name && event.data.name === 'saveData') {
236-
var configObject = JSON.stringify(event.data);
237-
document.getElementById('schema_instance_config').value = configObject;
238-
console.log('config object', configObject);
239-
schemaDataSaved = true;
240-
jQuery('#patternkit-patternkit-content-type-edit-form').trigger('submit');
241-
}
242-
});
243-
244-
document.getElementById('patternkit-patternkit-content-type-edit-form').onsubmit = function(){
245-
if (schemaDataSaved === false) {
246-
var frame = document.getElementById('schema-editor-iframe');
247-
frame.contentWindow.postMessage('sendSaveData', '*');
248-
return false;
249-
}
250-
};
251-
</script>
252-
HTML;
253-
254-
$form['configuration'] = array(
255-
'#type' => 'markup',
256-
'#markup' => $markup,
257-
);
213+
$editor = $module->library->getEditor($subtype);
214+
if (!is_array($editor)) {
215+
$editor = array(
216+
'#type' => 'markup',
217+
'#markup' => $editor,
218+
);
219+
}
220+
$form['configuration'] = $editor;
258221

259222
return $form;
260223
}
@@ -401,20 +364,20 @@ function patternkit_patternkit_content_type_render(
401364
$content,
402365
array(
403366
'patternkit' => array(
404-
'pattern' => $subtype,
367+
'PatternkitPattern' => $subtype,
405368
'instance_id' => $instance_id,
406369
),
407370
)
408371
);
409-
//
410-
// if (!empty($config['pkdata']['attachments'])) {
411-
// $settings = _patternkit_merge_js(
412-
// $config['pkdata']['attachments'],
413-
// $base_dependencies
414-
// );
415-
// dpm($settings, 'settings');
416-
// dpm($base_dependencies, 'bd');
417-
// }
372+
//
373+
// if (!empty($config['pkdata']['attachments'])) {
374+
// $settings = _patternkit_merge_js(
375+
// $config['pkdata']['attachments'],
376+
// $base_dependencies
377+
// );
378+
// dpm($settings, 'settings');
379+
// dpm($base_dependencies, 'bd');
380+
// }
418381

419382

420383
// Build the response object.
@@ -431,7 +394,7 @@ function patternkit_patternkit_content_type_render(
431394
}
432395
else {
433396
// Default ttl to 30 days.
434-
$ttl = variable_get('patternkit_default_module_ttl', 30 * 24 * 60 * 60);
397+
$ttl = variable_get('patternkit_default_module_ttl', PATTERNKIT_DEFAULT_TTL);
435398
}
436399

437400
// Save to the cache bin (if caching is enabled).
@@ -880,7 +843,7 @@ function _patternkit_fetch_webcomponent_assets($subtype, $config, &$pk_obj) {
880843

881844
// Create the stub object.
882845
$pk_obj = (object) array(
883-
'pattern' => $subtype,
846+
'PatternkitPattern' => $subtype,
884847
'attachments' => array(),
885848
'body' => 'fragment.html',
886849
);

0 commit comments

Comments
 (0)