Skip to content

Instantly share code, notes, and snippets.

@martinherweg
Last active November 13, 2017 13:12
Show Gist options
  • Save martinherweg/e6627b45a625f56e1eb585c045d61e80 to your computer and use it in GitHub Desktop.
Save martinherweg/e6627b45a625f56e1eb585c045d61e80 to your computer and use it in GitHub Desktop.
Create new Sections based on a Base JSON
{
"sections": [
{
"name": "A New Section",
"handle": "a_new_section_handle"
}
]
}
const path = require('path');
const fs = require('fs-extra')
const glob = require('glob');
const BaseData = require('./sections/SectionsBase.json');
let NewSections = [];
// load all filePaths of Departments
const departmentFiles = glob.sync('**/Department*.json', {
cwd: './scripts/sections/',
realpath: true
});
// dynamic require all DepartmentFiles and rest them in them in the newSections Array;
departmentFiles.forEach(file => {
NewSections = [...NewSections, ...require(file)['sections']]
})
const { sections: BaseSections, entryTypes: BaseEntryTypes } = BaseData;
NewSections = NewSections.map(section => {
return {
...BaseSections[0],
...section
}
});
let NewEntryTypes = NewSections.map(section => {
return BaseEntryTypes.map(type => {
const newHandle = {
sectionHandle: section.handle,
};
return {
...type,
...newHandle
}
})
});
// flatten NewEntryTypes
NewEntryTypes = NewEntryTypes.reduce((a,b) => {
return a.concat(b);
},[]);
BaseData.sections = NewSections;
BaseData.entryTypes = NewEntryTypes;
try {
fs.writeJsonSync('./dist/craft/config/thearchitect/_master.json', BaseData);
console.log('Import File successfully created');
} catch (error) {
console.error(error);
}
{
"groups": ["Komponenten", "Grid", "Media", "Page"],
"sections": [
{
"name": "Seitenbaum",
"handle": "pages",
"type": "structure",
"enableVersioning": "1",
"typesettings": {
"hasUrls": "1",
"template": "pages/_entry",
"urlFormat": "{slug}",
"nestedUrlFormat": "{parent.uri}/{slug}",
"en": {
"urlFormat": "{slug}",
"nestedUrlFormat": "{parent.uri}/{slug}"
}
}
}
],
"fields": [
{
"group": "Komponenten",
"name": "Bild",
"handle": "figure",
"instructions": "",
"required": false,
"type": "Assets",
"typesettings": {
"useSingleFolder": "",
"sources": "*",
"defaultUploadLocationSource": "assets",
"defaultUploadLocationSubpath": "",
"singleUploadLocationSource": "assets",
"singleUploadLocationSubpath": "",
"restrictFiles": "1",
"allowedKinds": ["image"],
"limit": "1",
"viewMode": "large",
"selectionLabel": ""
}
},
{
"group": "Komponenten",
"name": "Button Link",
"handle": "buttonLink",
"instructions": "",
"required": false,
"type": "FruitLinkIt",
"typesettings": {
"types": ["email", "tel", "custom", "entry", "asset"],
"defaultText": "",
"allowCustomText": "1",
"allowTarget": "1",
"entrySources": [],
"entrySelectionLabel": "Select an entry",
"assetSources": "*",
"assetSelectionLabel": "Select an asset",
"categorySources": "*",
"categorySelectionLabel": "Select a category"
}
},
{
"group": "Grid",
"name": "Card Thumbnails anzeigen",
"handle": "gridShowThumbnails",
"instructions": "",
"required": false,
"type": "Lightswitch",
"typesettings": {
"default": "1"
}
},
{
"group": "Grid",
"name": "Cards ausw\u00e4hlen",
"handle": "gridCards",
"instructions": "",
"required": false,
"type": "Entries",
"typesettings": {
"sources": [],
"targetLocale": "",
"limit": "",
"selectionLabel": "Cards hinzuf\u00fcgen"
}
},
{
"group": "Media",
"name": "Download",
"handle": "download",
"instructions": "",
"required": false,
"type": "Assets",
"typesettings": {
"useSingleFolder": "",
"sources": "*",
"defaultUploadLocationSource": "assets",
"defaultUploadLocationSubpath": "",
"singleUploadLocationSource": "assets",
"singleUploadLocationSubpath": "",
"restrictFiles": "1",
"allowedKinds": ["excel", "pdf", "text", "word"],
"targetLocale": "",
"limit": "1",
"viewMode": "large",
"selectionLabel": ""
}
},
{
"group": "Komponenten",
"name": "Intro",
"handle": "intro",
"instructions": "",
"required": false,
"type": "RichText",
"typesettings": {
"configFile": "Simple.json",
"availableAssetSources": "*",
"availableTransforms": "*",
"cleanupHtml": "1",
"purifyHtml": "1",
"purifierConfig": "",
"columnType": "text"
}
},
{
"group": "Media",
"name": "Media: Multiple Images",
"handle": "mediaMultipleImages",
"instructions":
"The image get's uploaded in a folder generated by the slug of the entry.",
"required": false,
"type": "Assets",
"typesettings": {
"useSingleFolder": "1",
"sources": "*",
"defaultUploadLocationSource": "assets",
"defaultUploadLocationSubpath":
"{% set uploadfolder = '' %} {% if object is neoblock %} {% if object.owner.elementType == 'Category' %} {% set uploadfolder = object.owner.slug %} {% else %} {% set uploadfolder = object.owner.slug %} {% endif %} {% elseif object is not neoblock and object.elementType == 'Category' %} {% set uploadfolder = object.owner.slug %} {% elseif object is not neoblock %} {% set uploadfolder = object.slug|default('unknown-slug') %} {% elseif object.owner is neoblock %} {% set uploadfolder = object.owner.owner.slug|default('unknown-slug') %} {% else %} {% set uploadfolder = 'unknown-slug/' %} {% endif %} {{ uploadfolder }}",
"singleUploadLocationSource": "assets",
"singleUploadLocationSubpath":
"{% set uploadfolder = '' %} {% if object is neoblock %} {% if object.owner.elementType == 'Category' %} {% set uploadfolder = object.owner.slug %} {% else %} {% set uploadfolder = object.owner.slug %} {% endif %} {% elseif object is not neoblock and object.elementType == 'Category' %} {% set uploadfolder = object.owner.slug %} {% elseif object is not neoblock %} {% set uploadfolder = object.slug|default('unknown-slug') %} {% elseif object.owner is neoblock %} {% set uploadfolder = object.owner.owner.slug|default('unknown-slug') %} {% else %} {% set uploadfolder = 'unknown-slug/' %} {% endif %} {{ uploadfolder }}",
"restrictFiles": "1",
"allowedKinds": ["image"],
"targetLocale": "",
"limit": "",
"viewMode": "large",
"selectionLabel": "Bild hinzuf\u00fcgen"
}
},
{
"group": "Media",
"name": "Media: Single Video",
"handle": "mediaSingleVideo",
"instructions": "",
"required": false,
"type": "Assets",
"typesettings": {
"useSingleFolder": "1",
"sources": "*",
"defaultUploadLocationSource": "assets",
"defaultUploadLocationSubpath": "",
"singleUploadLocationSource": "assets",
"singleUploadLocationSubpath": "videos",
"restrictFiles": "1",
"allowedKinds": ["json"],
"targetLocale": "",
"limit": "1",
"viewMode": "large",
"selectionLabel": "Video ausw\u00e4hlen"
}
},
{
"group": "Komponenten",
"name": "Richtext Editor",
"handle": "richtext",
"instructions": "",
"required": false,
"type": "RichText",
"typesettings": {
"configFile": "Standard.json",
"availableAssetSources": "*",
"availableTransforms": "*",
"cleanupHtml": "1",
"purifyHtml": "1",
"purifierConfig": "",
"columnType": "text"
}
},
{
"group": "Grid",
"name": "Untergeordnete Seiten anzeigen",
"handle": "gridShowChildren",
"instructions": "",
"required": false,
"type": "Lightswitch",
"typesettings": {
"default": "1"
}
},
{
"group": "Page",
"name": "Inhalt",
"handle": "components",
"instructions": "",
"required": false,
"type": "Neo",
"typesettings": {
"maxBlocks": null,
"groups": {
"name": [],
"sortOrder": []
},
"blockTypes": {
"new0": {
"sortOrder": "1",
"name": "Sektion",
"handle": "sektion",
"maxBlocks": null,
"childBlocks": [
"intro",
"figure",
"richtext",
"grid",
"video",
"button"
],
"maxChildBlocks": null,
"topLevel": "1",
"fieldLayout": []
},
"new1": {
"sortOrder": "2",
"name": "Intro",
"handle": "intro",
"maxBlocks": "1",
"childBlocks": "",
"maxChildBlocks": null,
"topLevel": "0",
"fieldLayout": {
"Blockquote": ["intro"]
}
},
"new2": {
"sortOrder": "3",
"name": "Bild",
"handle": "figure",
"maxBlocks": null,
"childBlocks": "",
"maxChildBlocks": null,
"topLevel": "0",
"fieldLayout": {
"Bild": ["figure"]
}
},
"new3": {
"sortOrder": "4",
"name": "Richtext / Headline",
"handle": "richtext",
"maxBlocks": null,
"childBlocks": "",
"maxChildBlocks": null,
"topLevel": "0",
"fieldLayout": {
"Richtext / Headline": ["richtext"]
}
},
"new4": {
"sortOrder": "5",
"name": "Cards",
"handle": "grid",
"maxBlocks": null,
"childBlocks": "",
"maxChildBlocks": null,
"topLevel": "0",
"fieldLayout": {
"Grid": ["gridShowChildren", "gridCards", "gridShowThumbnails"]
},
"reasons": {
"gridCards": [
[
{
"fieldId": "gridShowChildren",
"compare": "==",
"value": "false"
}
]
]
}
},
"new5": {
"sortOrder": "6",
"name": "Video",
"handle": "video",
"maxBlocks": null,
"childBlocks": "",
"maxChildBlocks": null,
"topLevel": "0",
"fieldLayout": {
"Video": ["mediaSingleVideo"]
},
"relabel": [
{
"field": "mediaSingleVideo",
"name": "Video",
"instructions": null
}
]
},
"new6": {
"sortOrder": "7",
"name": "Button",
"handle": "button",
"maxBlocks": null,
"childBlocks": "",
"maxChildBlocks": null,
"topLevel": "0",
"fieldLayout": {
"Button": ["buttonLink"]
}
}
}
}
}
],
"entryTypes": [
{
"sectionHandle": "pages",
"hasTitleField": "1",
"titleLabel": "Titel",
"name": "Einfache Seite",
"handle": "page",
"fieldLayout": {
"Page": ["mediaMultipleImages", "components"]
},
"relabel": [
{
"field": "mediaMultipleImages",
"name": "Stage",
"instructions":
"SliderImages"
},
{
"field": "images",
"name": "Stage",
"instructions":
"SliderImages"
}
]
},
{
"sectionHandle": "pages",
"hasTitleField": "1",
"titleLabel": "Titel",
"name": "Arzt",
"handle": "doctor",
"fieldLayout": {
"Page": ["mediaMultipleImages", "jameda", "components"]
},
"relabel": [
{
"field": "mediaMultipleImages",
"name": "Stage",
"instructions":
"SliderImages"
}
]
},
{
"sectionHandle": "pages",
"hasTitleField": "1",
"titleLabel": "Titel",
"name": "Video",
"handle": "video",
"fieldLayout": {
"Page": ["mediaMultipleImages", "components", "mediaSingleVideo"]
},
"relabel": [
{
"field": "mediaMultipleImages",
"name": "Stage",
"instructions":
"SliderImages"
},
{
"field": "mediaSingleVideo",
"name": "Video",
"instructions": null
}
]
},
{
"sectionHandle": "pages",
"hasTitleField": "1",
"titleLabel": "Titel",
"name": "Download",
"handle": "download",
"fieldLayout": {
"Page": ["mediaMultipleImages", "download"]
},
"relabel": [
{
"field": "mediaMultipleImages",
"name": "Stage",
"instructions":
"SliderImages"
}
]
},
{
"sectionHandle": "pages",
"hasTitleField": "1",
"titleLabel": "Titel",
"name": "Kontakt",
"handle": "contact",
"fieldLayout": {
"Page": ["mediaMultipleImages", "components"]
},
"relabel": [
{
"field": "mediaMultipleImages",
"name": "Stage",
"instructions":
"SliderImages"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment