From ed84ab3639bae98eff6bd7e06838baa632efb769 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 23 Oct 2022 22:51:13 -0400 Subject: [PATCH] Afform - Ensure container elements like
always have #children even if empty --- ext/afform/core/CRM/Afform/ArrayHtml.php | 4 ++++ .../tests/phpunit/Civi/Afform/AfformGetTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/ext/afform/core/CRM/Afform/ArrayHtml.php b/ext/afform/core/CRM/Afform/ArrayHtml.php index 44eec67092..2425de090a 100644 --- a/ext/afform/core/CRM/Afform/ArrayHtml.php +++ b/ext/afform/core/CRM/Afform/ArrayHtml.php @@ -236,6 +236,10 @@ class CRM_Afform_ArrayHtml { $arr['#children'] = $this->convertNodesToArray($node->childNodes); } } + // Empty containers should still get a #children attribute + elseif (in_array($node->tagName, ['div', 'fieldset'], TRUE)) { + $arr['#children'] = []; + } return $arr; } elseif ($node instanceof DOMText) { diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php index 3218f02869..f84eae063d 100644 --- a/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php +++ b/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetTest.php @@ -55,6 +55,22 @@ class AfformGetTest extends \PHPUnit\Framework\TestCase implements HeadlessInter $this->assertArrayNotHasKey('base_module', $result); } + public function testGetLayoutWithEmptyNode() { + Afform::create(FALSE) + ->addValue('name', $this->formName) + ->addValue('title', 'Test Form') + ->addValue('layout', '
') + ->execute(); + + $layout = Afform::get(FALSE) + ->addWhere('name', '=', $this->formName) + ->execute()->single()['layout']; + + // Ensure container elements like
always have #children even if empty + $this->assertEquals([], $layout[0]['#children'][1]['#children']); + $this->assertArrayNotHasKey('#children', $layout[0]['#children'][0]); + } + public function testAfformAutocomplete(): void { $title = uniqid(); Afform::create() -- 2.25.1