You are here

class TwigArray in Bamboo Twig 8.5

Same name and namespace in other branches
  1. 8.2 bamboo_twig_extensions/src/TwigExtension/TwigArray.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigArray
  2. 8.3 bamboo_twig_extensions/src/TwigExtension/TwigArray.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigArray
  3. 8.4 bamboo_twig_extensions/src/TwigExtension/TwigArray.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigArray

Provides bridge for Array functions and filters.

Expose the features of Twig_Extensions_Extension_Array.

Hierarchy

  • class \Drupal\bamboo_twig_extensions\TwigExtension\TwigArray extends \Drupal\bamboo_twig_extensions\TwigExtension\Twig_Extension

Expanded class hierarchy of TwigArray

1 string reference to 'TwigArray'
bamboo_twig_extensions.services.yml in bamboo_twig_extensions/bamboo_twig_extensions.services.yml
bamboo_twig_extensions/bamboo_twig_extensions.services.yml
1 service uses TwigArray
bamboo_twig_extensions.twig.array in bamboo_twig_extensions/bamboo_twig_extensions.services.yml
Drupal\bamboo_twig_extensions\TwigExtension\TwigArray

File

bamboo_twig_extensions/src/TwigExtension/TwigArray.php, line 12

Namespace

Drupal\bamboo_twig_extensions\TwigExtension
View source
class TwigArray extends \Twig_Extension {

  /**
   * List of all Twig functions.
   */
  public function getFilters() {
    return [
      new TwigFilter('bamboo_extensions_shuffle', [
        $this,
        'shuffle',
      ]),
    ];
  }

  /**
   * Unique identifier for this Twig extension.
   */
  public function getName() {
    return 'bamboo_twig_extensions.twig.array';
  }

  /**
   * Shuffles an array.
   *
   * Can't use the Twig filter callback cause the shuffle function is
   * actually declared as a global function and not method of
   * Twig_Extensions_Extension_Array.
   *
   * @param array|\Traversable $array
   *   An array.
   *
   * @return array|bool
   *   The shuffled array; or FALSE on failure.
   */
  public function shuffle($array) {
    if ($array instanceof \Traversable) {
      $array = iterator_to_array($array, FALSE);
    }
    shuffle($array);
    return $array;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwigArray::getFilters public function List of all Twig functions.
TwigArray::getName public function Unique identifier for this Twig extension.
TwigArray::shuffle public function Shuffles an array.