Last active
May 2, 2016 18:57
-
-
Save davidDuymelinck/2c08945b889c6087a810c8442521c104 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$form = [ | |
1 => [ | |
'step_name' => 'step 1', | |
'sections' => [ | |
[ | |
'section_name' => 'section 1', | |
'fields' => [ | |
'field_1' => [], | |
'field_2' => [] | |
] | |
] | |
] | |
] | |
]; | |
foreach($form as $step => $stepSettings) { | |
foreach($stepSettings['sections'] as $sectionKey => $sectionSettings) { | |
foreach($sectionSettings['fields'] as $fieldName => $fieldSettings) { | |
$form[$step]['sections'][$sectionKey]['fields'][$fieldName]['visible'] = false; | |
} | |
$hiddenFieldCount = 0; | |
foreach($form[$step]['sections'][$sectionKey]['fields'] as $fieldName => $fieldSettings) { | |
if($fieldSettings['visible'] == false) { | |
$hiddenFieldCount++; | |
} | |
} | |
$form[$step]['sections'][$sectionKey]['visible'] = $hiddenFieldCount < count($sectionSettings['fields']); | |
} | |
$hiddenSectionCount = 0; | |
foreach($form[$step]['sections'] as $sectionKey => $sectionSettings) { | |
if($sectionSettings['visible'] == false) { | |
$hiddenSectionCount++; | |
} | |
} | |
$form[$step]['visible'] = $hiddenSectionCount < count($form[$step]['sections']); | |
} | |
var_dump($form); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\array_manipulator\ArrayManipulator; | |
$arrayManipulator = new ArrayManipulator([ | |
1 => [ | |
'step_name' => 'step 1', | |
'sections' => [ | |
[ | |
'section_name' => 'section 1', | |
'fields' => [ | |
'field_1' => [], | |
'field_2' => [] | |
] | |
] | |
] | |
] | |
]); | |
$arrayManipulator | |
->addManipulator(function($array) { | |
return array_map(function($item){ | |
$item['visible'] = false; | |
return $item; | |
}, $array); | |
}, 3, 'fields') | |
->addManipulator(function($array) { | |
foreach($array as $sectionKey => $sectionSettings) { | |
$hiddenFieldCount = 0; | |
foreach($sectionSettings['fields'] as $fieldName => $fieldSettings) { | |
if($fieldSettings['visible'] == false) { | |
$hiddenFieldCount++; | |
} | |
} | |
$array[$sectionKey]['visible'] = $hiddenFieldCount < count($sectionSettings['fields']); | |
} | |
return $array; | |
}, 2) | |
->addManipulator(function($array) { | |
$hiddenSectionCount = 0; | |
foreach($array['sections'] as $sectionKey => $sectionSettings) { | |
if($sectionSettings['visible'] == false) { | |
$hiddenSectionCount++; | |
} | |
} | |
$array['visible'] = $hiddenSectionCount < count($array['sections']); | |
return $array; | |
}, 1); | |
var_dump($arrayManipulator->manipulate()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment