drupal-twig-extensions
Version:
JavaScript implementation of Drupal’s Twig extensions
68 lines (64 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.options = exports.name = exports.acceptedArguments = void 0;
exports.without = without;
var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @file The without filter
*
* Docs for TwigExtension::withoutFilter (Drupal 9.3.x):
*
* ```
* new TwigFilter('without', [$this, 'withoutFilter'])
* ```
*/
const name = 'without';
exports.name = name;
const options = {
is_variadic: true
};
// The is_variadic option means this filter accepts a variable number of args.
exports.options = options;
const acceptedArguments = [];
/**
* Removes child elements from a copy of the original array.
*
* Creates a copy of the renderable array and removes child elements by key
* specified through filter's arguments. The copy can be printed without these
* elements. The original renderable array is still available and can be used
* to print child elements in their entirety in the twig template.
*
* @param {Array|Object} element
* The parent renderable array to exclude the child items.
* @param {...string|string[]} ...
* The string keys of element to prevent printing. Arguments can include
* string keys directly, or arrays of string keys to hide.
*
* @returns {Object}
* The filtered renderable array.
*/
exports.acceptedArguments = acceptedArguments;
function without(element) {
if (!element) {
return {};
}
const filteredElement = (0, _lodash.default)(element);
let args = Array.prototype.slice.call(arguments, 1);
if (args.length) {
let exclude = [];
args.forEach(name => {
if (Array.isArray(name)) {
exclude = exclude.concat(name);
} else {
exclude.push(name);
}
});
exclude.forEach(name => {
delete filteredElement[name];
});
}
return filteredElement;
}