From ec4504e017a18837eb3429becbd1c6e1c0bc708f Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 26 Oct 2018 12:56:07 -0600 Subject: [PATCH] fix for #2236 + add new `|nested` filter --- CHANGELOG.md | 8 ++++++++ classes/Twig/AdminTwigExtension.php | 18 ++++++++++++++++++ .../templates/forms/fields/list/list.html.twig | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6598a0a5..4f6df933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# v1.8.13 +## mm/dd/2018 + +1. [](#new) + * Added new `|nested()` Twig filter to access array objects with dot notation syntax +1. [](#bugfix) + * Fixed issue with complex lists structure and nested dot-notation [#2236](https://github.com/getgrav/grav/issues/2236) + # v1.8.12 ## 10/24/2018 diff --git a/classes/Twig/AdminTwigExtension.php b/classes/Twig/AdminTwigExtension.php index 15d1eeab..e0862b92 100644 --- a/classes/Twig/AdminTwigExtension.php +++ b/classes/Twig/AdminTwigExtension.php @@ -31,6 +31,7 @@ class AdminTwigExtension extends \Twig_Extension new \Twig_SimpleFilter('toYaml', [$this, 'toYamlFilter']), new \Twig_SimpleFilter('fromYaml', [$this, 'fromYamlFilter']), new \Twig_SimpleFilter('adminNicetime', [$this, 'adminNicetimeFilter']), + new \Twig_SimpleFilter('nested', [$this, 'nestedFilter']), ]; } @@ -42,6 +43,23 @@ class AdminTwigExtension extends \Twig_Extension ]; } + public function nestedFilter($current, $name) + { + $path = explode('.', trim($name, '.')); + + foreach ($path as $field) { + if (is_object($current) && isset($current->{$field})) { + $current = $current->{$field}; + } elseif (is_array($current) && isset($current[$field])) { + $current = $current[$field]; + } else { + return null; + } + } + + return $current; + } + public function cloneFunc($obj) { return clone $obj; diff --git a/themes/grav/templates/forms/fields/list/list.html.twig b/themes/grav/templates/forms/fields/list/list.html.twig index 764f5ed4..806d367b 100644 --- a/themes/grav/templates/forms/fields/list/list.html.twig +++ b/themes/grav/templates/forms/fields/list/list.html.twig @@ -77,7 +77,7 @@ {% set childName = itemName -%} {%- elseif childName starts with '.' -%} {% set childKey = childName|trim('.') %} - {% set childValue = val[childName[1:]] %} + {% set childValue = val|nested(childName) %} {% set childName = itemName ~ childName %} {% else %} {% set childKey = childName %}