You are here

public function TwigArray::shuffle in Bamboo Twig 8.3

Same name and namespace in other branches
  1. 8.5 bamboo_twig_extensions/src/TwigExtension/TwigArray.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigArray::shuffle()
  2. 8.2 bamboo_twig_extensions/src/TwigExtension/TwigArray.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigArray::shuffle()
  3. 8.4 bamboo_twig_extensions/src/TwigExtension/TwigArray.php \Drupal\bamboo_twig_extensions\TwigExtension\TwigArray::shuffle()

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.

Parameters

array|\Traversable $iterator: An array.

Return value

array|bool The shuffled array; or FALSE on failure.

File

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

Class

TwigArray
Provides bridge for Array functions and filters.

Namespace

Drupal\bamboo_twig_extensions\TwigExtension

Code

public function shuffle($iterator) {
  $extension = new \Twig_Extensions_Extension_Array();
  $filters = $extension
    ->getFilters();
  foreach ($filters as $filter) {
    if ($filter
      ->getName() == 'shuffle') {
      $callable = $filter
        ->getCallable();
      return $callable($iterator);
    }
  }
  return FALSE;
}