Skip to content
Snippets Groups Projects

Issue #3421484: Create a twig function that will return list items

1 file
+ 35
0
Compare changes
  • Side-by-side
  • Inline
+ 35
0
@@ -2,6 +2,8 @@
namespace Drupal\ui_patterns\Template;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Render\Markup;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
@@ -35,6 +37,10 @@ class TwigExtension extends AbstractExtension {
$this,
'renderPatternPreview',
]),
new TwigFunction('get_list_values', [
$this,
'getListValues',
]),
];
}
@@ -93,4 +99,33 @@ class TwigExtension extends AbstractExtension {
];
}
/**
* Provides just the list values.
*
* @param array|\Drupal\Core\Render\Markup $list_values
* Array or Markup string to check.
*
* @return array
* Empty or list values from field.
*/
public function getListValues(array|Markup $list_values): array {
$list = [];
if ($list_values instanceof MarkupInterface) {
$clean_string = preg_replace('/<!--(.|\s)*?-->\s*|\r|\n/', '', $list_values);
if ($clean_string) {
$list = explode(',', $clean_string);
}
}
else {
foreach ($list_values as $key => $list_value) {
if (!str_starts_with($key, '#')) {
$list[] = $list_value;
}
}
}
return $list;
}
}
Loading