Skip to content

Commit

Permalink
Add default icon: "box"
Browse files Browse the repository at this point in the history
  • Loading branch information
medienbaecker committed Dec 28, 2024
1 parent a90d5e7 commit 1f328e6
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Kirby\Toolkit\Str;
use Kirby\Data\Yaml;

function createModuleRegistry() {
function createModuleRegistry()
{

$registry = ['blueprints' => [], 'templates' => [], 'pageModels' => []];

Expand All @@ -18,8 +19,8 @@ function createModuleRegistry() {
// ----------------------------------------------------------------------

foreach (Dir::dirs($modulesFolder) as $folder) {
$blueprintPath = $modulesFolder . "/". $folder . "/" . $folder . ".yml";
$templatePath = $modulesFolder . "/". $folder . "/" . $folder . ".php";
$blueprintPath = $modulesFolder . "/" . $folder . "/" . $folder . ".yml";
$templatePath = $modulesFolder . "/" . $folder . "/" . $folder . ".php";
$registry = addToModulesRegistry($registry, $folder, $blueprintPath, $templatePath);
}

Expand All @@ -41,14 +42,14 @@ function createModuleRegistry() {
// Legacy registry (deprecated)
// ----------------------------------------------------------------------

$moduleBlueprints = array_filter(kirby()->blueprints(), function($blueprint) {
$moduleBlueprints = array_filter(kirby()->blueprints(), function ($blueprint) {
return Str::startsWith($blueprint, 'module.');
});

// Check if there are any module blueprints and throw a deprecation warning
if(count($moduleBlueprints)) {
if (count($moduleBlueprints)) {
deprecated('This folder structure is deprecated. Please move your module blueprints to site/blueprints/modules/ and your module templates to site/snippets/modules/.');
foreach($moduleBlueprints as $moduleBlueprint) {
foreach ($moduleBlueprints as $moduleBlueprint) {
$blueprintPath = $blueprintsFolder . "/pages/" . $moduleBlueprint . ".yml";
$templatePath = $snippetsFolder . "/modules/" . $moduleBlueprint . ".php";
$registry = addToModulesRegistry($registry, $moduleBlueprint, $blueprintPath, $templatePath);
Expand All @@ -60,16 +61,17 @@ function createModuleRegistry() {
// ----------------------------------------------------------------------

$blueprintNames = array_keys($registry['blueprints']);
$blueprintNames = array_map(function($blueprintName) {
$blueprintNames = array_map(function ($blueprintName) {
return Str::replace($blueprintName, 'pages/', '');
}, $blueprintNames);

foreach($registry['blueprints'] as &$blueprint) {
if(
foreach ($registry['blueprints'] as &$blueprint) {
if (
!isset($blueprint['options']) ||
(
is_array($blueprint['options']) && !isset($blueprint['options']['changeTemplate']
))
is_array($blueprint['options']) && !isset(
$blueprint['options']['changeTemplate']
))
) {
$blueprint['options']['changeTemplate'] = $blueprintNames;
}
Expand All @@ -78,7 +80,7 @@ function createModuleRegistry() {
// ----------------------------------------------------------------------
// Add modules container blueprint with pages section and redirect field
// ----------------------------------------------------------------------

$registry['blueprints']['pages/modules'] = [
'title' => 'Modules',
'options' => [
Expand All @@ -97,34 +99,34 @@ function createModuleRegistry() {
]
],
];

// ----------------------------------------------------------------------
// Add modules container model
// ----------------------------------------------------------------------

$registry['pageModels']['modules'] = 'ModulesPage';

return $registry;

}

function addToModulesRegistry(array $registry, string $name, string $blueprintPath, string $templatePath) {
function addToModulesRegistry(array $registry, string $name, string $blueprintPath, string $templatePath)
{

// Check if blueprint already exists in registry
if(array_key_exists('pages/module.'. $name, $registry['blueprints'])) {
if (array_key_exists('pages/module.' . $name, $registry['blueprints'])) {
return;
}

// Check if blueprint exists
if(!F::exists($blueprintPath)) {
if (!F::exists($blueprintPath)) {
return;
}

// Turn the blueprint into an array
$blueprintArray = Yaml::read($blueprintPath);

// Set up default values for status and navigation
if(!array_key_exists('status', $blueprintArray)) {
if (!array_key_exists('status', $blueprintArray)) {
$blueprintArray['status'] = [
'draft' => [
'text' => false
Expand All @@ -134,34 +136,36 @@ function addToModulesRegistry(array $registry, string $name, string $blueprintPa
]
];
}
if(!array_key_exists('navigation', $blueprintArray)) {
if (!array_key_exists('navigation', $blueprintArray)) {
$blueprintArray['navigation'] = ['status' => 'all', 'template' => 'all'];
}
if (!array_key_exists('icon', $blueprintArray)) {
$blueprintArray['icon'] = "box";
}

// Adjust the create blueprint option if it doesn't exist
if(!array_key_exists('create', $blueprintArray)) {
if (!array_key_exists('create', $blueprintArray)) {

$blueprintArray['create'] = [];

// Add slug field if autoslug is enabled
if(option('medienbaecker.modules.autoslug') === true) {
if (option('medienbaecker.modules.autoslug') === true) {
$blueprintArray['create']['slug'] = '{{ page.uniqueModuleSlug }}';
}

// Set status to listed if autopublish is enabled
if(option('medienbaecker.modules.autopublish') === true) {
if (option('medienbaecker.modules.autopublish') === true) {
$blueprintArray['create']['status'] = 'listed';
}

// Disable redirect if the redirect option is not explicitely set to true
if(option('medienbaecker.modules.redirect') !== true) {
if (option('medienbaecker.modules.redirect') !== true) {
$blueprintArray['create']['redirect'] = false;
}

}

// Add module prefix to blueprint name
if(Str::startsWith($name, 'module.') === false) {
if (Str::startsWith($name, 'module.') === false) {
$name = 'module.' . $name;
}

Expand All @@ -171,5 +175,4 @@ function addToModulesRegistry(array $registry, string $name, string $blueprintPa
$registry['pageModels'][$name] = option('medienbaecker.modules.model', 'ModulePage');

return $registry;

}
}

0 comments on commit 1f328e6

Please sign in to comment.