From 36c7ac1fb464244e4dc6429c74c18e5fd37dbc0b Mon Sep 17 00:00:00 2001 From: Neilime Date: Wed, 1 Jul 2020 13:40:07 +0200 Subject: [PATCH] feat(Form): add form row grouping --- src/TwbsHelper/Form/View/Helper/Form.php | 98 +++++++------------ .../Form/View/Helper/FormCollection.php | 6 +- src/TwbsHelper/View/Helper/ButtonGroup.php | 29 ------ .../View/Helper/ClassAttributeTrait.php | 8 +- .../Forms/Layout/FormGrid/HorizontalForm.php | 41 +------- 5 files changed, 46 insertions(+), 136 deletions(-) diff --git a/src/TwbsHelper/Form/View/Helper/Form.php b/src/TwbsHelper/Form/View/Helper/Form.php index 8741c18b6..ec7f43d0b 100644 --- a/src/TwbsHelper/Form/View/Helper/Form.php +++ b/src/TwbsHelper/Form/View/Helper/Form.php @@ -88,16 +88,8 @@ public function render(\Laminas\Form\FormInterface $oForm): string */ protected function renderElements(\Laminas\Form\FormInterface $oForm): string { - // Store button groups - $aButtonGroups = []; - // Store button group options - $aButtonGroupsOptions = []; - // Store elements rendering - $aElementsRendering = []; - - // Retrieve view helper plugin manager $oHelperPluginManager = $this->getView()->getHelperPluginManager(); @@ -107,15 +99,17 @@ protected function renderElements(\Laminas\Form\FormInterface $oForm): string // Retrieve form collection helper $oFormCollectionHelper = $oHelperPluginManager->get('formCollection'); - // Retrieve button group helper - $oButtonGroupHelper = $oHelperPluginManager->get('buttonGroup'); - // Store column option $bHasColumn = false; + $sRowClass = $oForm->getOption('row_class') ?? 'row'; + + // Store element rows rendering + $aRowsRendering = []; + // Prepare options $sFormLayout = $oForm->getOption('layout'); - foreach ($oForm as $iKey => $oElement) { + foreach ($oForm as $oElement) { $aOptions = $oElement->getOptions(); if (!$bHasColumn && !empty($aOptions['column'])) { @@ -126,67 +120,47 @@ protected function renderElements(\Laminas\Form\FormInterface $oForm): string if ($sFormLayout && empty($aOptions['layout'])) { $aOptions = $oElement->setOption('layout', $sFormLayout)->getOptions(); } - - // Manage button group option - if ( - $oElement instanceof \Laminas\Form\Element\Button - && !empty($buttonGroupOptions = $oButtonGroupHelper->getButtonGroupOptions($aOptions)) - ) { - $sButtonGroupKey = $buttonGroupOptions['group_name']; - - if (isset($aButtonGroups[$sButtonGroupKey])) { - $aButtonGroups[$sButtonGroupKey][] = $oElement; - } else { - $aButtonGroups[$sButtonGroupKey] = [$oElement]; - $aElementsRendering[$iKey] = $sButtonGroupKey; - } - if (!isset($aButtonGroupsOptions[$sButtonGroupKey])) { - // Only the first occured options will be set, other are ignored. - $aButtonGroupsOptions[$sButtonGroupKey] = $buttonGroupOptions['group_options']; - } - } elseif ($oElement instanceof \Laminas\Form\FieldsetInterface) { + $sRowRenderingKey = $aOptions['row_name'] ?? $sRowClass; + + if ($oElement instanceof \Laminas\Form\FieldsetInterface) { $this->setClassesToElement($oElement, ['form-group']); - $aElementsRendering[$iKey] = $oFormCollectionHelper->__invoke($oElement); + $sElementMarkup = $oFormCollectionHelper->__invoke($oElement); } else { - $aElementsRendering[$iKey] = $oFormRowHelper->__invoke($oElement); + $sElementMarkup = $oFormRowHelper->__invoke($oElement); + } + + if ($sElementMarkup) { + if(isset($aRowsRendering[$sRowRenderingKey])){ + $aRowsRendering[$sRowRenderingKey]['content'] .= PHP_EOL . $sElementMarkup; + if(!empty($aOptions['column'])){ + $aRowsRendering[$sRowRenderingKey]['hasColumn'] = true; + } + } + else{ + $aRowsRendering[$sRowRenderingKey] = [ + 'hasColumn' => !empty($aOptions['column']), + 'attributes' => $this->setClassesToAttributes([], [$sRowClass]), + 'content' => $sElementMarkup, + ]; + } } } - // Assemble elements rendering + // Assemble rows rendering $sFormContent = ''; - foreach ($aElementsRendering as $sElementRendering) { - // Check if element rendering is a button group key - if (isset($aButtonGroups[$sElementRendering])) { - $aButtons = $aButtonGroups[$sElementRendering]; - $aGroupOptions = $aButtonGroupsOptions[$sElementRendering]; - $oElement = current($aButtons); - - if ( - !empty($aGroupOptions['column']) - && $sFormLayout == self::LAYOUT_HORIZONTAL - ) { - // Make sure, that form row will also get actual row attribute - $aOptions = $oElement->setOption('column', $aGroupOptions['column'])->getOptions(); - } - - $sElementRendering = $oFormRowHelper->renderFormRow( - $oElement, - $oButtonGroupHelper($aButtons, $aGroupOptions) + foreach ($aRowsRendering as $aRowRendering) { + $sRowContent = $aRowRendering['content']; + if ($aRowRendering['hasColumn'] && self::LAYOUT_HORIZONTAL !== $sFormLayout) { + $sRowContent = $this->htmlElement( + 'div', + $aRowRendering['attributes'], + $sRowContent ); } - if ($sElementRendering) { - $sFormContent .= ($sFormContent ? PHP_EOL : '') . $sElementRendering; - } - } - if ($bHasColumn && self::LAYOUT_HORIZONTAL !== $sFormLayout) { - $sFormContent = $this->htmlElement( - 'div', - ['class' => $oForm->getOption('row_class') ?? 'row'], - $sFormContent - ); + $sFormContent .= ($sFormContent ? PHP_EOL : '') . $sRowContent; } return $sFormContent; diff --git a/src/TwbsHelper/Form/View/Helper/FormCollection.php b/src/TwbsHelper/Form/View/Helper/FormCollection.php index 4e53851cf..0d2c428e9 100644 --- a/src/TwbsHelper/Form/View/Helper/FormCollection.php +++ b/src/TwbsHelper/Form/View/Helper/FormCollection.php @@ -112,7 +112,11 @@ public function render(\Laminas\Form\ElementInterface $oElement): string } if ($sColumSize) { - $sMarkup = $this->htmlElement('div', ['class' => $this->getColumnClass($sColumSize)], $sMarkup); + $sMarkup = $this->htmlElement( + 'div', + $this->setClassesToAttributes([], [$this->getColumnClass($sColumSize)]), + $sMarkup + ); } $sMarkup = $sLegendContent . $sMarkup; diff --git a/src/TwbsHelper/View/Helper/ButtonGroup.php b/src/TwbsHelper/View/Helper/ButtonGroup.php index 2307bd31d..6ea31c216 100644 --- a/src/TwbsHelper/View/Helper/ButtonGroup.php +++ b/src/TwbsHelper/View/Helper/ButtonGroup.php @@ -136,33 +136,4 @@ public function getFormElementHelper(): \TwbsHelper\Form\View\Helper\FormElement new \TwbsHelper\Options\ModuleOptions() ); } - - /** - * Return valid button group settings or null - * - * @param array $options - * @return array|NULL - */ - public function getButtonGroupOptions(array $options): ?array - { - if ( - isset($options['button_group']) - && is_array($options['button_group']) - && isset($options['button_group']['group_name']) - && is_string($options['button_group']['group_name']) - ) { - $buttonGroupOptions = []; - $buttonGroupOptions['group_name'] = $options['button_group']['group_name']; - if ( - isset($options['button_group']['group_options']) - && is_array($options['button_group']['group_options']) - ) { - $buttonGroupOptions['group_options'] = $options['button_group']['group_options']; - } else { - $buttonGroupOptions['group_options'] = []; - } - return $buttonGroupOptions; - } - return null; - } } diff --git a/src/TwbsHelper/View/Helper/ClassAttributeTrait.php b/src/TwbsHelper/View/Helper/ClassAttributeTrait.php index a7017b81f..ccdc28803 100644 --- a/src/TwbsHelper/View/Helper/ClassAttributeTrait.php +++ b/src/TwbsHelper/View/Helper/ClassAttributeTrait.php @@ -58,11 +58,11 @@ public function setClassesToAttributes( iterable $aAttributes, iterable $aAddClasses = [], iterable $aRemoveClasses = [] - ): array { + ): iterable { $aClasses = $this->addClassesAttribute($aAttributes['class'] ?? '', $aAddClasses); if ($aClasses) { $aClasses = array_diff( - $aClasses, + is_array($aClasses) ? $aClasses : iterator_to_array($aClasses), is_array($aRemoveClasses) ? $aRemoveClasses : iterator_to_array($aRemoveClasses) ); $aAttributes['class'] = join(' ', $aClasses); @@ -74,7 +74,7 @@ protected function addClassesAttribute(string $sClassAttribute, iterable $aClass { return $this->cleanClassesAttribute(array_merge( $this->getClassesAttribute($sClassAttribute, false), - $aClasses + is_array($aClasses) ? $aClasses : iterator_to_array($aClasses) )); } @@ -82,7 +82,7 @@ protected function cleanClassesAttribute(iterable $aClasses): array { $aClasses = array_unique( array_filter( - $aClasses, + is_array($aClasses) ? $aClasses : iterator_to_array($aClasses), function ($sClass) { return !!trim($sClass); } diff --git a/tests/TestSuite/Documentation/Components/Forms/Layout/FormGrid/HorizontalForm.php b/tests/TestSuite/Documentation/Components/Forms/Layout/FormGrid/HorizontalForm.php index 1cfd053ff..ec84f12ea 100644 --- a/tests/TestSuite/Documentation/Components/Forms/Layout/FormGrid/HorizontalForm.php +++ b/tests/TestSuite/Documentation/Components/Forms/Layout/FormGrid/HorizontalForm.php @@ -102,36 +102,7 @@ ], ], ], - ], - [ - 'spec' => [ - 'type' => 'button', - 'name' => 'button1', - 'options' => [ - 'label' => 'Button 1', - 'variant' => 'primary', - 'button_group' => [ - 'group_name' => 'button-group-1', - 'group_options' => [ - 'column' => 'sm-10', - ], - ], - ], - ], - ], - [ - 'spec' => [ - 'type' => 'button', - 'name' => 'button2', - 'options' => [ - 'label' => 'Button 2', - 'variant' => 'secondary', - 'button_group' => [ - 'group_name' => 'button-group-1', - ], - ], - ], - ], + ], [ 'spec' => [ 'type' => 'submit', @@ -218,16 +189,6 @@ ' ' . PHP_EOL . ' ' . PHP_EOL . '
' . PHP_EOL . - '
' . PHP_EOL . - '
' . PHP_EOL . - ' ' . PHP_EOL . - ' ' . PHP_EOL . - '
' . PHP_EOL . - '
' . PHP_EOL . - '
' . PHP_EOL . - '
' . PHP_EOL . '
' . PHP_EOL . ' ' . PHP_EOL .