Skip to content

Refactored to use twig from library folder - #1

Merged
cybtachyon merged 7 commits into
masterfrom
feature/jassmith/RefactorTwigLib
Feb 6, 2018
Merged

cybtachyon merged 7 commits into
masterfrom
feature/jassmith/RefactorTwigLib

Conversation

@SiliconValet

Copy link
Copy Markdown
Contributor

You'll need to put Twig in the /sites/all/libraries/Twig folder

I separated the hook_patternkit_library out into a custom module, where I also supply the actual components.

Happy to do a demo with you.

@cybtachyon cybtachyon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple comments. Looks great!

Comment thread src/PatternkitDrupalTwigLib.php Outdated
// @todo Move to own JS file & Drupal Settings config var.
$markup = <<<HTML
<div id="editor_holder"></div>
<div id="magic-pixie-dust"></div>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Editor enhancements look good - but magic-pixie-dust isn't that descriptive as to what's happening here. Maybe magic-schema-container?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boring

Comment thread src/PatternkitDrupalTwigLib.php Outdated
static $metadata;

// Use static pattern to avoid rebuilding multiple times per request.
if (is_null($metadata)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be cleaner to replace this with the check-and-return anti-pattern, but may just be my personal preference.

if (!is_null($metadata)) {
  return $metadata;
}
$it = new Recurs...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/shrug

// Create the pattern from defaults.
$pattern = $this->createPattern(
(object) [
'$schema' => 'http =>//json-schema.org/draft-04/schema#',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these defaults be in the PatternkitPattern class?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That whole section needs a refactor. The problem was that some folders have a .json, but no .twig, and some vice versa. This is particularly problematic because of the nested dependencies. no_html.twig was a recurring one in particular.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I'll put it into the roadmap, thanks!

Comment thread src/PatternkitDrupalTwigLib.php Outdated
* Twig engine instance object.
*/
public function getTwigInstance() {
static $twig_engine;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be determinate on if tfd7 is already loaded or not? I'm wondering about the overhead of potentially bootstrapping the twig engine twice, although we probably need to test if it actually makes an impact or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced tfd7 entirely, but now that I think of it, you guys probably use it for other stuff. TFD7 does a /lot/ of extra work here.

Invoking twig with the namespacing requirements means you're paying for the class invocation regardless of approach, so at the very least this work is siloed from that code.

Comment thread src/PatternkitDrupalTwigLib.php Outdated

// Namespacing is not necessary here, as each "engine" is unique to
// the "library".
$loader->addPath($templatesDirectory);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested yet, but does this preclude support twig libraries that depend on namespaces?

Will update review comment if I get a chance to test it out before you get to this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, you can add a namespace here. I just didn't because then I'd have to overload the FileLoader. I actually started to do that and then realized that I didn't need them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overload -> subclass

Comment thread patternkit.module
if ($theme->engine !== 'twig' || !isset($theme->info['namespaces'])) {
continue;
// If undefined, create the libraries metadata.
if (is_null($libraries)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use if ($libraries === null) { here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are the coding standards you are following?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://mojo.redhat.com/docs/DOC-1133285 is_null is typically an unncessary function call, although I think PHP5.4+ inlines it before running

Comment thread src/PatternkitDrupalTwigLib.php Outdated
<div id="editor_holder"></div>
<div id="magic-pixie-dust"></div>
<script type="text/javascript">
let target = document.getElementById("magic-pixie-dust");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we risking compatibility issues by using let here and not using a transpiler?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.

Comment thread src/PatternkitDrupalTwigLib.php Outdated
*/
public function renderTwigTemplate($template, array $variables = array()) {

$content = '';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary variable declaration - remove if convenient.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not unnecessary in the exception short circuit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scratch that, it looks like I fixed that at some point.

Comment thread patternkit.module
* Implements hook_patternkit_library().
*/
function patternkit_patternkit_library() {
// @todo Replace with a Service that calls a Factory.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be removing support for themes to add pattern libraries? Maybe make it an option?

My reasoning is in Drupal 8, adding the 'namespace' key to the theme is the generally accepted way to add component libraries via the Component Libraries module.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it sounds good to you, I'll get a patch going for the following:

  • Populate libraries from both hook usage as well as theme namespaces
  • Set the default 'enabled' state of each library based on module/theme 'enabled' state
  • Allow overriding the 'enabled' state of each library in the admin UI / via variables

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the namespace discovery stuff. no reason not to allow both.

Comment thread src/PatternkitTwigWrapper.php Outdated
<?php
/**
* @file
* customer-portal-kbase-copy

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this annotation here?

// Create the pattern from defaults.
$pattern = $this->createPattern(
(object) [
'$schema' => 'http =>//json-schema.org/draft-04/schema#',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I'll put it into the roadmap, thanks!

@cybtachyon

Copy link
Copy Markdown
Collaborator

Looks good to me! Thanks again; ready to merge.

@cybtachyon
cybtachyon merged commit cc9a120 into master Feb 6, 2018
@cybtachyon
cybtachyon deleted the feature/jassmith/RefactorTwigLib branch February 6, 2018 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants