Skip to content

Instantly share code, notes, and snippets.

@evanlouie
Created April 27, 2017 23:47
Show Gist options
  • Save evanlouie/b0aaf761ac3fba336f51800bfadd3763 to your computer and use it in GitHub Desktop.
Save evanlouie/b0aaf761ac3fba336f51800bfadd3763 to your computer and use it in GitHub Desktop.
Schematic Schema FieldTab to Matrix Dumper
<?php
public function actionDumpFields()
{
$name = 'Resource';
$matrixFieldHandle = 'resource';
$section = 'resources';
$entryType = 'resources';
$schema = CRAFT_CONFIG_PATH.'/schema.yml';
$yaml = \Symfony\Component\Yaml\Yaml::parse(file_get_contents($schema));
$generateMatrixBlock = function ($name) {
return [
'name' => $name,
'required' => 'false',
'instructions' => '',
'translatable' => '0',
'type' => 'Matrix',
'settings' => ['maxBlocks' => '1'],
'context' => 'global',
'blockTypes' => [
'entry' => [
'name' => 'Entry',
'fields' => [],
],
],
];
};
$allFields = [];
foreach ($yaml['fields'] as $group => $fields) {
foreach ($fields as $fieldHandle => $field) {
$allFields[$fieldHandle] = $field;
}
}
$newBlocks = [];
$fieldLayoutTabs = $yaml['sections'][$section]['entryTypes'][$entryType]['fieldLayout']['tabs'];
foreach ($fieldLayoutTabs as $tabName => $fieldHandleToRequiredMap) {
$newBlock = $generateMatrixBlock($tabName);
foreach ($fieldHandleToRequiredMap as $fieldHandle => $isRequired) {
$newBlock['blockTypes']['entry']['fields'][$fieldHandle] = $allFields[$fieldHandle];
$newBlock['blockTypes']['entry']['fields'][$fieldHandle]['required'] |= (bool) $isRequired;
}
$newBlocks[strtolower($tabName)] = $newBlock;
}
$newMatrixBlockYaml = \Symfony\Component\Yaml\Yaml::dump($newBlocks, PHP_INT_MAX, 2);
echo $newMatrixBlockYaml;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment