GinLbExtension.php in Gin Layout Builder 1.0.x
File
src/TwigExtension/GinLbExtension.php
View source
<?php
namespace Drupal\gin_lb\TwigExtension;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Template\TwigExtension;
use Twig\TwigFunction;
class GinLbExtension extends TwigExtension {
private static $ginLbClasses = NULL;
public function getFunctions() {
return [
new TwigFunction('glb_classes', [
$this,
'ginClasses',
]),
];
}
public function getName() {
return 'gin_lb';
}
private static function getGinLbClasses() {
if (GinLbExtension::$ginLbClasses === NULL) {
GinLbExtension::$ginLbClasses = json_decode(file_get_contents(__DIR__ . '/../classes.json'));
}
return GinLbExtension::$ginLbClasses;
}
public static function ginClasses(Attribute $attribute) {
$gin_lb_classes = self::getGinLbClasses();
if ($attribute
->getClass() === NULL) {
return $attribute;
}
$renamed_classes = [];
$script_classes = [
'form-autocomplete',
];
foreach ($attribute
->getClass() as $class_string) {
$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;
}
}