Skip to content

Commit 862b9de

Browse files
authored
Merge pull request #2 from drupal-pattern-lab/develop
Merging down latest updates from Develop into Master
2 parents 49f6491 + 52f70cb commit 862b9de

File tree

11 files changed

+514
-3
lines changed

11 files changed

+514
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
vendor

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017
3+
Copyright (c) 2017
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

100644100755
Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
1-
# unified-twig-extensions
2-
Share Pattern Lab's custom Twig extensions with Drupal 8
1+
# Unified Twig Extensions
2+
3+
Share Pattern Lab's custom twigs functions, filters and tags with Drupal 8. Huzzah!
4+
5+
6+
## Getting Started
7+
Simply add to Drupal and enable the `
8+
Unified Twig Extensions` module on the `admin/modules` page to get started.
9+
10+
11+
## Note on Paths
12+
Note: currently looks for compatible extensions in your current active D8 theme path + either the 'pattern-lab/source/_twig-components' or 'source/_twig-components' folders. @TODO: allow users to customized / override this!
13+
14+
15+
## Included Examples
16+
I'm including a couple example twig extensions to add to your existing Pattern Lab-enabled theme to get started:
17+
1. `example/_twig-components/functions/link.function.php` --> example of having Drupal ignore a PL Twig extension given the link function already exists in Drupal.
18+
19+
2. `example/_twig-components/tags/grid.tag.php` and `example/_twig-components/tags/cell.tag.php` --> example of a custom Twig tag that abstracts away some of the markup involved in an ITCSS-based grid system.
20+
21+
To test this out, try adding these two custom Twig tags to your theme's existing _twig-components folder and try adding the following HTML (to both PL's twig templates and/or a Drupal template):
22+
23+
```twig
24+
{% grid 'o-grid--large' %}
25+
{% cell 'u-1/1 u-1/2@small u-2/3@medium' %}
26+
Grid cell
27+
{% endcell %}
28+
29+
{% cell 'u-1/1 u-1/2@small u-1/3@medium' %}
30+
Grid cell
31+
{% endcell %}
32+
{% endgrid %}
33+
```
34+
35+
Everything should be working as expected if you don't encounter any errors and the following HTML gets output (rendered of course):
36+
37+
```html
38+
<div class="o-grid o-grid--large">
39+
<div class="o-grid__item u-1/1 u-1/2@small u-2/3@medium">
40+
Grid cell
41+
</div>
42+
43+
<div class="o-grid__item u-1/1 u-1/2@small u-1/3@medium">
44+
Grid cell
45+
</div>
46+
</div>
47+
```

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "drupal-pattern-lab/unified-twig-extensions",
3+
"description": "Share Pattern Lab's custom Twig extensions with Drupal 8.",
4+
"type": "drupal-module",
5+
"support": {
6+
"source": "https://github.com/drupal-pattern-lab/unified-twig-extensions.git"
7+
},
8+
"authors": [{
9+
"name": "Salem Ghoweri",
10+
"role": "Contributor"
11+
}]
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
if (!class_exists('Drupal')) {
4+
$function = new Twig_SimpleFunction(
5+
'link',
6+
function ($title, $url, $attributes) {
7+
if (isset($attributes) && isset($attributes['class'])) {
8+
$classes = join(' ', $attributes['class']);
9+
return '<a href="' . $url . '" class="' . $classes . '">' . $title . '</a>';
10+
} else {
11+
return '<a href="' . $url . '">' . $title . '</a>';
12+
}
13+
},
14+
array('is_safe' => array('html'))
15+
);
16+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
// these files are loaded three times and we can't re-set a class
4+
if (!class_exists("Project_cell_Node", false)) {
5+
6+
class Project_cell_Node extends Twig_Node {
7+
8+
private $class = "";
9+
10+
public function setClass($class)
11+
{
12+
$this->class = $class;
13+
}
14+
15+
public function compile(Twig_Compiler $compiler)
16+
{
17+
$compiler->addDebugInfo($this);
18+
$compiler->write("echo \"<div class='o-grid__item ");
19+
20+
$class = "";
21+
if($this->class instanceof \Twig_Node_Expression_Constant) {
22+
$class = preg_replace("/\b(lg|md|sm|xs)([0-9]+)\b/", 'col-$1-$2', $this->class->getAttribute("value"));
23+
}
24+
$compiler->raw($class);
25+
26+
$compiler->raw("'>\";")->raw(PHP_EOL);
27+
parent::compile($compiler);
28+
$compiler->write("echo \"</div>\";")->raw(PHP_EOL);
29+
}
30+
31+
}
32+
33+
}
34+
35+
// these files are loaded three times and we can't re-set a class
36+
if (!class_exists("Project_cell_TokenParser", false)) {
37+
38+
class Project_cell_TokenParser extends Twig_TokenParser {
39+
40+
public function parse(Twig_Token $token){
41+
$inheritanceIndex = 1;
42+
43+
$stream = $this->parser->getStream();
44+
45+
$nodes = array();
46+
$classes = array();
47+
$returnNode = null;
48+
49+
if($stream->test(Twig_Token::STRING_TYPE)) {
50+
$classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression();
51+
} else {
52+
$classes[$inheritanceIndex] = "col-md-12";
53+
}
54+
55+
$stream->expect(Twig_Token::BLOCK_END_TYPE);
56+
57+
$continue = true;
58+
59+
while($continue) {
60+
61+
$content = $this->parser->subparse(array($this, 'decideMyTagFork'));
62+
$nodes[$inheritanceIndex][] = $content;
63+
$tag = $stream->next()->getValue();
64+
65+
switch($tag) {
66+
case "cell":
67+
$inheritanceIndex++;
68+
if($stream->test(Twig_Token::STRING_TYPE)) {
69+
$classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression();
70+
} else {
71+
$classes[$inheritanceIndex] = "col-md-12";
72+
}
73+
break;
74+
case "endcell":
75+
$currentNodes = $nodes[$inheritanceIndex];
76+
$class = $classes[$inheritanceIndex];
77+
unset($nodes[$inheritanceIndex]);
78+
unset($classes[$inheritanceIndex]);
79+
$inheritanceIndex--;
80+
if($inheritanceIndex == 0) {
81+
$returnNode = new Project_cell_Node($currentNodes);
82+
$returnNode->setClass($class);
83+
$continue = false;
84+
} else {
85+
$node = new Project_cell_Node($currentNodes);
86+
$node->setClass($class);
87+
$nodes[$inheritanceIndex][] = $node;
88+
}
89+
break;
90+
default:
91+
throw new Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "%s" to close the "%s" block started at line %d)', "grid", "endgrid", $this->startLine), -1);
92+
}
93+
$stream->expect(Twig_Token::BLOCK_END_TYPE);
94+
}
95+
96+
return $returnNode;
97+
}
98+
99+
public function getTag()
100+
{
101+
return "cell";
102+
}
103+
104+
/**
105+
* Callback called at each tag name when subparsing, must return
106+
* true when the expected end tag is reached.
107+
*
108+
* @param \Twig_Token $token
109+
* @return bool
110+
*/
111+
public function decideMyTagFork(Twig_Token $token)
112+
{
113+
return $token->test(array("cell", "endcell"));
114+
}
115+
116+
}
117+
118+
}
119+
120+
?>
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
// these files are loaded three times and we can't re-set a class
4+
if (!class_exists("Project_grid_Node", false)) {
5+
6+
class Project_grid_Node extends Twig_Node {
7+
8+
private $class = "";
9+
10+
public function setClass($class){
11+
$this->class = $class;
12+
}
13+
14+
15+
public function compile(Twig_Compiler $compiler){
16+
// $compiler->addDebugInfo($this);
17+
// $compiler->write("echo \"<div class='o-grid'>\";")->raw(PHP_EOL);
18+
// parent::compile($compiler);
19+
// $compiler->write("echo \"</div>\";")->raw(PHP_EOL);
20+
21+
22+
$compiler->addDebugInfo($this);
23+
$compiler->write("echo \"<div class='o-grid ");
24+
25+
$class = " ";
26+
if($this->class instanceof \Twig_Node_Expression_Constant) {
27+
$class = preg_replace("/\b(lg|md|sm|xs)([0-9]+)\b/", 'col-$1-$2', $this->class->getAttribute("value"));
28+
}
29+
$compiler->raw($class);
30+
31+
$compiler->raw("'>\";")->raw(PHP_EOL);
32+
parent::compile($compiler);
33+
$compiler->write("echo \"</div>\";")->raw(PHP_EOL);
34+
35+
}
36+
37+
}
38+
39+
}
40+
41+
// these files are loaded three times and we can't re-set a class
42+
if (!class_exists("Project_grid_TokenParser", false)) {
43+
44+
class Project_grid_TokenParser extends Twig_TokenParser {
45+
46+
public function parse(Twig_Token $token)
47+
{
48+
49+
50+
// $stream = $this->parser->getStream();
51+
// $stream->expect(Twig_Token::BLOCK_END_TYPE);
52+
// $inheritanceIndex = 1;
53+
// $nodes = array();
54+
// $classes = array();
55+
// $returnNode = null;
56+
$inheritanceIndex = 1;
57+
58+
$stream = $this->parser->getStream();
59+
60+
$nodes = array();
61+
$classes = array();
62+
$returnNode = null;
63+
64+
65+
66+
if($stream->test(Twig_Token::STRING_TYPE)) {
67+
$classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression();
68+
} else {
69+
$classes[$inheritanceIndex] = "col-md-12";
70+
}
71+
72+
$stream->expect(Twig_Token::BLOCK_END_TYPE);
73+
$continue = true;
74+
75+
76+
77+
while($continue) {
78+
79+
$content = $this->parser->subparse(array($this, 'decideMyTagFork'));
80+
$nodes[$inheritanceIndex][] = $content;
81+
$tag = $stream->next()->getValue();
82+
// $stream->expect(Twig_Token::BLOCK_END_TYPE);
83+
switch($tag) {
84+
case "grid":
85+
// $inheritanceIndex++;
86+
//
87+
// if($stream->test(Twig_Token::STRING_TYPE)) {
88+
// $classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression();
89+
// } else {
90+
// $classes[$inheritanceIndex] = "col-md-12";
91+
// }
92+
//
93+
// break;
94+
$inheritanceIndex++;
95+
if($stream->test(Twig_Token::STRING_TYPE)) {
96+
$classes[$inheritanceIndex] = $this->parser->getExpressionParser()->parseStringExpression();
97+
} else {
98+
$classes[$inheritanceIndex] = "col-md-12";
99+
}
100+
break;
101+
case "endgrid":
102+
// $currentNodes = $nodes[$inheritanceIndex];
103+
// $class = $classes[$inheritanceIndex];
104+
// unset($nodes[$inheritanceIndex]);
105+
// unset($classes[$inheritanceIndex]);
106+
// $inheritanceIndex--;
107+
// if($inheritanceIndex == 0) {
108+
// $returnNode = new Project_grid_Node($currentNodes);
109+
// $returnNode->setClass($class);
110+
// $continue = false;
111+
// // break 2;
112+
// } else {
113+
// $nodes[$inheritanceIndex][] = new Project_grid_Node($currentNodes);
114+
// $node->setClass($class);
115+
// $nodes[$inheritanceIndex][] = $node;
116+
// }
117+
// break;
118+
$currentNodes = $nodes[$inheritanceIndex];
119+
$class = $classes[$inheritanceIndex];
120+
unset($nodes[$inheritanceIndex]);
121+
unset($classes[$inheritanceIndex]);
122+
$inheritanceIndex--;
123+
if($inheritanceIndex == 0) {
124+
$returnNode = new Project_grid_Node($currentNodes);
125+
$returnNode->setClass($class);
126+
$continue = false;
127+
} else {
128+
$node = new Project_grid_Node($currentNodes);
129+
$node->setClass($class);
130+
$nodes[$inheritanceIndex][] = $node;
131+
}
132+
break;
133+
default:
134+
throw new Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "%s" to close the "%s" block started at line %d)', "endgrid", "grid", $this->startLine), -1);
135+
}
136+
$stream->expect(Twig_Token::BLOCK_END_TYPE);
137+
}
138+
139+
return $returnNode;
140+
}
141+
142+
public function getTag()
143+
{
144+
return "grid";
145+
}
146+
147+
/**
148+
* Callback called at each tag name when subparsing, must return
149+
* true when the expected end tag is reached.
150+
*
151+
* @param \Twig_Token $token
152+
* @return bool
153+
*/
154+
public function decideMyTagFork(Twig_Token $token)
155+
{
156+
return $token->test(array("grid", "endgrid"));
157+
}
158+
159+
}
160+
161+
}
162+
163+
?>

0 commit comments

Comments
 (0)