class GinLbExtension in Gin Layout Builder 1.0.x
A class providing Gin Twig function.
@package Drupal\gin_lb\TwigExtension
Hierarchy
- class \Drupal\Core\Template\TwigExtension extends \Twig\Extension\AbstractExtension
- class \Drupal\gin_lb\TwigExtension\GinLbExtension
Expanded class hierarchy of GinLbExtension
1 file declares its use of GinLbExtension
- GinLbExtensionTest.php in tests/
Unit/ TwigExtension/ GinLbExtensionTest.php
1 string reference to 'GinLbExtension'
1 service uses GinLbExtension
File
- src/
TwigExtension/ GinLbExtension.php, line 14
Namespace
Drupal\gin_lb\TwigExtensionView source
class GinLbExtension extends TwigExtension {
private static $ginLbClasses = NULL;
/**
* {@inheritdoc}
*/
public function getFunctions() {
return [
new TwigFunction('glb_classes', [
$this,
'ginClasses',
]),
];
}
/**
* {@inheritdoc}
*/
public function getName() {
return 'gin_lb';
}
/**
* Returns a list of all glb- prefixed classes.
* The classes are generated by an postcss plugin.
*
* @return []
* The classes
*/
private static function getGinLbClasses() {
if (GinLbExtension::$ginLbClasses === NULL) {
GinLbExtension::$ginLbClasses = json_decode(file_get_contents(__DIR__ . '/../classes.json'));
}
return GinLbExtension::$ginLbClasses;
}
/**
* Replaces all theme classes with gin classes.
*
* @param \Drupal\Core\Template\Attribute $attribute
* Twig attributes.
*
* @return \Drupal\Core\Template\Attribute
* The replaced attributes.
*/
public static function ginClasses(Attribute $attribute) {
$gin_lb_classes = self::getGinLbClasses();
if ($attribute
->getClass() === NULL) {
return $attribute;
}
$renamed_classes = [];
// These classes need to be kept for script purposes.
// If this class has been styled, then we'll keep both classes: the original
// and the custom glb one.
$script_classes = [
'form-autocomplete',
];
foreach ($attribute
->getClass() as $class_string) {
// $attribute->getClass returns multiple classes as one class.
// To prefix this classes - explode it.
$classes = explode(' ', $class_string);
foreach ($classes as $class) {
if (in_array('glb-' . $class, $gin_lb_classes)) {
$renamed_classes[] = 'glb-' . $class;
if (in_array($class, $script_classes)) {
$renamed_classes[] = $class;
}
}
else {
$renamed_classes[] = $class;
}
}
}
$attribute
->setAttribute('class', $renamed_classes);
return $attribute;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GinLbExtension:: |
private static | property | ||
GinLbExtension:: |
public | function |
Overrides TwigExtension:: |
|
GinLbExtension:: |
private static | function | Returns a list of all glb- prefixed classes. The classes are generated by an postcss plugin. | |
GinLbExtension:: |
public | function |
Overrides TwigExtension:: |
|
GinLbExtension:: |
public static | function | Replaces all theme classes with gin classes. | |
TwigExtension:: |
protected | property | The date formatter. | |
TwigExtension:: |
protected | property | The file URL generator. | |
TwigExtension:: |
protected | property | The renderer. | |
TwigExtension:: |
protected | property | The theme manager. | |
TwigExtension:: |
protected | property | The URL generator. | |
TwigExtension:: |
public | function | Attaches an asset library to the template, and hence to the response. | |
TwigExtension:: |
protected | function | Bubbles Twig template argument's cacheability & attachment metadata. | |
TwigExtension:: |
public | function | Creates an Attribute object. | |
TwigExtension:: |
public | function | Overrides twig_escape_filter(). | |
TwigExtension:: |
public | function | Provides a placeholder wrapper around ::escapeFilter. | |
TwigExtension:: |
public | function | Gets the name of the active theme. | |
TwigExtension:: |
public | function | Gets the path of the active theme. | |
TwigExtension:: |
public | function | ||
TwigExtension:: |
public | function | Gets a rendered link from a url object. | |
TwigExtension:: |
public | function | ||
TwigExtension:: |
public | function | Generates a URL path given a route name and parameters. | |
TwigExtension:: |
public | function | ||
TwigExtension:: |
public | function | Generates an absolute URL given a route name and parameters. | |
TwigExtension:: |
public | function | Determines at compile time whether the generated URL will be safe. | |
TwigExtension:: |
public | function | Wrapper around render() for twig printed output. | |
TwigExtension:: |
public | function | Joins several strings together safely. | |
TwigExtension:: |
public | function | Removes child elements from a copy of the original array. | |
TwigExtension:: |
public | function | Constructs \Drupal\Core\Template\TwigExtension. |