Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adding in lineage fix for PL when using Twig templates + line by line…
… comments to explain my PHP chicken scratch.

Addresses existing open issue #3 and corresponds with the 2nd half of work mentioned in drupal-pattern-lab/patternengine-php-twig#1
  • Loading branch information
bolt-bot committed May 10, 2017
commit d7506a09a188ed6ae3cc3fb420078d569758dbb8
45 changes: 45 additions & 0 deletions src/PatternLab/PatternData/Helpers/LineageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,51 @@ public function run() {

foreach ($foundLineages as $lineage) {

/**
* Fix for Pattern Lab Lineages when using Twig Namespaces.
* Converts the full file path to PL-friendly shorthand so
* they are internally registered.
*
* 1. Only handle instances where we aren't or can't use the
* shorthand PL path reference in templates, specifically
* in Twig / D8 when we need to use Twig namespaces in
* our template paths.
* 2. Strip off the @ sign at the beginning of our $lineage string.
* 3. Break apart the full lineage path based on any slashes that
* may exist.
* 4. Store the length of our broken up path for reference below
* 5. Store the first part of the string up to the first slash "/"
* 6. Now grab the last part of the pattern key, based on the length
* of the path we previously exploded.
* 7. Remove any "_" from pattern Name.
* 8. Remove any potential prefixed numbers or number + dash
* combos on our Pattern Name.
* 9. Strip off the pattern path extension (.twig,
* .mustache, etc) if it exists.
* 10. If the pattern name parsed had an extension,
* re-assign our Pattern Name to that.
* 11. Finally, re-assign $lineage to the default PL pattern key.
*/

if ($lineage[0] == '@') { /* [1] */
$lineage = ltrim($lineage, '@'); /* [2] */
$lineageParts = explode('/', $lineage); /* [3] */
$length = count($lineageParts); /* [4] */
$patternType = $lineageParts[0]; /* [5] */

$patternName = $lineageParts[$length - 1]; /* [6] */
$patternName = ltrim($patternName, '_'); /* [7] */
$patternName = preg_replace('/^[0-9\-]+/', '',
$patternName); /* [8] */

$patternNameStripped = explode('.' . $patternExtension, $patternName); /* [9] */

if (count($patternNameStripped) > 1) { /* [10] */
$patternName = $patternNameStripped[0];
}
$lineage = $patternType . "-" . $patternName; /* [11] */
}

if (PatternData::getOption($lineage)) {

$patternLineages[] = array("lineagePattern" => $lineage,
Expand Down