Skip to content

Fix for Broken Lineage Functionality in PL When Using Twig templates w/ Path Namespaces (ie. Drupal 8-friendly Paths) #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2017
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
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