Skip to content

Refactored to use twig from library folder #1

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 7 commits into from
Feb 6, 2018

Conversation

SiliconValet
Copy link
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.

Copy link
Collaborator

@cybtachyon cybtachyon left a comment

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!

@@ -86,8 +86,13 @@ public function getEditor($subtype = NULL,
: $config;
// @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
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
Contributor Author

Choose a reason for hiding this comment

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

boring

static $metadata;

// Use static pattern to avoid rebuilding multiple times per request.
if (is_null($metadata)) {
Copy link
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
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
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
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
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!

* Twig engine instance object.
*/
public function getTwigInstance() {
static $twig_engine;
Copy link
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
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.


// Namespacing is not necessary here, as each "engine" is unique to
// the "library".
$loader->addPath($templatesDirectory);
Copy link
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
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
Contributor Author

Choose a reason for hiding this comment

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

overload -> subclass

if ($theme->engine !== 'twig' || !isset($theme->info['namespaces'])) {
continue;
// If undefined, create the libraries metadata.
if (is_null($libraries)) {
Copy link
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
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
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

<script type="text/javascript">
let target = document.getElementById("magic-pixie-dust");
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

yes.

*/
public function renderTwigTemplate($template, array $variables = array()) {

$content = '';
Copy link
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
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
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.

}

/**
* Implements hook_patternkit_library().
*/
function patternkit_patternkit_library() {
// @todo Replace with a Service that calls a Factory.
Copy link
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
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
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.

<?php
/**
* @file
* customer-portal-kbase-copy
Copy link
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
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
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