Skip to content

Commit 04dc07f

Browse files
cyb.tachyoncybtachyon
authored andcommitted
Issue #2945643 by cyb.tachyon, jlovato, Silicon.Valet: Drupal 8 Port
1 parent a2e8cf8 commit 04dc07f

File tree

5 files changed

+67
-47
lines changed

5 files changed

+67
-47
lines changed

js/twigeditor.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
* @file
3+
* Provides Twig Pattern Library Editing Functionality.
4+
*
5+
* @external Drupal
6+
*
7+
* @external jQuery
8+
*
9+
* @external JSONEditor
10+
*/
11+
112
(function ($, Drupal) {
213
Drupal.behaviors.patternkitEditor = {
314
attach: function (context, settings) {

modules/patternkit_example/lib/patternkit/Gulpfile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* Gulp theme compilation script for rhc_ops_ui.
3+
* Gulp theme compilation script for patternkit_example.
44
*/
55

66
/* eslint-env node */
@@ -58,7 +58,7 @@ options.tslint = {
5858

5959
options.styleguide = {
6060
builder: 'node_modules/kss/builder/twig',
61-
namespace: 'rhcOpsLib:' + options.theme.patterns,
61+
namespace: 'patternkit:' + options.theme.patterns,
6262
source: [
6363
options.theme.patterns
6464
],
@@ -67,7 +67,7 @@ options.styleguide = {
6767
// The css and js paths are URLs, like '/misc/jquery.js'.
6868
// The following paths are relative to the generated style guide.
6969
css: [
70-
path.relative(options.paths.styleguide, options.theme.css + '/rhc_ops_ui.css'),
70+
path.relative(options.paths.styleguide, options.theme.css + '/patternkit.css'),
7171
path.relative(options.paths.styleguide, options.theme.css + '/patternfly.min.css'),
7272
path.relative(options.paths.styleguide, options.theme.css + '/patternfly-additions.min.css')
7373
],
@@ -91,7 +91,7 @@ options.styleguide = {
9191
path.relative(options.paths.styleguide, options.theme.js + '/patternfly-functions.min.js'),
9292
path.relative(options.paths.styleguide, options.theme.js + '/patternfly-settings.min.js')
9393
],
94-
title: 'RHC Ops UI Style Guide'
94+
title: 'Patternkit UI Style Guide'
9595
};
9696

9797
options.gulpWatchOptions = {};

src/Pattern.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Drupal\patternkit;
44

5+
use function is_string;
6+
use function PHPSTORM_META\type;
7+
58
/**
69
* A collection of a JSON schema and renderable markup.
710
*
@@ -11,35 +14,6 @@
1114
*/
1215
class Pattern {
1316

14-
/**
15-
* PatternkitPattern constructor.
16-
*
17-
* @param string $name
18-
* The name of the pattern.
19-
* @param object|array $schema
20-
* An optional JSON Schema object to use.
21-
*/
22-
public function __construct($name, $schema = NULL) {
23-
$this->name = $name;
24-
$this->category = 'default';
25-
$this->title = NULL;
26-
$this->html = NULL;
27-
$this->version = NULL;
28-
$this->attachments = NULL;
29-
$this->schema = $schema;
30-
31-
// If schema is undefined, initialize empty.
32-
if (empty($schema)) {
33-
return;
34-
}
35-
36-
// Walk the provided schemas and generate the pattern.
37-
foreach ($schema as $key => $value) {
38-
if ($key !== 'schema' && property_exists($this, (string) $key)) {
39-
$this->{$key} = $value;
40-
}
41-
}
42-
}
4317

4418
/**
4519
* The required minimum data for a pattern.
@@ -161,6 +135,36 @@ public function __construct($name, $schema = NULL) {
161135
*/
162136
protected $libraryPluginId;
163137

138+
/**
139+
* PatternkitPattern constructor.
140+
*
141+
* @param string $name
142+
* The name of the pattern.
143+
* @param object|array $schema
144+
* An optional JSON Schema object to use.
145+
*/
146+
public function __construct($name, $schema = NULL) {
147+
$this->name = $name;
148+
$this->category = 'default';
149+
$this->title = NULL;
150+
$this->html = NULL;
151+
$this->version = NULL;
152+
$this->attachments = NULL;
153+
$this->schema = $schema;
154+
155+
// If schema is undefined, initialize empty.
156+
if (empty($schema)) {
157+
return;
158+
}
159+
160+
// Walk the provided schemas and generate the pattern.
161+
foreach ($schema as $key => $value) {
162+
if ($key !== 'schema' && property_exists($this, (string) $key)) {
163+
$this->{$key} = $value;
164+
}
165+
}
166+
}
167+
164168
/**
165169
* Returns the pattern description.
166170
*
@@ -188,7 +192,7 @@ public function getId(): string {
188192
* The label of the pattern.
189193
*/
190194
public function getLabel(): string {
191-
return !empty($this->title) ? $this->title : $this->name;
195+
return !empty($this->title) && is_string($this->title) ? $this->title : $this->name;
192196
}
193197

194198
/**

src/PatternLibraryParser/TwigPatternLibraryParser.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
use Drupal\patternkit\Pattern;
1010
use Drupal\patternkit\PatternEditorConfig;
1111
use Drupal\patternkit\PatternLibraryParserBase;
12-
use function dump;
13-
use function file_exists;
14-
use function get_call_stack;
15-
use function kint_trace;
16-
use function strlen;
1712
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
1813

1914
/**
@@ -163,11 +158,6 @@ public function parseLibraryInfo($library, $path, array $library_data = []): arr
163158
if (!empty($data['json']) && $file_contents = file_get_contents($data['json'])) {
164159
$pattern = $this->createPattern($name, (array) $this->serializer::decode($file_contents) + $library_data);
165160
$pattern->name = $name;
166-
// URL is redundant for the twig based components, so we use it to
167-
// store namespace.
168-
$pattern->url = $library;
169-
// @todo add default of library version fallback to extension version.
170-
$pattern->version = $pattern->version ?? 'VERSION';
171161
}
172162
else {
173163
// Create the pattern from defaults.
@@ -190,6 +180,11 @@ public function parseLibraryInfo($library, $path, array $library_data = []): arr
190180
if (file_exists($twig_file)) {
191181
$pattern->filename = trim(substr($twig_file, strlen($path)), '/\\');
192182
$pattern->template = file_get_contents($twig_file);
183+
// URL is redundant for the twig based components, so we use it to
184+
// store namespace.
185+
$pattern->url = $library;
186+
// @todo add default of library version fallback to extension version.
187+
$pattern->version = $pattern->version ?? 'VERSION';
193188
}
194189
}
195190

src/Plugin/PatternLibrary/PatternLibraryTwig.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Drupal\patternkit\PatternEditorConfig;
1010
use Drupal\patternkit\PatternLibraryParser\TwigPatternLibraryParser;
1111
use Drupal\patternkit\PatternLibraryPluginDefault;
12+
use function print_r;
13+
use function strchr;
14+
use function strlen;
1215
use Symfony\Component\DependencyInjection\ContainerInterface;
1316

1417
/**
@@ -97,7 +100,13 @@ public static function create(ContainerInterface $container, array $configuratio
97100
public function getEditor(Pattern $pattern = NULL,
98101
PatternEditorConfig $config = NULL) {
99102
$hostname = $_SERVER['HTTP_HOST'];
100-
$pattern_schema = $pattern ? $pattern->schema : new \stdClass();
103+
$pattern_schema = $pattern->schema ?? new \stdClass();
104+
if (empty($pattern_schema['properties'])) {
105+
$filename = $pattern->filename ?? '';
106+
$filename = substr($filename, 0, -(int) strrchr($filename, '.'));
107+
$this->messenger()->addStatus($this->t('You need to manually add pattern fields since none were found in a proper JSON Schema properties value in @filename.json.',
108+
['@filename' => $filename]));
109+
}
101110
$schema_json = $this->serializer::encode($pattern_schema);
102111
$starting_json = $config !== NULL
103112
? $this->serializer::encode($config->fields)
@@ -154,9 +163,10 @@ public function getMetadata(Extension $extension, $library, $path): array {
154163
public function render(array $assets): array {
155164
$elements = [];
156165
foreach ($assets as $pattern) {
157-
if (empty($pattern->filename) || empty($pattern->config)) {
166+
if (empty($pattern->filename)) {
158167
return [];
159168
}
169+
// @todo Allow filename to cache the template contents based on settings.
160170
$template = $pattern->filename;
161171
// Add the namespace, if provided.
162172
if (!empty($pattern->url)) {
@@ -172,7 +182,7 @@ public function render(array $assets): array {
172182
/** @var \Drupal\Core\Template\TwigEnvironment $twig */
173183
$twig = \Drupal::service('twig');
174184
$template = $twig->load("$namespace/$pattern->filename");
175-
$elements[] = $template->render($pattern->config);
185+
$elements[] = $template->render($pattern->config ?? []);
176186
}
177187
return $elements;
178188
}

0 commit comments

Comments
 (0)