You are here

class GinLbExtension in Gin Layout Builder 1.0.x

A class providing Gin Twig function.

@package Drupal\gin_lb\TwigExtension

Hierarchy

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'
gin_lb.services.yml in ./gin_lb.services.yml
gin_lb.services.yml
1 service uses GinLbExtension
gin_lb.twig in ./gin_lb.services.yml
Drupal\gin_lb\TwigExtension\GinLbExtension

File

src/TwigExtension/GinLbExtension.php, line 14

Namespace

Drupal\gin_lb\TwigExtension
View 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

Namesort descending Modifiers Type Description Overrides
GinLbExtension::$ginLbClasses private static property
GinLbExtension::getFunctions public function Overrides TwigExtension::getFunctions
GinLbExtension::getGinLbClasses private static function Returns a list of all glb- prefixed classes. The classes are generated by an postcss plugin.
GinLbExtension::getName public function Overrides TwigExtension::getName
GinLbExtension::ginClasses public static function Replaces all theme classes with gin classes.
TwigExtension::$dateFormatter protected property The date formatter.
TwigExtension::$fileUrlGenerator protected property The file URL generator.
TwigExtension::$renderer protected property The renderer.
TwigExtension::$themeManager protected property The theme manager.
TwigExtension::$urlGenerator protected property The URL generator.
TwigExtension::attachLibrary public function Attaches an asset library to the template, and hence to the response.
TwigExtension::bubbleArgMetadata protected function Bubbles Twig template argument's cacheability & attachment metadata.
TwigExtension::createAttribute public function Creates an Attribute object.
TwigExtension::escapeFilter public function Overrides twig_escape_filter().
TwigExtension::escapePlaceholder public function Provides a placeholder wrapper around ::escapeFilter.
TwigExtension::getActiveTheme public function Gets the name of the active theme.
TwigExtension::getActiveThemePath public function Gets the path of the active theme.
TwigExtension::getFilters public function
TwigExtension::getLink public function Gets a rendered link from a url object.
TwigExtension::getNodeVisitors public function
TwigExtension::getPath public function Generates a URL path given a route name and parameters.
TwigExtension::getTokenParsers public function
TwigExtension::getUrl public function Generates an absolute URL given a route name and parameters.
TwigExtension::isUrlGenerationSafe public function Determines at compile time whether the generated URL will be safe.
TwigExtension::renderVar public function Wrapper around render() for twig printed output.
TwigExtension::safeJoin public function Joins several strings together safely.
TwigExtension::withoutFilter public function Removes child elements from a copy of the original array.
TwigExtension::__construct public function Constructs \Drupal\Core\Template\TwigExtension.