Created
May 14, 2019 16:51
-
-
Save rasteiner/26987241614fcfa662a6ebec92be832f to your computer and use it in GitHub Desktop.
fields shortcut
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 Kirby\Cms\PageBlueprint; | |
class shortcutsPreset { | |
static private $counter = 0; | |
static private function randomSectionName() { | |
self::$counter += 1; | |
return 'shortcuts_preset_section_' . self::$counter; | |
} | |
static private function replace_key($arr, $oldkey, $newkey) { | |
if (array_key_exists($oldkey, $arr)) { | |
$keys = array_keys($arr); | |
$keys[array_search($oldkey, $keys)] = $newkey; | |
return array_combine($keys, $arr); | |
} | |
return $arr; | |
} | |
static public function preset($props) { | |
//normalize props | |
if(isset($props['fields'])) { | |
$props['sections'][self::randomSectionName()] = [ | |
'type' => 'fields', | |
'fields' => $props['fields'] | |
]; | |
unset($props['fields']); | |
} | |
if(isset($props['sections'])) { | |
$props['columns'][] = [ | |
'width' => '1/1', | |
'sections' => $props['sections'] | |
]; | |
unset($props['sections']); | |
} | |
if (isset($props['columns'])) { | |
$props['tabs']['main']['columns'] = $props['columns']; | |
unset($props['columns']); | |
} | |
foreach ($props['tabs'] as &$tab) { | |
foreach ($tab['columns'] as &$column) { | |
foreach ($column['sections'] as $key => &$section) { | |
if($key === 'fields' && !is_array($section['fields'])) { | |
$column['sections']['fields'] = [ | |
'type' => 'fields', | |
'fields' => $column['sections']['fields'] | |
]; | |
$column['sections'] = self::replace_key($column['sections'], 'fields', self::randomSectionName()); | |
} | |
} | |
} | |
} | |
return $props; | |
} | |
} | |
PageBlueprint::$presets['shortcuts'] = Closure::fromCallable(['shortcutsPreset', 'preset']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment