Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Add support for drupal attributes in configuration files #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 23 additions & 42 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _ = require('lodash');
const fs = require('fs');
const Path = require('path');
const utils = Fractal.utils;
const drupalAttribute = require('drupal-attribute');

class TwigAdapter extends Fractal.Adapter {

Expand Down Expand Up @@ -177,59 +178,19 @@ class TwigAdapter extends Fractal.Adapter {
}

render(path, str, context, meta) {
let attributes = new AttributesObject();
let self = this;

meta = meta || {};

function AttributesObject() {
let self = this;
this.classes = '';
this.attr = [];

this.addClass = function(...str) {
// console.log(str);
self.classes = _.flatten(str).join(' ');

return self;
};

this.removeClass = function(...str) {
// todo implement
// self.classes = str.join(' ');

return self;
};

this.setAttribute = function(attribute, value) {
let str = `${attribute}="${value}"`;

self.attr.push(str);
self.attr = _.uniq(self.attr);

return self;
};
}

AttributesObject.prototype.toString = function toString() {
let attrList = [
this.classes ? `class="${this.classes}"` : '',
this.attr ? this.attr.join(' ') : '',
];

return attrList.join(' ');
};



if (!this._config.pristine) {
setEnv('_self', meta.self, context);
setEnv('_target', meta.target, context);
setEnv('_env', meta.env, context);
setEnv('_config', this._app.config(), context);
setEnv('title_prefix', '', context);
setEnv('title_suffix', '', context);
setEnv('attributes', attributes, context);
preprocessAttributes(context);

}

return new Promise(function(resolve, reject){
Expand All @@ -256,6 +217,26 @@ class TwigAdapter extends Fractal.Adapter {
context[key] = value;
}
}

/**
* This function go though in the context object and search for keys,
* which start with the '_' character and ends with the '_attributes' string.
* When it finds these, it convert the related object into Drupal Attribute.
* This function is recursive.
*
* @param context
*/
function preprocessAttributes(context) {

// Go though the contexts by its keys.
Object.keys(context).forEach(function (context_key) {
if (context_key.charAt(0) === '_' && context_key.endsWith('_attributes') && typeof context[context_key] == 'object') {
context[context_key.substr(1)] = new drupalAttribute(new Map(Object.entries(context[context_key])));
} else if (context_key.charAt(0) !== '_' && typeof context[context_key] == 'object') {
preprocessAttributes(context[context_key]);
}
});
}
}

}
Expand Down