Skip to content

Commit

Permalink
feat(Form): add form row grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
neilime committed Jul 1, 2020
1 parent 1aebcef commit 36c7ac1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 136 deletions.
98 changes: 36 additions & 62 deletions src/TwbsHelper/Form/View/Helper/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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'])) {
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/TwbsHelper/Form/View/Helper/FormCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 0 additions & 29 deletions src/TwbsHelper/View/Helper/ButtonGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/TwbsHelper/View/Helper/ClassAttributeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -74,15 +74,15 @@ 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)
));
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -218,16 +189,6 @@
' </div>' . PHP_EOL .
' </div>' . PHP_EOL .
' <div class="form-group&#x20;row">' . PHP_EOL .
' <div class="col-sm-10&#x20;offset-sm-2">' . PHP_EOL .
' <div class="btn-group">' . PHP_EOL .
' <button type="button" name="button1" class="btn&#x20;btn-primary" value="">' .
'Button 1</button>' . PHP_EOL .
' <button type="button" name="button2" class="btn&#x20;btn-secondary" value="">' .
'Button 2</button>' . PHP_EOL .
' </div>' . PHP_EOL .
' </div>' . PHP_EOL .
' </div>' . PHP_EOL .
' <div class="form-group&#x20;row">' . PHP_EOL .
' <div class="col-sm-10">' . PHP_EOL .
' <button type="submit" name="submit" class="btn&#x20;btn-primary" value="">' .
'Sign in</button>' . PHP_EOL .
Expand Down

0 comments on commit 36c7ac1

Please sign in to comment.